Re: reusing an object

From: Jacob Jensen (jacob_news_dkNoSpaaaammmm_at_yahoo.co.uk)
Date: 03/04/04


Date: Thu, 4 Mar 2004 13:05:52 +0100

I would create a function SetDefault() in SomeObject that sets all the
values to their defaults.
You cannot do

> // I would now like a clean so1
> // can I do this?
>
> so1 = new SomeObject();

o you cannot do this, since new SomeObject() creates a new object in memory
and returns a pointer to it, and so1 is not a pointer to a SomeObject.
You could do what you want to do by declaring pointers to SomObject as such

SomeObject* pso1;

pso1 = new SomeObject(); // memory is allocated dynamically and is pointed
at by pso1
pso1->setvalue1(1);
pso1->setvalue2(1);

// If you want a new "clean" object, simply delete the one created, and
create a new one
delete pso1; // If this is omitted, you would have garbage = memory not
pointed to by anyone
pso1 = new SomeObject(); // a new object is created in memory and is pointed
at by pso1



Relevant Pages

  • Re: another COM-Question: how to inject a type-lib into a dll?
    ... > But let me ask just another question on this: If this pointer is ... This is why the object exposes the COM interface in the first ... Make sure SomeObject has a reference count of 1 at this point. ... The declaraion of SomeOtherCOMMethod has a different signature. ...
    (microsoft.public.vc.atl)
  • Re: Whats going onto the stack here?
    ... SOMEOBJECT pointers. ... than a container which contains a pointer to SOMEOBJECT plus the linkers. ... So there's no extra indirection, the compiler just computes the appropriate ...
    (microsoft.public.vc.language)