Constructor Initializer

Author Topic: Constructor Initializer  (Read 217 times)

Offline beingchinmayTopic starter

  • Trade Count: (0)
  • Semi-Newbie
  • *
  • Thank You 0
  • Posts: 33
  • Karma: 0
  • Gender: Male
  • I am Chinmay
    • Fat Cutter
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