954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how does C++ know which constructor to call if you don't specify one?

Please explain.
Does it automatically include the default constructor?

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

do you mean if you don't explicitly create a constructor in your class, or are you referring to constructor overloading?

if it's that you don't explicitly create your own, here's what happens

#include <iostream> //feel free to give this a test to see for yourself, it should compile ok
class Test
{
    public:
        int x;
};

int main()
{
    Test testObject;
    std::cout << testObject.x << std::endl; //this will print the compiler default for an int
    return 0;
}


the object will be created with the default values for all fields.

If you are asking about constructor overloading, it's just like function overloading: the compiler looks at the function signature to choose it for you.

for more info, maybe check out: http://www.cplusplus.com/doc/tutorial/classes/

Hope that's helpful

~J

jesseb07
Junior Poster
111 posts since Dec 2006
Reputation Points: 76
Solved Threads: 15
 

If you do not explicitly define a constructor for your class, the program will call the default constructor. This is not recommended as your variables will contain random value. Constructor and destructor are highly recommended to carefully handle memory (constructor - to initialize values of variable before they are used)
(destructor - to prevent memory leak)


Regards,
Nick

chiwawa10
Junior Poster
156 posts since Jul 2005
Reputation Points: 88
Solved Threads: 27
 

If not defined the compiler create a :

default constructor like so someClass() { }, that does nothing
default destructor like so ~someClass() {}, that does nothing
default assignment operator
default copy constructor
default address operator

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

basically i am trying to use constructors with operator overloading. I think i understand. You can call these constructors in other methods.

EX: maybe a set_x() and set_y() in the constructor which accepts two double values. The set_x(double x). You can call the input method and call this particular constructor.

I am getting really confused on when to use operator overloading.
Can someone explain?

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

when you want a operator to show other then default behavior you should use operator overloading.
for example you have created a class ComplexNumber.
by default + operator doesn't add two ComplexNumber, here you need to overload + operator.

DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

@firstPerson the default Constructor & Destructor are not always trivial at the Compiler level, to users it might be...

Hi lotrsimp12345,

This usually irritates the programmer that why the compiler emits a default constructor.

Let me explain you a bit detail of how C++ standard dictate about default constructor and its semantics.

if your class contains primitive types, then your default constructor would be trivial i.e.

class Foo {
public:
  int a;
}

if we consider the Foo class its default constructor would be trivial i.e. doesn't modify the state and probably member "a" contains the garbage (or some 0xCCCCCCCC etc). Basically in C++ its always the programmer's responsiblity to intialize the values. This is what standard tells us. Logically if we look with the eye of programmer it seems irritating for us to explicitly intialize the value, yes it is, but we have some other facilities (default argument values) in the language to acheive the same.

class Foo { 
    Bar b;
}


In above code Bar is the subobject which must be initialized before the intialization of Foo in this case compiler synthesized the constructor which in turns calls the default constructor for Bar. same goes for inheritance as well and obviously destructor would release the memory by calling the destructor of subobjects.

if C++ default constructor initializes the memory to "ZERO values" for primitive types then the name of default constructor should be "ZERO Constructor" :) rather than default constructor.

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 

If there is no user defined constructor in C++ classes then the compiler will automatically invoke the default constructor by itself.
It will applicable only if you dont have any constructor as well you need to try to create an object.

packrisamy
Newbie Poster
3 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You