Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Order of member initialization
Re: Order of member initialization [message #58885 is a reply to message #58883] Wed, 21 September 2022 11:49 Go to previous messageGo to previous message
peterh is currently offline  peterh
Messages: 108
Registered: November 2018
Location: Germany
Experienced Member
Yes, I did not stop testing after I wrote this and discovered problems.

Did you see my previous example some messages above, where the array is allocated from Heap?
class B : public A{
public:
	Array<int> &b_array;
	B() : A(*(new Array<int>)),b_array(A::array){
        ..........
        }


This works securely, I believe, because the array is constructed and initialized to an empty array when it is allocated.
This ensures the array is constructed, before A and B are constructed.

I do however not know if C++ guarantees that A is constructed before B. This could be different with different compilers.
I do not find much about this in my books and online, and this could mean it is implementation and compiler vendor dependant.
However, if this can be guaranteed, then this should be a clean solution.

The most clean way is probably: create an empty array and give it to the B::B(....) constructor as an argument. Then B has no need to create and initialize the array.

[Updated on: Wed, 21 September 2022 12:09]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Can TheIDE be used in two instances?
Next Topic: Some Questions
Goto Forum:
  


Current Time: Wed May 08 20:01:53 CEST 2024

Total time taken to generate the page: 0.02556 seconds