Argument Constructor/Default Constructor

Started by
6 comments, last by JohnBolton 17 years, 2 months ago
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;
}



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;}
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.
sorry - my speed-reading is terrible - I removed my incorrect post
moe.ron
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 hereclass 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.]
Moved to For Beginners.
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.
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement