Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the constructor creates objects of a class?

I was reading

Object-Oriented Programming in C++ by Robert Lafore and it is mentioned(pg. no. 235)

"If there was no constructor, an implicit no-argument constructor is built into the program automatically by the compiler, and it’s this constructor that created the objects, even though we didn’t define it in the class. This no-argument constructor is called the default constructor. If it weren’t created automatically by the constructor, you wouldn’t be able to create objects of a class for which no constructor was defined."

Does the constructor creates the object?

I understand a constructor can be used to initialize an object. But even if I don't need to initialize objects of the class I'm creating, a default constructor is generated by the compiler. Therefore I suspect there is some other use/need of a constructor. Is there any?

like image 656
pasha Avatar asked Feb 26 '16 09:02

pasha


2 Answers

Does the constructor creates objects of a class?

No, it simply initializes the members of a class to some valid initial state. Allocation of space for an object is not its duty.

Therefore I suspect there is some other use/need of a constructor. Is there any?

No, classes can exist without any constructors; even without the compiler provided one. An example (live here):

struct A {
    A() = delete;  // prevent the compiler-provided default constructor
    int x;
};

int main() {
    A a = {1};     //    OK: aggregate initialization
    A b;           // Error: use of deleted function
}

When no constructors are defined for a class, the members are left default initialized, which for primitives would be undefined (garbage) and for objects of a custom type would be to call their respective default constructors which may or may not initialize the object. This behaviour is what the compiler-provided default constructor (called a trivial default constructor) exhibits: it is just there in name but doesn't do anything.

However, instead of saying the class has no constructor, we say it has a default constructor, which shouldn't be confused with a user-provided default constructor which may do proper initializations. Remember, a default constructor is just a constructor which can be called with zero arguments, user-provided or not.

Some might think the constructor is a misnomer for an initializer and they are partly right. However, the creator of the language is partly right too, since from raw bits (of memory), the notion of an object comes into existence only after the constructor is through with it; it constructs the object from raw memory by setting its states to some valid values, to give the notion of a coherent object.

like image 129
legends2k Avatar answered Sep 28 '22 01:09

legends2k


Does the constructor creates the object?

There are 2 things that are done when an object is created.

  • Allocation of memory for the object
  • The initialization of the object.

It's slightly misleading to say that the constructor creates the object. The call to the constructor simply does the initialization part. For example, when you define an automatic variable:

Classname instance;

The compiler takes care of the allocating memory for the object when it's block is entered and calls the constructor when the execution reaches the declaration of the variable.

Technically, it's enough to allocate space for an object if the type is "Plain Old Data", but using a constructor for initialization is strongly encouraged in C++.

Therefore I suspect there is some other use/need of a constructor.

The use of a constructor is, as you mentioned, to initialize the object to a valid state.

But when I'm using copy assignment, no constructor would be called right?

Constructor is not used when copy assignment is used. But when you use copy assignment, then object that you assign to has already been created and the constructor was called when that happened.

For a class A, A a; A b=a;...How many times would be the constructor called in this case?

Twice. A a Here, the default constructor is used. A b=a Here, the copy constructor is used. b is copy initialized. There is no copy assignment in this example.

A b(a); and A b = a; do these both call different operators/functions?

No. They both call the copy constructor. The first is explicit call to the copy constructor and the second is copy initialization.

like image 45
eerorika Avatar answered Sep 28 '22 03:09

eerorika