C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Scoping in initializer list

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Ham Pastrami
Guest





PostPosted: Sun Dec 17, 2006 7:34 am    Post subject: Scoping in initializer list Reply with quote



class Point {
public:
const int x, y;
Point(int x, int y);
}

Point::Point(int x, int y) : x(x), y(y)
{
}

This compiles with MSVC and the data seems correct at runtime, but is this
code safe or is it compiler-dependent? It seems ambiguous that the
initializer list is using member_x and arg_x at the same time. More
specifically, what should happen when

Point::Point(int x, int y) : x(x+1), y(x+y)

the initializer for y could be reasonably interpreted as either (member_x+y)
or (arg_x+y)?
Back to top
Michael Ashton
Guest





PostPosted: Sun Dec 17, 2006 8:38 am    Post subject: Re: Scoping in initializer list Reply with quote



Ham Pastrami wrote:

Quote:
class Point {
public:
const int x, y;
Point(int x, int y);
}

Point::Point(int x, int y) : x(x), y(y)
{
}

This compiles with MSVC and the data seems correct at runtime, but is this
code safe or is it compiler-dependent? It seems ambiguous that the
initializer list is using member_x and arg_x at the same time. More
specifically, what should happen when

Point::Point(int x, int y) : x(x+1), y(x+y)

the initializer for y could be reasonably interpreted as either (member_x+y)
or (arg_x+y)?

This might work, but it's bad naming. The constructor arguments should
have different names from the class members (or vice-versa).

As I understand the standard, this should compile and run as you
expect. Member initialiser lists are special; only base classes and
class members can be mentioned in them, so names of initialisers are
only searched for among those lists.

But arguments for initialisers can be a wider range of things,
including (of course) the arguments to the constructor. In this case,
the constructor arguments are in the narrower scope, so they shadow the
class members.

Nevertheless, it's confusing to read, and bad practice.

--mpa
Back to top
John Carson
Guest





PostPosted: Sun Dec 17, 2006 9:02 am    Post subject: Re: Scoping in initializer list Reply with quote



"Ham Pastrami" <nomail (AT) dot (DOT) com> wrote in message
news:o81hh.6655$Gr2.1536 (AT) newssvr21 (DOT) news.prodigy.net
Quote:
class Point {
public:
const int x, y;
Point(int x, int y);
}

Point::Point(int x, int y) : x(x), y(y)
{
}

This compiles with MSVC and the data seems correct at runtime, but is
this code safe or is it compiler-dependent? It seems ambiguous that
the initializer list is using member_x and arg_x at the same time.
More specifically, what should happen when

Point::Point(int x, int y) : x(x+1), y(x+y)

the initializer for y could be reasonably interpreted as either
(member_x+y) or (arg_x+y)?


This is covered in Section 12.6.2/7 of the Standard. In looking up
initializers, the constructor's arguments hide class members. If there are
no constructor arguments matching the initializer name, then class members
are looked up. Thus y(x+y) is y(arg_x+arg_y).

Formally,

"Names in the expression-list [i.e., the arguments of the items in the
initializer list] of a mem-initializer are evaluated in the scope of the
constructor for which the mem-initializer is specified."

So think what would happen in the body of a constructor. In the body of the
constructor (as with any other member function), function arguments hide
class members of the same name. Thus function arguments do the same when
used to initialize member variables.

I agree with Michael Ashton, however, that such code is bad style.

--
John Carson
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.