"Ivan Liu" <da*******@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
Thanks a lot.
However I don't quite understand the logic..
It seems like inehritance but I don't have much experience
and I don't understand the meaning of this simple line:
>>
MyClass(T &objA) : obj(objA) {}
That line is a constructor for the MyClass class. The part ": obj(objA)" is
called the "initializer list".
The ":" specifies the start of the list, and is followed by a
comma-separated list of initializers. (In this case, there's only one
initializer: "obj(objA)".) The "obj" is the name of the member to be
initialized, and the "(objA)" part tells the compiler what to initialize obj
with. So this initializer list initializes the member obj to the reference
passed in as a parameter: objA.
This is the only way to initialize a member reference, since you can't
assign to a reference using "=". (Using "=" on a reference changes the
_value_ of what it refers to, it doesn't make it refer to something else,
like it does when using "=" on a pointer.)
Look up initializer lists in your favorite C++ textbook for more info and
examples.
-Howard