Call constructor

This is a discussion on Call constructor within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Is it legal and good code to call one constructor from another in the same class? I show ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Call constructor

    Hello everyone,


    Is it legal and good code to call one constructor from another in the same class? I show the pseudo code below.

    Code:
    Test(bool param1, bool param2) 
    {
    	// set and manipulate param1
    
    	// call constructor with a single bool input parameter
    
    	Test (param2);
    }
    
    Test(bool param2) : active (param2)
    {
    	// do something
    }

    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Instead of showing "pseudo code", write what you think is the real code, and attempt to compile it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    I have compiled it. The compile error and my code are listed below.

    But I do not understand what does the compile error mean -- " error C2082: redefinition of formal parameter 'param2'" and "error C2512: 'Test' : no appropriate default constructor available"?

    Code:
    class Test {
    private:
    	bool active;
    public:
    	
    	Test (bool param2) : active (param2)
    	{
    		// do something
    	}
    
    	Test(bool param1, bool param2) 
    	{
    		// set and manipulate param1
    
    		// call constructor with a single bool input parameter
    
    		Test (param2);
    	}
    
    };
    1>d:\visual studio 2008\projects\test_20080324\test_20080324\main2.cp p(17) : error C2082: redefinition of formal parameter 'param2'
    1>d:\visual studio 2008\projects\test_20080324\test_20080324\main2.cp p(17) : error C2512: 'Test' : no appropriate default constructor available

    Quote Originally Posted by laserlight View Post
    Instead of showing "pseudo code", write what you think is the real code, and attempt to compile it.

    regards,
    George

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    The compiler is interpreting the code as:
    Code:
    	Test(bool param1, bool param2) 
    	{
    		// set and manipulate param1
    
    		// call constructor with a single bool input parameter
    
    		Test param2;
    	}
    Now, it is clear that you are trying to create an object named param2. However, there is a bool parameter named param2, thus you get a "redefinition of formal parameter 'param2'" error. Furthermore, this Test object is created with the default constructor, but there is no default constructor for Test, thus the "no appropriate default constructor available".

    Basically, at least under the current C++ Standard, you cannot chain constructor calls, unlike in say, Java.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    Question answered.

    Quote Originally Posted by laserlight View Post
    The compiler is interpreting the code as:
    Code:
    	Test(bool param1, bool param2) 
    	{
    		// set and manipulate param1
    
    		// call constructor with a single bool input parameter
    
    		Test param2;
    	}
    Now, it is clear that you are trying to create an object named param2. However, there is a bool parameter named param2, thus you get a "redefinition of formal parameter 'param2'" error. Furthermore, this Test object is created with the default constructor, but there is no default constructor for Test, thus the "no appropriate default constructor available".

    Basically, at least under the current C++ Standard, you cannot chain constructor calls, unlike in say, Java.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  3. Call constructor within constructor. How to?
    By xErath in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 05:30 PM
  4. Can't find my constructor
    By Nippashish in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 08:17 PM
  5. Arguments up constructor tree?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 01:03 AM

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