开发者

Importance of a singlecolon ":" in C++ [duplicate]

This question already has answers here: 开发者_如何学C Closed 10 years ago.

Possible Duplicate:

What is this weird colon-member syntax in the constructor?

Rarely in the regular codes I encounter the a single colon in classes for e.g.:

A::member():b(),c()
{
}

What is the importance of the single colon over here? Why is it used here? Is it mandatory sometimes? If so in which cases?


A single colon in this context is used to signal that you are using an initializer list. An initializer list is used to:

  • Invoke base class constructors from a derived class
  • Initialize member variables of the class

As noted by others, an initializer list can only be used on a class constructor.

While it's also possible to initialize member variables in the body of the constructor, there are several reasons for doing so via the initializer list:

  • Constant pointers or references cannot be initialized from the body of the constructor
  • It is usually more efficient to use the initializer list as it will (from memory) only invoke the constructor for the member, rather than the constructor and assignment operator which can be costly for non-POD types.

Having said all of this, the formatting of your code is a bit odd. In the code that I usually work with, use of an initializer list would be indented like this:

A::A()
    :b(),
     c()
{
}

This makes it more clear to me that the : has no relation to the :: used to define class membership in A::A().


The single colon specifies an initialization list, as the other couple of responses have already stated.

There are a number of gotchas about these lists and constructors in general. C++ does a reasonable job of generating default constructors and copy constructors but if you write your own, it leaves it up to you to handle everything:

  1. Be cautious of using use the word invoke because the presence on the list specifies which constructor will be used, it doesn't actually invoke that constructor. By that I mean that the order of invocation is governed solely by the class declaration. Most compilers will warn you if the initalizations are non in the same order as declared. The order is - base classes constructors in the order of declaration then member variables. It might help to think of it as a depth-first traversal up the class hierarchy.
  2. The items in the list are specifications of which member variable or base class constructors will be used - particularly important for writing copy constructors.
  3. Base classes that are not specified in the list will have their default constructor invoked. This is a major gotcha when you write your own copy constructor - you have to be careful to specify the base copy constructors otherwise they will not get their members copied.
  4. Members not in the list are not initialized
  5. As a corollary to 4. you therefore have a maintenance headache of having to remember to add members to each constructor.
  6. As a good practice, try not to rely on member initialization order anyway. You should try very hard to avoid code where one member must be initialized before another.


It is used to initialize references. In c++ you cannot assign or modify references, so in class they can only be "assigned" with semicolon syntax.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