473,289 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

reusing an object

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
6 2589
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
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jo Voordeckers | last post by:
Hello all, I'm pretty new to the Java newsgroups so I apologize for dropping this into several maybe offtopic groups. I'm sorry! So on to my problem... I've come to a point in our RMI...
6
by: Ivan Voras | last post by:
Can this be done: (this example doesn't work) ---- class A: def a_lengthy_method(self, params): # do some work depending only on data in self and params class B:
3
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm...
9
by: Alan | last post by:
Using VC++ (1998) compiler with PFE32 editor in Win2K Pro SP4. (DigitalMars CD on order. ) The program (below) instantiates a class and then deletes it. I would have thought that reusing the...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
4
by: Old Wolf | last post by:
#include <stdio.h> #include <stdarg.h> Is this safe: void foo(const char *fmt, ...) { va_list ap; va_start(ap,fmt);
4
by: Tedb | last post by:
Is there any reason why you can't reuse a delegate object instance versus creating a new one each time? For example in the following scenario I have a DataPoints object with an array of DataPoint...
12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
1
by: vbDavidC | last post by:
Hi, I am fairly new to .net and objects. I learned to create a reader object in method 1, however if I wanted to create multiple select queries in the same module I did not know how to reuse...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.