Thread: Call constructor

  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
    28,413
    Instead of showing "pseudo code", write what you think is the real code, and attempt to compile it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:03 AM