Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++11 in class initializer v default constructor?

9 views
Skip to first unread message

nvangogh

unread,
Apr 7, 2013, 10:39:21 AM4/7/13
to
The default constructor function initializes data members of a class
object, in one of two ways:

1. If an in class initializer is given, it will initialize the data
member with that value (why is a special function needed to do this?)
- or
2. If no in class initializer is present, it will initialize the data
member with a value that depends upon it's type.

Does this mean that constructors will become obsolete once we have use
of the new feature of in class initializers? If I am able to initialize
a variable directly and simply as a non class member variable, my
question is what purpose will a constructor serve then?

Bart van Ingen Schenau

unread,
Apr 9, 2013, 4:46:34 AM4/9/13
to
No, constructors will not become obsolete, especially not the
constructors that take arguments.
Constructors allow two things that are not possible with in-class
initializers:
- Initialization of members with a value that is not known (or whose
origin is unknown) at the point where the class is defined.
- Operations other than initialization of the members

With the existence of in-class initializers, the need for defining custom
default constructors will be reduced, but it will never be completely
gone.

Bart v Ingen Schenau
0 new messages