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 #58868 is a reply to message #58867] Tue, 20 September 2022 17:05 Go to previous messageGo to previous message
Tom1
Messages: 1212
Registered: March 2007
Senior Contributor
peterh wrote on Tue, 20 September 2022 17:47
I must correct myself, it doesnt work.
This code:
#include <Core/Core.h>

using namespace Upp;

class A{
public:
	Array<int> *array;
	A(Array<int> &array_) : array(&array_){
		Cout() <<"construct A\n";
		Cout() << "Array Initial item count in A = " << array->GetCount() << "\n";
	}
};

class B : public A{
public:
	Array<int> b_array;
	void *p=&b_array;
	B() : A(b_array){
		Cout() <<"construct B\n";
		Cout() << "Array Initial item count in B = " << b_array.GetCount() << "\n";
	}
};

CONSOLE_APP_MAIN{
	B b;
	Cout() <<"Fertig\n";
	Sleep(1000000);
}

produces this output:
construct A
Array Initial item count in A = 107746496
construct B
Array Initial item count in B = 0
Fertig


So constructor A is executed, before B was constructed.

It should however work, if A doesnt use or touch the referenced array before it was constructed.


Hi Peter,

Yes, it does work when the constructor of A does not use or pass the array on to be used by others. However, this may get difficult to control when projects are large.

Best regards,

Tom
 
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: Thu May 09 03:01:49 CEST 2024

Total time taken to generate the page: 0.01855 seconds