If you like SEOmastering Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Constructor Initializer

Started by beingchinmay, 09-07-2016, 06:27:55

Previous topic - Next topic

beingchinmayTopic starter

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


Quote// 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

Quotemember-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.



If you like SEOmastering Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...