7

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?

5
  • 2
    how else would you create an object if not by calling the constructor? Commented Feb 26, 2016 at 9:41
  • 3
    The terminology is kind of misleading (IMHO) regarding constructors. A constructor doesn't actually construct or create the object, the object is already created when the constructor is called. What the constructor really does is initialize the already created object. Commented Feb 26, 2016 at 9:42
  • 2
    The constructor initialize the object, aka initialize members etc. It doesnt really allocate memory for it etc, this is done before. About the fact there is always a constructor is because makes life easier and the language slightly easier to parse Commented Feb 26, 2016 at 9:42
  • 1
    As far as I know, it's literally just an initializer and it can be optimized away if no initialization is necessary. Like if you have a chunk of (void*) memory, you should be able to cast it to a trivial object without invoking the constructor and the object should work just fine (if it actually does need the construction, then you'd need to call the constructor on that memory chunk via placement-new). Commented Feb 26, 2016 at 10:09
  • @pasha I'd recommend C++ Primer, 5th Edition over the one you're reading; it's also part of the definitive C++ book guide and list. Commented Feb 26, 2016 at 10:35

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

8 Comments

But when I'm using copy assignment, no constructor would be called right?
@pasha: "Copy assignment" can only be used to assign the value(s) of one instance of an object to another. Thus, both would already exist and had their constructors called earlier. If you use copy construction then the (implicit or explicit) copy constructor is called.
For a class A, A a; A b=a;...How many times would be the constructor called in this case?
@pasha there is diff between statements int x = 20 and x=30 ;
A a; Calls the default constructor. A b=a; Calls the copy constructor (not the copy assignment operator). b = a calls the copy assignment operator (not the copy constructor).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.