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