[prog] C++ Templates

Katie Lucas katie at fysh.org
Wed Nov 12 11:44:50 EST 2003


On Mon, Jun 30, 2003 at 10:09:39AM -0700, Laurel Fan wrote:
> On Thu, Jun 26, 2003 at 05:06:11PM +0200, Dan Richter wrote:
> > The "::" indicates membership. The ":" (which is used only in a 
> > constructor) is used to call the constructors of class members. This is 
> > frivolous in this case (you're calling the "constructor" of an int), but 
> > when you're dealing with complex classes it can be useful.
> 
> Actually, using initializer lists in constructors to initialize
> members, though not necessary (you can initialize them with = in the
> function body of the constructor), is considered good style by many,
> including in Scott Meyers' book "Effective C++".

This is because there is a difference between the two.

If you use an initialiser in an initialiser list, you create the
object and pass in that value as the param to the constructor.

If you assign with = in the coinstuctor body, the member first has its
null constructor called and then its assignment called. This is
significant if the member doesn't have a null constructor. An example
of this would be a referenced -- you can't have a reference that
doesn't point to something so it has to have the thing it points to
passed in when you make it: hence you HAVE to use an initialiser list
entry for a member which is a reference.




More information about the Programming mailing list