By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

initializing a member pointer

P: n/a
hello, I have probably a stupid question, but I don't get one thing.
In the following source, the first constructor "does not work" - it
creates a pointer variable pI, sets ii as *pI, but this created pI is
not the member one, the *C::pI is still uninitialised. In the later
constructor it's ok.
What's wrong with the first one?
Thank you for your help and time

m
#include <iostream>

class C
{
private:
int *pI;
public:
/**/
// this constuctor does not work
C(int ii) {
int *pI = new int(ii);
}
/**/
// this one is ok
/*
C(int ii) {
int *pTmp = new int(ii);
pI = pTmp;
}
*/
~C() {delete pI;}
void print()
{
std::cout << " *pI = " << *pI << std::endl;
}
};
int main(int argc, char* argv[])
{
C d(3);
d.print();
}

Nov 28 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 28, 1:44 pm, maj...@majsta.net wrote:
hello, I have probably a stupid question, but I don't get one thing.
In the following source, the first constructor "does not work" - it
creates a pointer variable pI, sets ii as *pI, but this created pI is
not the member one, the *C::pI is still uninitialised. In the later
constructor it's ok.
What's wrong with the first one?
Thank you for your help and time

m

#include <iostream>

class C
{
private:
int *pI;
public:
/**/
// this constuctor does not work
C(int ii) {
int *pI = new int(ii);
}
/**/
// this one is ok
/*
C(int ii) {
int *pTmp = new int(ii);
pI = pTmp;
}
*/
~C() {delete pI;}
void print()
{
std::cout << " *pI = " << *pI << std::endl;
}};

int main(int argc, char* argv[])
{
C d(3);
d.print();
}
Hi,

In the first constructor by doing "int *pI = new int(ii);" you
effectively declare a local int* variable named pI and hide the class
member C::pI, at scope end pI is destroyed and C::pI becomes visible
again.

If you don't redeclare pI (which is a mistake in this case) you'll get
the right behaviour since the compiler will translate that to this-
>pI.
Cheers
Nov 28 '07 #2

P: n/a
On Nov 28, 2:44 pm, maj...@majsta.net wrote:
hello, I have probably a stupid question, but I don't get one thing.
In the following source, the first constructor "does not work" - it
creates a pointer variable pI, sets ii as *pI, but this created pI is
not the member one, the *C::pI is still uninitialised. In the later
constructor it's ok.
What's wrong with the first one?
Thank you for your help and time

m

#include <iostream>

class C
{
private:
int *pI;
public:
/**/
// this constuctor does not work
C(int ii) {
int *pI = new int(ii);
You defind a local pI, independent from C::pI. The pI is initialized
with an int with ii value. You don't intialize the C::pI. The scope of
pI is the constructor body. You should initialize pI like this:
C(int ii) : pI(new int(ii)) {}
}
/**/
// this one is ok
/*
C(int ii) {
int *pTmp = new int(ii);
pI = pTmp;
}
*/
~C() {delete pI;}
void print()
{
std::cout << " *pI = " << *pI << std::endl;
}};

int main(int argc, char* argv[])
{
C d(3);
d.print();
}
Nov 28 '07 #3

P: n/a
On Wed, 28 Nov 2007 03:44:32 -0800 (PST) in comp.lang.c++,
ma****@majsta.net wrote,
> // this constuctor does not work
C(int ii) {
int *pI = new int(ii);
}
As others have written, it is completely wrong to declare a local
variable here that is named the same as your member variable.

But it's still wrong to use assignment instead of construction to
build member variables. Not that there is much difference in the
case of ints, but what you have should still be written as:

C(int ii) : pI(new int(ii)) {
}

Then next question would be WHY are you creating memory management
headaches for yourself by using 'new' at all here. I suppose this
is only a simplified example from your actual application, but
chances are you are still making trouble for no reason. Never use
'new' unless you have explored the alternatives and nothing easier
will do the job.
Nov 28 '07 #4

P: n/a
On Nov 28, 5:57 pm, David Harmon <sou...@netcom.comwrote:
On Wed, 28 Nov 2007 03:44:32 -0800 (PST) in comp.lang.c++,
maj...@majsta.net wrote,
// this constuctor does not work
C(int ii) {
int *pI = new int(ii);
}
As others have written, it is completely wrong to declare a local
variable here that is named the same as your member variable.
No one said that it was completely wrong. It's generally
considered bad style, because it is confusing to the reader, but
it is legal. In the case of constructors, in fact, it's not
that exceptional to give the argument (which also acts as a
local variable) the same name as the member.
But it's still wrong to use assignment instead of construction to
build member variables. Not that there is much difference in the
case of ints, but what you have should still be written as:
C(int ii) : pI(new int(ii)) {
}

Then next question would be WHY are you creating memory management
headaches for yourself by using 'new' at all here. I suppose this
is only a simplified example from your actual application, but
chances are you are still making trouble for no reason. Never use
'new' unless you have explored the alternatives and nothing easier
will do the job.
It's not always that simple. The usual reason for dynamic
allocation in such cases is polymorphism. And it's not always
possible to determine the desired actual type with a single
expression.

(In such cases, it's certainly worth considering using something
like boost::scoped_ptr for the member. If there's more than one
such pointer, you must consider some sort of special pointer
management.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 29 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.