initializing a member pointer

M

majsta

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();
}
 
C

Cupu

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-

Cheers
 
S

Saeed Amrollahi

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();
}
 
D

David Harmon

On Wed, 28 Nov 2007 03:44:32 -0800 (PST) in comp.lang.c++,
(e-mail address removed) 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.
 
J

James Kanze

@majsta.net wrote,
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top