Constructor Initializer

Author Topic: Constructor Initializer  (Read 1093 times)

Offline beingchinmayTopic starter

  • Trade Count: (0)
  • Jr. Member
  • **
  • Thank You 1
  • Posts: 73
  • Karma: 0
  • Gender: Male
  • I am Chinmay
    • Modular Kitchen Designer
Constructor Initializer
« on: 09-07-2016, 05:27:55 »
A special syntax is used for initializing subelements of objects with constructors. Constructor initializers for structure and class members are specified by a colon and a comma-separated list that follows the constructor parameter list and that precedes the code body. A constructor initializer is a data member identifier followed by a parenthesized expression. Using this syntax, the counter constructor can be recoded as

// Default constructor for counter
inline counter::counter(int i = 0) :
value(i % 100) { }

The member variable value is initialized by the expression i % 100. The constructor definition has a compound statement that is empty. Notice that initialization replaces assignment. The individual members must be initializable as

member-name (expression list)

It is not always possible to assign values to members in the body of the constructor. An initializer list is required when a nonstatic member is either a const or a reference type.


Related Topics

  Subject / Started by Replies Last post
0 Replies
Last post 12-28-2015, 00:24:57
by chinmay.sahoo
5 Replies
Last post 07-08-2021, 10:01:58
by Lishmalinyjames