[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: G++ rejects legal code



> Why only one data member?  Members sy and st are public in the base
> class.  Initialization in the constructor body works OK. Moreover,
> (week argument, but) Visual C++ compiles this OK.

This is explained in 12.6.2, [class.base.init]. Paragraph 1 explains
the syntax for member initializers

ctor-initializer: 
   : mem-initializer-list 

mem-initializer-list: 
   mem-initializer 
   mem-initializer , mem-initializer-list 

mem-initializer: 
   mem-initializer-id ( expression-list opt ) 

mem-initializer-id: 
   ::opt nested-name-specifier-opt class-name 
   identifier

Then, paragraph 2 explains what "identifier" means here

# Names in a mem-initializer-id are looked up in the scope of the
# constructor s class and, if not found in that scope, are looked up
# in the scope containing the constructor s definition.  [Note: ...]
# Unless the mem-initializer-id names a nonstatic data member of the
# constructor's class or a direct or virtual base of that class, the
# mem-initializer is ill-formed.  A mem-initializer-list can
# initialize a base class using any name that denotes that base class
# type.

So the identifier is only looked up in the scope of the class itself,
not in any base classes; it must name a data member or a base class.

In the body of the constructor, it is not initialization anymore - it
is assignment. Inside a method, name lookup will find member names of
the base class.

Please note that specifying an initialization in a derived class would
be extremely confusing. In C++, each object is initialized
(constructed) only once. Now, each constructor first calls the base
class constructor. The base class constructor, in turn, will
initialize all members. Now, in the derived class, you want to
initialize the member again???

I recommend to study a book on C++.

Regards,
Martin



Reply to: