Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. member variable initialization

member variable initialization

Scheduled Pinned Locked Moved C / C++ / MFC
question
27 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    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

    CPalliniC G E 3 Replies Last reply
    0
    • G George_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

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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.

      In testa che avete, signor di Ceprano?

      G 1 Reply Last reply
      0
      • CPalliniC CPallini

        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.

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks CPallini, My question is answered. regards, George

        1 Reply Last reply
        0
        • G George_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

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          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

          G T 2 Replies Last reply
          0
          • G George L Jackson

            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

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            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

            T G 2 Replies Last reply
            0
            • G George L Jackson

              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

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              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]

              G N 2 Replies Last reply
              0
              • G George_George

                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

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                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]

                CPalliniC G 3 Replies Last reply
                0
                • T toxcct

                  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]

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  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.

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • T toxcct

                    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]

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    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.

                    In testa che avete, signor di Ceprano?

                    T 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      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.

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      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]

                      CPalliniC 1 Reply Last reply
                      0
                      • T toxcct

                        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]

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #11

                        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.

                        In testa che avete, signor di Ceprano?

                        T 1 Reply Last reply
                        0
                        • CPalliniC CPallini

                          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.

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #12

                          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]

                          CPalliniC 1 Reply Last reply
                          0
                          • T toxcct

                            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]

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #13

                            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.

                            In testa che avete, signor di Ceprano?

                            T 1 Reply Last reply
                            0
                            • CPalliniC CPallini

                              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.

                              T Offline
                              T Offline
                              toxcct
                              wrote on last edited by
                              #14

                              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]

                              CPalliniC D 2 Replies Last reply
                              0
                              • G George_George

                                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

                                G Offline
                                G Offline
                                George L Jackson
                                wrote on last edited by
                                #15

                                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

                                G 1 Reply Last reply
                                0
                                • T toxcct

                                  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]

                                  G Offline
                                  G Offline
                                  George L Jackson
                                  wrote on last edited by
                                  #16

                                  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

                                  T 1 Reply Last reply
                                  0
                                  • G George L Jackson

                                    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

                                    T Offline
                                    T Offline
                                    toxcct
                                    wrote on last edited by
                                    #17

                                    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

                                    G 1 Reply Last reply
                                    0
                                    • T toxcct

                                      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]

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #18

                                      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.

                                      In testa che avete, signor di Ceprano?

                                      1 Reply Last reply
                                      0
                                      • T toxcct

                                        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]

                                        D Offline
                                        D Offline
                                        David Crow
                                        wrote on last edited by
                                        #19

                                        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

                                        CPalliniC 1 Reply Last reply
                                        0
                                        • D David Crow

                                          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

                                          CPalliniC Offline
                                          CPalliniC Offline
                                          CPallini
                                          wrote on last edited by
                                          #20

                                          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.

                                          In testa che avete, signor di Ceprano?

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups