J
Jerry Krinock
I've written the following demo to help me understand a problem I'm having
in a larger program. The "main" function constructs a Foo object, and then
later "reconstructs" it by calling the constructor again. In my larger
program, I find that the member variables don't get re-initialized when
"reconstructed". I don't have that problem in this demo, but the second
time the constructor is called, its "this" points to a different location.
My questions:
1. Why is the pointer 0xbffffad8 in the third output line, not 0x00300460
like the others?
2. Is there anything wrong with "reconstructing" an object as I have done
in "main"?
Thanks very much to anyone who can help!
Jerry Krinock
class Foo
{
int fooey ;
public:
Foo(int f)
{
fooey = f ;
cout << "In constructor, Addr=" << this << endl ;
}
int GetFooey() { return fooey ; }
} ;
int main ()
{
Foo* aFoo = new Foo(22) ;
cout <<"1. Addr=" <<aFoo <<", fooey=" <<(*aFoo).GetFooey() <<endl ;
// Now, "reconstruct" *aFoo with a different value for fooey
*aFoo = Foo(33) ;
cout <<"2. Addr=" <<aFoo <<", fooey=" <<(*aFoo).GetFooey() <<endl ;
}
****** Output: **********
In constructor, Addr=0x00300460
1. Addr=0x00300460, fooey=22
In constructor, Addr=0xbffffad8
2. Addr=0x00300460, fooey=33
in a larger program. The "main" function constructs a Foo object, and then
later "reconstructs" it by calling the constructor again. In my larger
program, I find that the member variables don't get re-initialized when
"reconstructed". I don't have that problem in this demo, but the second
time the constructor is called, its "this" points to a different location.
My questions:
1. Why is the pointer 0xbffffad8 in the third output line, not 0x00300460
like the others?
2. Is there anything wrong with "reconstructing" an object as I have done
in "main"?
Thanks very much to anyone who can help!
Jerry Krinock
class Foo
{
int fooey ;
public:
Foo(int f)
{
fooey = f ;
cout << "In constructor, Addr=" << this << endl ;
}
int GetFooey() { return fooey ; }
} ;
int main ()
{
Foo* aFoo = new Foo(22) ;
cout <<"1. Addr=" <<aFoo <<", fooey=" <<(*aFoo).GetFooey() <<endl ;
// Now, "reconstruct" *aFoo with a different value for fooey
*aFoo = Foo(33) ;
cout <<"2. Addr=" <<aFoo <<", fooey=" <<(*aFoo).GetFooey() <<endl ;
}
****** Output: **********
In constructor, Addr=0x00300460
1. Addr=0x00300460, fooey=22
In constructor, Addr=0xbffffad8
2. Addr=0x00300460, fooey=33