member variable initialization
-
Hello everyone, I am wondering in the following code, member variable a in class B is not put in the initialization list or constructor of B directly, but it is initialized. How and when member variable a of class B is created and initialized? Is constructor of B invokes constructor of a? Output is, In constructor A In constructor B
using namespace std; class A { public: A() { cout << "In constructor A" << endl; } }; class B { public: A a; B() { cout << "In constructor B" << endl; } }; int main() { B b; return 0; }
thanks in advance, George
You can find all answers here http://msdn2.microsoft.com/en-us/library/kstd9k24(vs.71).aspx[^] :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
You can find all answers here http://msdn2.microsoft.com/en-us/library/kstd9k24(vs.71).aspx[^] :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Thanks CPallini, My question is answered. regards, George
-
Hello everyone, I am wondering in the following code, member variable a in class B is not put in the initialization list or constructor of B directly, but it is initialized. How and when member variable a of class B is created and initialized? Is constructor of B invokes constructor of a? Output is, In constructor A In constructor B
using namespace std; class A { public: A() { cout << "In constructor A" << endl; } }; class B { public: A a; B() { cout << "In constructor B" << endl; } }; int main() { B b; return 0; }
thanks in advance, George
Order of the member variables affects initialization. Play around with the order of the A and B member variables in C:
#include <iostream> using std::wcout; class A { public: A() { wcout << L"A\n"; } }; class B { public: B() { wcout << L"B\n"; } }; class C { A a; B b; public: C() : a() { wcout << L"D\n"; } }; int _tmain(int argc, _TCHAR* argv[]) { C c; wcout << L"End\n"; }
More constructor information: http://www.parashift.com/c++-faq-lite/ctors.html[^] Free e-books: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html[^]
"We make a living by what we get, we make a life by what we give." --Winston Churchill
modified on Tuesday, December 18, 2007 10:40:05 AM
-
Order of the member variables affects initialization. Play around with the order of the A and B member variables in C:
#include <iostream> using std::wcout; class A { public: A() { wcout << L"A\n"; } }; class B { public: B() { wcout << L"B\n"; } }; class C { A a; B b; public: C() : a() { wcout << L"D\n"; } }; int _tmain(int argc, _TCHAR* argv[]) { C c; wcout << L"End\n"; }
More constructor information: http://www.parashift.com/c++-faq-lite/ctors.html[^] Free e-books: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html[^]
"We make a living by what we get, we make a life by what we give." --Winston Churchill
modified on Tuesday, December 18, 2007 10:40:05 AM
Thanks George, The output is, A B D End. But I do not know what do you want to prove? Any more descriptions please? regards, George
-
Order of the member variables affects initialization. Play around with the order of the A and B member variables in C:
#include <iostream> using std::wcout; class A { public: A() { wcout << L"A\n"; } }; class B { public: B() { wcout << L"B\n"; } }; class C { A a; B b; public: C() : a() { wcout << L"D\n"; } }; int _tmain(int argc, _TCHAR* argv[]) { C c; wcout << L"End\n"; }
More constructor information: http://www.parashift.com/c++-faq-lite/ctors.html[^] Free e-books: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html[^]
"We make a living by what we get, we make a life by what we give." --Winston Churchill
modified on Tuesday, December 18, 2007 10:40:05 AM
please, George, don't give too much to George_George. he's been abusing this board for several months, and he doesn't even search the web before asking very simple question. so, please, don't give him too easy answers...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Thanks George, The output is, A B D End. But I do not know what do you want to prove? Any more descriptions please? regards, George
George_George wrote:
I do not know what do you want to prove?
are you stupid ? you have the order of execution, so you can deduce from there the order in which the members are initialized... :doh:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
George_George wrote:
I do not know what do you want to prove?
are you stupid ? you have the order of execution, so you can deduce from there the order in which the members are initialized... :doh:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
perhaps a stronger example could be
...
public:
C() : b()
{
wcout << L"D\n";
}
...:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
George_George wrote:
I do not know what do you want to prove?
are you stupid ? you have the order of execution, so you can deduce from there the order in which the members are initialized... :doh:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
are you stupid ?
You're definitely unable to be patient with that guy! :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
toxcct wrote:
are you stupid ?
You're definitely unable to be patient with that guy! :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
PATIENT ? WTF !!! i've been waiting 1 year and a half for an answer to a question of mine, and you call me not patient ? :mad:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
PATIENT ? WTF !!! i've been waiting 1 year and a half for an answer to a question of mine, and you call me not patient ? :mad:
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
i've been waiting 1 year and a half for an answer to a question of mine, and you call me not patient ?
and did he eventually give it? :rolleyes:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
toxcct wrote:
i've been waiting 1 year and a half for an answer to a question of mine, and you call me not patient ?
and did he eventually give it? :rolleyes:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
eventually ? have you ever seen that monkey replying to one of your questions ? but you know, those chinese people (i mean, the ones living in china) have so much restrictions over their head by their governement that i wouldn't be astonished that they are feared to give too much personal informations. anyway, i definitely know a good one about him : George_George is a prat, a moron, wait, A DICKHEAD !! someone with a minimal common sense wouldn't reask same things again and again.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
eventually ? have you ever seen that monkey replying to one of your questions ? but you know, those chinese people (i mean, the ones living in china) have so much restrictions over their head by their governement that i wouldn't be astonished that they are feared to give too much personal informations. anyway, i definitely know a good one about him : George_George is a prat, a moron, wait, A DICKHEAD !! someone with a minimal common sense wouldn't reask same things again and again.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
have you ever seen that monkey replying to one of your questions ?
Yes, he did. I understand you're angry with him, but... be patient. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
toxcct wrote:
have you ever seen that monkey replying to one of your questions ?
Yes, he did. I understand you're angry with him, but... be patient. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
CPallini wrote:
Yes, he did.
where ? and about what ? if it was about a technical question, yes, he will sometimes answer. about personnal question, never. and not only he doesn't reply not really exactly, but he totally ignore us.
CPallini wrote:
I understand you're angry with him, but... be patient.
as long as he continue in his direction, I have absolutely no reason to change my idea of this guy. it's up to him to show me a better side of his.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Thanks George, The output is, A B D End. But I do not know what do you want to prove? Any more descriptions please? regards, George
Change the order of A and B in the C class and see the result:
class C { B b; A a; public: C() : a() { wcout << L"D\n"; } };
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
please, George, don't give too much to George_George. he's been abusing this board for several months, and he doesn't even search the web before asking very simple question. so, please, don't give him too easy answers...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Toxcct, I understand your pain, and have been following George_George exploits. He bites the hand that feeds him! However, my goal is to help and I won't let posters such as George_George change my focus just because he is a SOAB. Geo
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Toxcct, I understand your pain, and have been following George_George exploits. He bites the hand that feeds him! However, my goal is to help and I won't let posters such as George_George change my focus just because he is a SOAB. Geo
"We make a living by what we get, we make a life by what we give." --Winston Churchill
George L. Jackson wrote:
just because he is a SOAB
lol, i feel lighter now that someone else have the same feeling than me. BTW, I do have the same goal too, and I generally answer his technical questions, but there a time when he already has enough to find his answer. it's not our job to pound out a full working code. we are here to advice and to help when others are in real need. posting just to post is not my religion.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Tuesday, December 18, 2007 10:33:26 AM
-
CPallini wrote:
Yes, he did.
where ? and about what ? if it was about a technical question, yes, he will sometimes answer. about personnal question, never. and not only he doesn't reply not really exactly, but he totally ignore us.
CPallini wrote:
I understand you're angry with him, but... be patient.
as long as he continue in his direction, I have absolutely no reason to change my idea of this guy. it's up to him to show me a better side of his.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
where ? and about what ?
I asked him what is the reason behind all his questions. He answered was just for technical fun.
toxcct wrote:
I have absolutely no reason to change my idea of this guy
Oh, you don't have to. But I think you could just be more patient with him. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
CPallini wrote:
Yes, he did.
where ? and about what ? if it was about a technical question, yes, he will sometimes answer. about personnal question, never. and not only he doesn't reply not really exactly, but he totally ignore us.
CPallini wrote:
I understand you're angry with him, but... be patient.
as long as he continue in his direction, I have absolutely no reason to change my idea of this guy. it's up to him to show me a better side of his.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
it's up to him to show me a better side of his.
By telling you his age? How exactly does that matter to anything? :confused:
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
toxcct wrote:
it's up to him to show me a better side of his.
By telling you his age? How exactly does that matter to anything? :confused:
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
DavidCrow wrote:
By telling you his age?
Or if he comes from China? Well I think toxcct is just a curious guy. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
George L. Jackson wrote:
just because he is a SOAB
lol, i feel lighter now that someone else have the same feeling than me. BTW, I do have the same goal too, and I generally answer his technical questions, but there a time when he already has enough to find his answer. it's not our job to pound out a full working code. we are here to advice and to help when others are in real need. posting just to post is not my religion.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Tuesday, December 18, 2007 10:33:26 AM
Sometimes, I get over helpful. However, I just modified his code a bit and asked him to play with the order of the member variables to lead him to his own Aha! moment. Of course, his post proved: He was what I thought he was, and I let him off the hook. Geo
"We make a living by what we get, we make a life by what we give." --Winston Churchill