By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

reusing an object

P: n/a
Gil
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();
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
Gil wrote:
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();

Jul 22 '05 #3

P: n/a
> 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
Jul 22 '05 #4

P: n/a
Jacob Jensen wrote:
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.
Jacob

Jul 22 '05 #5

P: n/a
Jacob Jensen wrote:
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>
Jul 22 '05 #6

P: n/a
On 4 Mar 2004 03:41:58 -0800 in comp.lang.c++,
br**************@hotmail.com (Gil) was alleged to have written:
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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.