7 Years
Discussion Span
Last Post by packrisamy

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
        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



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)



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


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?


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.


@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 {
  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.

very informative.

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.