473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

C++ Initialisation List

A
Hi,

It is recommened by many practitioners to always use an initialisation list
to initialise data members of a class. However, I am having problems with
such a list when passing parameters/arguments to a constructor of a class -
see code below:

class Foo{

int number;

public:
Foo(int number): this->number(number) {}
};

The problem lies with the this pointer and the fact that the arguments that
is passed has the same name as a data member of the class. Of course if i
renamed the argument and thus avoid the need of the this pointer the problem
will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?

Any help appreciated

Regards,
A
Jul 19 '05 #1
11 5401

"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
Hi,

It is recommened by many practitioners to always use an initialisation list to initialise data members of a class. However, I am having problems with
such a list when passing parameters/arguments to a constructor of a class - see code below:

class Foo{

int number;

public:
Foo(int number): this->number(number) {}
};

The problem lies with the this pointer and the fact that the arguments that is passed has the same name as a data member of the class. Of course if i
renamed the argument and thus avoid the need of the this pointer the problem will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?

Any help appreciated

Regards,
A


See a recent post by Andrey Tarasevich (subject "Re: End-of-the-week fun")
that indicates the parameter and the data member may have the same name with
no problems. Just drop the "this" from your example...
Jul 19 '05 #2
A wrote:
...
class Foo{

int number;

public:
Foo(int number): this->number(number) {}
};

The problem lies with the this pointer and the fact that the arguments that
is passed has the same name as a data member of the class. Of course if i
renamed the argument and thus avoid the need of the this pointer the problem
will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?
...


Just drop 'this->' and you are done:

class Foo{
int number;
public:
Foo(int number): number(number) {}
};

The memeber names are looked up in the scope of the class ('Foo::number'
is found), the names used in the initializer are looked up in the scope
of the constructor (parameter 'number' is found).

However, many will regard this approach to member/parameter naming as an
example of bad coding style. Maybe you should invent a different name
for either the data member or the constructor parameter.

--
Best regards,
Andrey Tarasevich

Jul 19 '05 #3
"Andrey Tarasevich" <an**************@hotmail.com> schreef in bericht
news:P4********************@comcast.com...
A wrote:
...
class Foo{

int number;

public:
Foo(int number): this->number(number) {}
};

The problem lies with the this pointer and the fact that the arguments that is passed has the same name as a data member of the class. Of course if i renamed the argument and thus avoid the need of the this pointer the problem will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?
...


Just drop 'this->' and you are done:

class Foo{
int number;
public:
Foo(int number): number(number) {}
};

The memeber names are looked up in the scope of the class ('Foo::number'
is found), the names used in the initializer are looked up in the scope
of the constructor (parameter 'number' is found).

However, many will regard this approach to member/parameter naming as an
example of bad coding style. Maybe you should invent a different name
for either the data member or the constructor parameter.


I end all data members with an underscore.

class Point
{
public:
Point(int x, int y);
private:
int x_;
int y_;
};
Jul 19 '05 #4
>
I end all data members with an underscore.

class Point
{
public:
Point(int x, int y);
private:
int x_;
int y_;
};


what's the [rational] reason for "ending all data members with an
undescore" ?
__________________________________________________ _____________________________
Posted Via Uncensored-News.Com - Accounts Starting At $6.95 - http://www.uncensored-news.com
<><><><><><><> The Worlds Uncensored News Source <><><><><><><><>

Jul 19 '05 #5
helge wrote:

I end all data members with an underscore.

class Point
{
public:
Point(int x, int y);
private:
int x_;
int y_;
};


what's the [rational] reason for "ending all data members with an
undescore" ?


To show that they are data members and to avoid any accidental hiding
between member variables and parameters or local variables.

Jul 19 '05 #6
helge wrote:

I end all data members with an underscore.

class Point
{
public:
Point(int x, int y);
private:
int x_;
int y_;
};


what's the [rational] reason for "ending all data members with an
undescore" ?


To differentiate member variables from other variables
such as variables local to a function or module. It is
just one of many popular styles.

Another style is to prefix all member variables with "m_".
But the big point to remember is that all names starting
with an underscore are reserved for the compiler.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 19 '05 #7
Thomas Matthews wrote in news:3F**************@sbcglobal.net:
Another style is to prefix all member variables with "m_".
But the big point to remember is that all names starting
with an underscore are reserved for the compiler.


