Aquarius_Girl
Aquarius_Girl

Reputation: 22916

What is the role of a default (implicit) constructor exactly?

From Thinking in C++

The first hidden argument to the constructor is the this pointer. this pointer holds the address of the caller object. In the case of constructor this pointer points towards an uninitialized block of memory. It is the job of the constructor to initialize this block of memory properly.

So, basically, the constructor is used to get a pointer to the object's space in RAM? Is that correct?

How does the default constructor initialize that memory? Does it get initialized to zero?

Upvotes: 1

Views: 1122

Answers (5)

Didier Trosset
Didier Trosset

Reputation: 37437

No. The constructor is not used to get a pointer. The constructor receives this pointer.

The constructor is used to initialize the object's space in RAM.

The default constructor does what you want it to do! I mean your can write your own default constructor for your class. This default constructor can initialize the memory, or leave it uninitialized, as you choose.

In the same vein, the implicit default constructor will be written automatically by the compiler to initialize the object RAM space. It will take care of initializing the non-static member variables of your class that do have default constructors. For example, if your class has a non-static std::string as member variable, then std::string default constructor will be called to initialize this variable. All non-static member variables of POD types (int, char, pointers,...) will not be initialized.

Upvotes: 1

Emilio Garavaglia
Emilio Garavaglia

Reputation: 20730

So, basically, the constructor is used to get a pointer to the object's space in RAM? Is that correct?

No, that's just a consequence

How does the default constructor initialize that memory? Does it get initialized to zero?"

No: the default constructor is just a function. It does what what you define it to do. It is normally designed to "initialize" the memory by assigning to the contained variables (the object member) a proper value or by calling in turn their own constructors.

Think to this:

class point
{
public:
   int x,y; //< just two values

   point() //< default constructor
   { x=0; y=0; } //< function body assigning values
};

or

class point
{
public:   
   int x,y; //< just two values

   point() //< default constructor
    :x(), y() //< member init-list explicitly calling contrutors
   {} //< empty body
};

Note that, until you don't declare a variable of type point nothing will be executed.

int main()
{
   point p; //< p created and point::point() called using p as *this
   p.x=5; //< x member of p changed
   return 0; //just exit main (jumps to the closing } )
} //< p destructor called here, using p as *this.

As per the "implicit default constructor", what it does is just:

  • call the bases implicit default constuctors
  • call the members implicit default contructors.

For all class-based types, decaring a default ctor in fact replaces the implicit one, but -for "built-in types" (like int, char, float etc.)- the default explicit constructor -in fact- sets the value to zero, but the implicit one does nothing, thus letting the value uninitialized. (the implementations can use different techniques to come to this same result)

Upvotes: 0

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

So, basically, the constructor is used to get a pointer to the object's space in RAM? Is that correct?

No, the constructor is not used to get a pointer.

The purpose of the constructor is to initialize all member variables when an object of this class is created. It initializes the object in memory.

The default constructor is the constructor that can be called with no arguments.

When you implement a class, you can implement your own default constructor to make it do what you want it to do.

When you do not provide any constructor for your class, the compiler will provide a default constructor for this class, but it will not initialize the members.

Default constructors are significant because they are automatically invoked in certain circumstances:

  • When an object value is declared with no argument list, e.g. MyClass x;; or allocated dynamically with no argument list, e.g. new MyClass or new MyClass(); the default constructor is used to initialize the object
  • When an array of objects is declared, e.g. MyClass x[10]; or allocated dynamically, e.g. new MyClass [10]; the default constructor is used to initialize all the elements
  • When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called
  • When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called
  • In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly, e.g. vector<MyClass>(10); initializes the vector with 10 elements, which are filled with the default-constructed value of our type.

In the above circumstances, it is an error if the class does not have a default constructor.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409176

The role of the default constructor is the same as any other constructor: To initialize the object. The default constructor (or a constructor with only default arguments) sets member variables to a known state, like setting pointers to nullptr (or 0), etc.

Lets say we have this structure:

struct Foo
{
    int a;
    int* b;

    // Default constructor
    // Set `a` to `1`, and `b` to `nullptr`
    Foo() : a(1), b(nullptr) {}
};

Now when you declare an instance of the Foo class, then you know what state the member variables will have. If there wasn't a constructor, a normal local variable instance of the Foo class would have undefined values for the a and b members.

If you don't provide a constructor at all, default or other, then the compiler will make a default constructor for you (one is needed, or you would not be able to create instances of the classes), but it will not do any initialization of members.

The hidden argument talked about, is the variable known as this, and it exists for all member functions which are not static.

Upvotes: 2

Mark Garcia
Mark Garcia

Reputation: 17708

No.

MyClass obj;                    // Default construct
MyClass* obj2 = new MyClass;    // Also default construct

In the first one, space for obj is first allocated, typically in the stack. The second one is allocated on the heap. The addresses where they reside are the ones that are passed to the proper constructor in these case.

The first-argument-is-the-this-pointer situation does not only apply to the default constructor, as a hidden this pointer is passed to all non-static class methods as their first arguments[1].

Then you probably know the rest. The constructor sets-up and initializes the object's members and states and any other things that needs to be done.


[1]Have you observed some examples like this?

my_type obj;
func_that_calls_a_callback(std::bind(&my_type::method, &obj));

That demonstrates that the first parameter is a pointer to some object is bound to the first parameter of my_type::method. That becomes the this pointer which points, in this case, to obj.

Upvotes: 0

Related Questions