An interesting code about a constructor calling a constructor

This is a discussion on An interesting code about a constructor calling a constructor within the C++ Programming forums, part of the General Programming Boards category; Code: class A { public: int i; A():i(0){ cout<<"A1:"<<endl; cout<<"i="<<i<<endl; cout<<"A1 End"<<endl; } A(int x):i(x) { cout<<"A2:"<<endl; cout<<"i="<<i<<endl; A(); cout<<"i="<<i<<endl; ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    An interesting code about a constructor calling a constructor

    Code:
    class A
    {
    public:
    	int i;
    	A():i(0){
    		cout<<"A1:"<<endl;
    		cout<<"i="<<i<<endl;
    		cout<<"A1 End"<<endl;
    	}
    	A(int x):i(x)
    	{
    		cout<<"A2:"<<endl;
    		cout<<"i="<<i<<endl;
    		A();
    		cout<<"i="<<i<<endl;
    		cout<<"A2 End"<<endl;
    	}
    };
    
    int main()
    {
        A obj(2);
    }
    It's interesting to see that when A() modifies i to 0, A2 still has i as 2. How come?!

    int A::i is shared in the obj, right?

  2. #2
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    When it returns form A() it returns I to 2.

    I assume the primary constructor takes precedence.

    Why would you do this anyway, seems a bit asinine.
    Last edited by Terran; 05-31-2008 at 04:09 PM.

  3. #3
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    It's the same with inheritance too!

    Code:
    class B
    {
        public:
        int theInt;
        B():theInt(0) { cout << "B, theInt=" << theInt << endl; };
        B(int x):theInt(x) { cout << "B, theInt=" << theInt << endl; };
    
    };
    class A : public B
    {
    public:
    	int i;
    	A():B(),i(0) {
    
    		cout<<"A1:";
    		cout<<"i="<<i<<endl;
    		cout<<"A1 End"<<endl;
    	}
    	A(int x):B(x)
    	{
    
    		cout<<"A2:";
    		cout<<"i="<<i<<endl;
    		B();
    		A();
    		cout<<"A:B, theInt=" << B::theInt<<endl;
    		cout<<"i="<<i<<endl;
    		cout<<"A2 End"<<endl;
    	}
    };
    
    int main()
    {
        A obj(2);
        system("pause");
        return EXIT_SUCCESS;
    }
    Last edited by Terran; 05-31-2008 at 04:32 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    This is not multiple inheritance. This is single inheritance.
    As to why, I only know that &i inside A(int) != &i inside A().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    The question to your answer is - it isn't.
    But you may be confused by the actual events.
    First constructor A(int) is invoked, which invokes constructor B(int).
    In the body of A(int), it then invokes B(), which returns and then invokes A(), which then invokes B().
    Then it returns from the constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Quote Originally Posted by meili100 View Post
    Code:
    class A
    {
    public:
    	int i;
    	A():i(0){
    		cout<<"A1:"<<endl;
    		cout<<"i="<<i<<endl;
    		cout<<"A1 End"<<endl;
    	}
    	A(int x):i(x)
    	{
    		cout<<"A2:"<<endl;
    		cout<<"i="<<i<<endl;
    		A();
    		cout<<"i="<<i<<endl;
    		cout<<"A2 End"<<endl;
    	}
    };
    
    int main()
    {
        A obj(2);
    }
    It's interesting to see that when A() modifies i to 0, A2 still has i as 2. How come?!

    int A::i is shared in the obj, right?
    The line "A();" creates a completely new object of type A, unrelated to obj. If you want i to be shared between different copies of A objects, make it a static member.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by King Mir View Post
    The line "A();" creates a completely new object of type A, unrelated to obj. If you want i to be shared between different copies of A objects, make it a static member.
    Painfully obvious... just not to me... lol

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Code:
    class A
    {
    public:
    	int i;
    	A():i(0){
    		cout<<"A1:"<<endl;
    		cout<<"i="<<i<<endl;
    		cout<<"A1 End"<<endl;
    	}
    	A(int x):i(x)
    	{
    		cout<<"A2:"<<endl;
    		cout<<"i="<<i<<endl;
    		this->A(); //modify here
    		cout<<"i="<<i<<endl;
    		cout<<"A2 End"<<endl;
    	}
    };
    
    int main()
    {
        A obj(2);
    }
    Why I can't use this->A() to qualify the call?!

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Because a constructor is not a method. You'll need a separate reset() method to reset an object to it's initial state.

    Actually, there is another way. you can call the destructor then placement new. But you can't do that inside the constructor.
    Last edited by King Mir; 05-31-2008 at 11:36 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    That sounds like an awful hack anyway.
    Break the constructor out into a common method and call it instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    53
    In C++09, this is added as a new feature. The code you had is wrong because you simply instantiate a new A which disappears at the end of the (constructor) scope (like King Mir pointed out); you would expect it to work in an initializer list instead:

    Code:
    A(int x) : A() { ... }
    This will not compile today.

    The idea is that you often have a "basic" constructor which does a bunch of work, and some "specialized" ones which all do the same "basic" work plus something extra. Currently, you'll have to break out the "basic" stuff in a separate function, instead of calling the basic constructor from within the initializer list of the "specialized" ones.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. interesting error code returned from sento()
    By ufsargas in forum C Programming
    Replies: 1
    Last Post: 06-22-2006, 01:59 PM
  2. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21