-
February 8th, 2007, 08:38 AM
#1
[RESOLVED] Can you call a constructor twice
Can you call a constructor a second time to restore a class's data members to their default values?
-
February 8th, 2007, 08:43 AM
#2
Re: Can you call a constructor twice
Why dont you just write a function that initializes the data members. Then you can call this function from your constructor and at any other time!
Regards,
Laitinen
-
February 8th, 2007, 08:44 AM
#3
Re: Can you call a constructor twice
No.
Create an Initialize() member function. Call it from within the
constructor and whenever you want to set the class members
to their default value.
Edit : Laitinen was faster (again).
-
February 8th, 2007, 08:44 AM
#4
Re: Can you call a constructor twice
I am not sure I understand how are you going to do it...
The obvious was to restore default values is to implement a public method in the class and move all "default" assignment from ctor to this method, then you could call this method from ctor and from any other place as well.
Last edited by VictorN; February 8th, 2007 at 08:45 AM.
Reason: ****! I was too late again! :)
-
February 8th, 2007, 08:50 AM
#5
Re: Can you call a constructor twice
What the hell... you just said that I can't call a constructor twice. The purpose of a constructor in my case is to initialise some variables. But if I did what you said and called an Initialise() member function from within a constructor, that's calling a constructor twice.... think about it! *confused* I use the constructor to initialise variables, have another function that changes and messes around with those variables, then I need something to restore them back to their default. If I called a member function from within a constructor after I've left the constructor, I'm effectively calling the constructor twice?
-
February 8th, 2007, 08:56 AM
#6
Re: Can you call a constructor twice
Hi Mybowlcut!
I recommend you to read these faqs C++ Constructors
Best regards,
Laitinen
-
February 8th, 2007, 08:57 AM
#7
Re: Can you call a constructor twice
Originally Posted by Mybowlcut
If I called a member function from within a constructor after I've left the constructor, I'm effectively calling the constructor twice?
How is it called twice ? ... the constructor is only called once, so Initialize(0
would only be called once from within the constructor.
Edit: OK , maybe I see what you are saying. Yes there can be an
efficiency in using assignment versus initialization lists.
Last edited by Philip Nicoletti; February 8th, 2007 at 09:06 AM.
-
February 8th, 2007, 08:58 AM
#8
Re: Can you call a constructor twice
constructor != initializer
The constructor can do anything you want, including initialize. I think the advise already posted here should suit your needs.
-
February 8th, 2007, 09:03 AM
#9
Re: Can you call a constructor twice
No... no no no... why would I call a function to initiliase variables when I'm in a constructor already?
create class > constructor gets called, initiliases data members and eventually finishes > call class's function from main to mess around with it's data members > call constructor to intialise values back to their default
^^ for that can I call a constructor twice (as in once automatically and once manually)? Or do I have to create a member function completely identical to a constructor to initialise values once i've left the constructor?
-
February 8th, 2007, 09:11 AM
#10
Re: Can you call a constructor twice
Yes, you have a create a member function to initialize for the second time
-
February 8th, 2007, 09:19 AM
#11
Re: Can you call a constructor twice
Thankyou, I can understand that. Thanks to everyone else who tried to explain it to me as well.
-
February 8th, 2007, 09:38 AM
#12
Re: Can you call a constructor twice
Originally Posted by Mybowlcut
No... no no no... why would I call a function to initiliase variables when I'm in a constructor already?
create class > constructor gets called, initiliases data members and eventually finishes > call class's function from main to mess around with it's data members > call constructor to intialise values back to their default
^^ for that can I call a constructor twice (as in once automatically and once manually)? Or do I have to create a member function completely identical to a constructor to initialise values once i've left the constructor?
Completely identical - NO.
MYObject::MyObject()
{
Initialize();
}
MyOBject::Initialize()
{
//Variable assignments here
}
-
February 8th, 2007, 09:56 AM
#13
Re: Can you call a constructor twice
Originally Posted by Mybowlcut
for that can I call a constructor twice (as in once automatically and once manually)? Or do I have to create a member function completely identical to a constructor to initialise values once i've left the constructor?
No need to create function IDENTICAL to constructor, just make your constructor to call initializer:
Code:
class TestClass
{
private:
int a;
float b;
public:
TestClass()
{
init();
}
void init()
{
a = 42;
b = 1.0f / 13;
}
... getters/setters/messers
};
.... somewhere in your code:
TestClass obj; //contains initialized values
MessWithMyObject(obj); //change values
obj.init(); //restore values
Cheers
EDIT: Doh. Late again
Last edited by Hobson; February 8th, 2007 at 09:59 AM.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 8th, 2007, 06:48 PM
#14
Re: [RESOLVED] Can you call a constructor twice
[ Redirected thread ]
Q: Can I call a constructor twice?
A: Yes, you can (although you may not).
Try this little program
Code:
#include <iostream>
using namespace std;
class CFoo
{
public:
CFoo() {cout << "CFoo::CFoo - this is: " << this << endl;}
void* operator new(size_t, void* p) {return p;}
};
int main()
{
CFoo foo;
new(&foo)CFoo; // constructor called twice
return 0;
}
Of course, the previous answers are good. I have posted that just as an aside "curiosity"...
-
February 8th, 2007, 08:25 PM
#15
Re: [RESOLVED] Can you call a constructor twice
Could you explain this bit?
Code:
CFoo foo;
new(&foo)CFoo; // constructor called twice
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|