Such identifiers are reserved only in the global namespace,
this also include's anything at namspace scope declared extern "C".

Identifiers containing a double undescore are reserved everywhere
as are identifiers that start with an undescore followed by an
uppercase letter (presumably A-Z).

So prepending a *single* undescore to member-names that don't
begin with one of A-Z is OK.

Some say that identifiers begining with an uppercase E are reserved
too (as macro's for errno), I haven't seen the relevant quotes from
the standard('s), so I remain to be convinced on this one, still I
avoid all uppercase identifiers begining with E.

And the there is all the macro's defined by the C library getc,
errno, assert etc.

This should probably be in the FAQ, but I couldn't goggle it.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #8
Rolf Magnus wrote:
helge wrote:

I end all data members with an underscore.

class Point
{
public:
Point(int x, int y);
private:
int x_;
int y_;
};


what's the [rational] reason for "ending all data members with an
undescore" ?

To show that they are data members and to avoid any accidental hiding
between member variables and parameters or local variables.


well "hungarian notation" [and clones] are not a part of the language,
the "this" pointer is:

....

private: int m;

void foo(int m ) {
this->m =m;
}

....
__________________________________________________ _____________________________
Posted Via Uncensored-News.Com - Accounts Starting At $6.95 - http://www.uncensored-news.com
<><><><><><><> The Worlds Uncensored News Source <><><><><><><><>

Jul 19 '05 #9

"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
Hi,

It is recommened by many practitioners to always use an initialisation list to initialise data members of a class. However, I am having problems with
such a list when passing parameters/arguments to a constructor of a class - see code below:

class Foo{

int number;

public:
Foo(int number): this->number(number) {}
};

The problem lies with the this pointer and the fact that the arguments that is passed has the same name as a data member of the class. Of course if i
renamed the argument and thus avoid the need of the this pointer the problem will go away. However, is there a solution to this if i cannot avoid
changing the name of the argument?


Actually, that "trick" has always worked for me: you can use the same name
for the constructor parameter and the member variable (without the this
pointer, of course), and it won't get confused. I don't know if it is
allowed by the standard, but it has worked on all the compilers I've tried.
Jul 19 '05 #10

"helge" <he***@helge.net> wrote in message
news:3f********@news6.uncensored-news.com...

I end all data members with an underscore.

class Point
{
public:
Point(int x, int y);
private:
int x_;
int y_;
};


what's the [rational] reason for "ending all data members with an
undescore" ?


Ummm, to avoid the "problem" that is the subject of this thread? (Of
course, it actually *is* a problem with most normal functions.)
Jul 19 '05 #11

"helge" <he***@helge.net> wrote in message
news:3f********@news6.uncensored-news.com...

well "hungarian notation" [and clones] are not a part of the language,
the "this" pointer is:

...

private: int m;

void foo(int m ) {
this->m =m;
}

...


That's not really the point.
a) you shouldn't wait till the body of the constructor to initialize what
you can initialize in the initializer list
b) "this->" is clunky and redundant. One of the points of OO is to
eliminate extra things like this (much less need for function parameters,
etc.)
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Samuele Armondi | last post by:
Hi, I'm writing some exception class for a project, and I've got a class called BasicException that looks like this: class BasicException { private: std::string Description; int Number; ...
106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
2
by: John Smith | last post by:
Hi, I have a question regarding the initialisation of aggregates: The C (99) standard states: section 6.7.8, paragraph 21 states: If there are fewer initializers in a brace-enclosed list than...
3
by: Raghu | last post by:
Hello all, Can somebody help me hopw to resolve teh probelm of aggregate initialisation in c++. Her eis the piece of code. #include<stdio.h> class MyTest { public:
13
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e....
4
by: Kevin Frey | last post by:
I have an assembly written in C++/CLI that also links in non-clr Native C++ (the C++/CLI wraps the Native C++ functionality). This assembly has an in-built tracing system that needs to be...
3
by: Pantokrator | last post by:
Hi all, I've got a question about the scope of the constructor initialisation list. First of all, let me explain my classes: // ***************************************************** class...
6
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.