Jump to content
  • Advertisement
Sign in to follow this  
kelaklub

Argument Constructor/Default Constructor

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the class below I have commented out my default constructor, and the call that is being made to it from main(). When I simply remove the comments for the default constructor call from main(), the compiler complains. Does the creation of an argument constructor override the compiler created default constructor for a class? If I remove both constructors (I know, I know you should always create your own constructor rather than leaving it to the compiler) from the class and only call the default constructor from main(), no compile errors. Is this overriding of the default constructor when an argument constructor is available part of the C++ standard? I used g++ (GCC) 3.4.2 (mingw-special) to compile. Thanks.
#include <iostream>
using namespace std;

class clPoint
{    
public:
    int x, y;

    /*clPoint() // Default constructor.
    {
        x = 0;
        y = 0;
    }*/

    clPoint(int XX, int YY)  // Argument constructor.
    {
        x = XX;
        y = YY;
    }
};

int main()
{
    //clPoint pointA; // Call to Default constructor.
    clPoint pointB(5, 5); // Call to Argument constructor.
    return 0;
}



Share this post


Link to post
Share on other sites
Advertisement
Yes, if you supply any constructor, the compiler will not generate one, but you can always have a constructor with default parameters:

clPoint(int XX=0, int YY=0)
{
x = XX;
y = YY;
}

Share this post


Link to post
Share on other sites
I believe the answer to your question is basically yes.

The compiler doesn't really override the default constructor; it just doesn't make one. A default constructor is only created if the user doesn't define any constructors.

Share this post


Link to post
Share on other sites
Quote:
Original post by kelaklub
In the class below I have commented out my default constructor, and the call that is being made to it from main(). When I simply remove the comments for the default constructor call from main(), the compiler complains. Does the creation of an argument constructor override the compiler created default constructor for a class?

Yup. In essence, you're telling the compiler not to allow any objects to get made unless you do it yourself. That's handy when you want to make sure things are in a certain state, and the default constructor just can't do it for you. Another common way to do this is to make the default constructor private. (The same is often done with copy constructors to prevent unauthorized copying, used for counting references and such.)
Quote:
If I remove both constructors (I know, I know you should always create your own constructor rather than leaving it to the compiler) from the class and only call the default constructor from main(), no compile errors. Is this overriding of the default constructor when an argument constructor is available part of the C++ standard?

I believe so, but can't point to it.

Just to point out: your "argumented" constructor can actually act as the default constructor, too, if you give the arguments default values:
#include <iostream>
//using namespace std; no use for this guy here

class clPoint
{
public:
int x, y;

// don't need YOU any more...
/*clPoint() // Default constructor.
{
x = 0;
y = 0;
}*/


clPoint(int XX = 10, int YY = 10) // Argument constructor.
{
x = XX;
y = YY;
}
};

int main()
{
clPoint pointA; // Call to Default (Argument!) constructor.
// pointA is now (10, 10)

clPoint pointB(5, 5); // Call to Argument constructor.

return 0;
}




Neat!
-jouley

[Edit: Drat! Mustn't eat and type responses concurrently any more. Also: Source.]

Share this post


Link to post
Share on other sites
Cool. Thanks for all your help. Oh and thanks for moving my post, your right I should have really posted it here. Yeah, I wanted to invoke objects of this other class I am creating, and really wanted the user to be aware about a few certain parameters. Never really thought about making a constructor or copy constructor private though, that does open up possibilities.

Share this post


Link to post
Share on other sites
Quote:
Original post by kelaklub
...I know, I know you should always create your own constructor rather than leaving it to the compiler...

No, you should create your own constructor only if it is needed. Unnecessary code is an unnecessary source of bugs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!