 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gil Guest
|
Posted: Thu Mar 04, 2004 11:41 am Post subject: reusing an object |
|
|
Hi,
I created an object set private variables in the object. I want to
reuse the object but with default values. How can I do this?
SomeObject so1;
so1.setValue1(1);
so1.setValue2(1);
// I would now like a clean so1
// can I do this?
so1 = new SomeObject();
|
|
| Back to top |
|
 |
Jacob Jensen Guest
|
Posted: Thu Mar 04, 2004 12:05 pm Post subject: Re: reusing an object |
|
|
I would create a function SetDefault() in SomeObject that sets all the
values to their defaults.
You cannot do
| Quote: | // 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
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Mar 04, 2004 12:11 pm Post subject: Re: reusing an object |
|
|
Gil wrote:
| Quote: | Hi,
I created an object set private variables in the object. I want to
reuse the object but with default values. How can I do this?
SomeObject so1;
so1.setValue1(1);
so1.setValue2(1);
// I would now like a clean so1
// can I do this?
so1 = new SomeObject();
|
Try:
so1 = SomeObject();
|
|
| Back to top |
|
 |
Jacob Jensen Guest
|
Posted: Thu Mar 04, 2004 12:46 pm Post subject: Re: reusing an object |
|
|
| Quote: | Try:
so1 = SomeObject();
|
Does this not require implementing a copy constructor, because if the
default is not good enough, you might not get all the data reinitialized. Or
have got something wrong here?
Jacob
|
|
| Back to top |
|
 |
Buster Guest
|
Posted: Thu Mar 04, 2004 1:11 pm Post subject: Re: reusing an object |
|
|
Jacob Jensen wrote:
| Quote: | Try:
so1 = SomeObject();
Does this not require implementing a copy constructor, because if the
default is not good enough, you might not get all the data reinitialized. Or
have got something wrong here?
|
It requires an assignment operator (implicit or explicit). What the
operator should do is a separate issue. You are right in saying "if the
default is not good enough, you might not get all the data
reinitialized". It depends on your class.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Mar 04, 2004 1:20 pm Post subject: Re: reusing an object |
|
|
Jacob Jensen wrote:
| Quote: | Try:
so1 = SomeObject();
Does this not require implementing a copy constructor, because if the
default is not good enough, you might not get all the data
reinitialized. Or have got something wrong here?
|
The above doesn't use the copy constructor, but the assignment operator.
Often, the default for both copy constructor and assignment operator is
enough. But if it's not for your class, you should implement them
anyway since a class that you are not allowed to copy even though its
interface claims it be copyable is broken. Alternatively, if the class
is not supposed to be copied, the copy constructor and assignment
operator should be declared private and not implemented. In this case,
the above solution will of course not work. Another alternative (that
has its own problems though and is not really a good design, so I
advice against it) is to destroy and reconstruct the object in-place by
doing:
so1.~SomeObject();
new (&so1) SomeObject(); // #include <new>
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Thu Mar 04, 2004 4:19 pm Post subject: Re: reusing an object |
|
|
On 4 Mar 2004 03:41:58 -0800 in comp.lang.c++,
[email]brightoceanlight (AT) hotmail (DOT) com[/email] (Gil) was alleged to have written:
| Quote: | I created an object set private variables in the object. I want to
reuse the object but with default values. How can I do this?
|
I suspect that what you actually want is to put the declaration of the
variable inside your loop, so that the object is "automatically" created
freshly for each iteration.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|