Re: "Reconstructing" (Calling the constructor again)

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 02/11/05


Date: Fri, 11 Feb 2005 05:42:48 GMT


"Jerry Krinock" <jerry@ieee.org> wrote in message
news:BE317FA5.9DB0%jerry@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



Relevant Pages

  • Re: refrence from return value ?
    ... >>What would be the diffrence in calling it, and assigning ... > object will be dead after the assignement, and so the reference show to ... In this particular example the temporary object bound to the reference ... or constructor, the temporary object will only exist during the call. ...
    (comp.lang.cpp)
  • Re: Copy constructor and exception handling
    ... else if commenting off the copy constructor of class B, ... Is it a bug of Visual C++ compiler? ... "If the use of the temporary object can be eliminated without changing the ... constructor used to initialize the temporary copy is not accessible, ...
    (microsoft.public.vc.language)
  • Re: Teach myself C++.
    ... No doubt it will also be in the forthcoming C++0x standard, ... When an implementation introduces a temporary object of a class that has a non-trivial constructor, it shall ensure that a constructor is called for the temporary object. ... the destructor shall be called for a temporary with a non-trivial destructor. ...
    (sci.crypt)
  • Re: defining classes-- different methods
    ... reference to the function assigned to its - constructor - property, ... which is not true of an object assigned to the prototype property of a ... prototype Thus it is wasteful of resources to be assigning function ... not employing closures to emulate private members. ...
    (comp.lang.javascript)
  • Re: How to make an object instance available to all members of a page?
    ... Assigning default value is a logical exception from that rule. ... Contract StockContract = new Contract; ... into play because of the Dictionary/Sections properties. ... If you move it to constructor then default value will become null... ...
    (microsoft.public.dotnet.framework.aspnet)

Loading