473,396 Members | 1,917 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,396 software developers and data experts.

"Reconstructing" (Calling the constructor again)

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

Jul 23 '05 #1
4 7240
Jerry Krinock wrote:
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"?


1. Because you are construction a new instance of Foo. Obviously it must
have another address that the first Foo object. Furthermore you are not
"reconstructing" aFoo. Your code simply makes a shallow copy of Foo(33),
that is all values of Foo(33)'s members are assigned to their
corresponding members in Foo(22) (to which aFoo points).

2. Not in general, but instead of reconstruction use methods to alter
the object state (i.e., a method to set the value of fooey, perhaps with
some sanity checks on the value to set).

--
Regards,

Karsten
Jul 23 '05 #2

"Jerry Krinock" <je***@ieee.org> wrote in message
news:BE317FA5.9DB0%je***@ieee.org...
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.
No it doesn't. It assigns the value of another object of
the same type to it. A constructor can only be called once
for a given object, when it is created.
In my larger
program, I find that the member variables don't get re-initialized when
"reconstructed".
C++ doesn't have a concept of 'reinitialize'. An object
can be intialized only once, when it is created.
I don't have that problem in this demo, but the second
time the constructor is called,
The second constructor call is creating a different object.
(See below).
its "this" points to a different location.
Yes, because it's a different object.

My questions:

1. Why is the pointer 0xbffffad8 in the third output line, not 0x00300460
like the others?
Because those are addresses of two separate objects.
2. Is there anything wrong with "reconstructing" an object as I have done
in "main"?
You have not 'reconstructed' anything. You've created two
separate objects, and assigned the value of one to the other.

More below.
Thanks very much to anyone who can help!

Jerry Krinock
#include <iostream>
#include <ostream>
using std::cout;
using std::endl;

class Foo
{
int fooey ;
public:
Foo(int f)
Foo(int f) : fooey(f) /* this initalizes 'fooey' */ {
fooey = f ; /* this is not initialization, it's assignment */
cout << "In constructor, Addr=" << this << endl ;
}

int GetFooey() { return fooey ; }
int GetFooey() const { return fooey; }
} ;
int main ()
{
Foo* aFoo = new Foo(22) ;
/* This dynamically allocates a type 'Foo' object, causing
the constructor 'Foo::Foo(int)' to be called. The
address of this object is stored in the pointer object
'aFoo'. */
cout <<"1. Addr=" <<aFoo <<", fooey=" <<(*aFoo).GetFooey() <<endl ;
This outputs the address of the dynamicaly allocated object,
and the value of its data member 'fooey'. Note that there
is a 'shorthand' syntax for accessing members through a
pointer: aFoo->GetFooey() (but the way you wrote it is
valid as well).
// Now, "reconstruct" *aFoo with a different value for fooey
*aFoo = Foo(33) ;
You're *assigning* '*aFoo' a new value. Initialization
and assignment are not the same thing.

The above line creates a temporary type 'Foo' object, causing
the constructor 'Foo::Foo(int)' to be called. (This
initializes the temporary object, it does not affect
the one previously created.) Then the value of this
temporary object is *assigned* to the previously
created object (the one pointed to by 'aFoo'). Then
the temporary object is destroyed.
cout <<"2. Addr=" <<aFoo <<", fooey=" <<(*aFoo).GetFooey() <<endl ;
This output statement is the same as the one above, So again,
it outputs the address of the dynamically allocated object,
and the value of its data member 'fooey'. The object's address
does not change, it still exists at the location where it
was allocated. The only difference in the output should
be the value of 'fooey', 33 (since you assigned your object
a new value above).
}

****** Output: **********

In constructor, Addr=0x00300460
1. Addr=0x00300460, fooey=22
0x00300460 is the address of the dynamically allocated
object.
In constructor, Addr=0xbffffad8
0xbffffad8 is the address of the temporary object
(the one created with a constructor argument of 33)
2. Addr=0x00300460, fooey=33


0x00300460 is the address of the (same) dynamically allocated
object. So of course it is the same.
Which C++ book(s) are you reading? Perhaps you need
better ones.

-Mike
Jul 23 '05 #3
in article cu*************@news.t-online.com, Karsten Baumgarten at
in*****@spam.net wrote on 05/02/10 21:38:
instead of reconstruction use methods to alter the object state
Yes, I understand that the normal practice is to use a setter.

in article sB*****************@newsread3.news.pas.earthlink.n et, Mike Wahler
at mk******@mkwahler.net wrote on 05/02/10 21:42:
Which C++ book(s) are you reading? Perhaps you need better ones.


Unfortunately, a few years ago I bought a book which I later learned is
called "bullschildt" at a local store. Tell me the title of a better one
and I'll subscribe to it on O'Reilly Safari.

Thanks for the help. I understand the problems and can take it from here.

Jerry

Jul 23 '05 #4
"Jerry Krinock" <je***@ieee.org> wrote in message
news:BE31F606.A14D%je***@ieee.org...
in article sB*****************@newsread3.news.pas.earthlink.n et, Mike Wahler at mk******@mkwahler.net wrote on 05/02/10 21:42:
Which C++ book(s) are you reading? Perhaps you need better ones.


Unfortunately, a few years ago I bought a book which I later learned is
called "bullschildt" at a local store. Tell me the title of a better one
and I'll subscribe to it on O'Reilly Safari.


Which book is 'best' will vary among individuals and
their circumstances (e.g. complete beginner; have
experience with other language; etc.) Here are
some good peer reviews:

http://www.accu.org/bookreviews/public/index.htm

Also imo it's good to have more than a single
book. I suggest you choose two or more.

-Mike
Jul 23 '05 #5

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

Similar topics

12
by: spibou | last post by:
Why is a pointer allowed to point to one position past the end of an array but not to one position before the beginning of an array ? Is there any reason why the former is more useful than the...
8
by: Rasmus Kromann-Larsen | last post by:
The With Conundrum I'm currently writing a master thesis on (preparations for) static analysis of JavaScript, and after investigating the with statement, it only even more evident to me that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.