reusing an object

G

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();
 
J

Jacob Jensen

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
 
R

Rolf Magnus

Gil said:
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();
 
J

Jacob Jensen

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
 
B

Buster

Jacob said:
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.
 
R

Rolf Magnus

Jacob said:
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>
 
D

David Harmon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top