Can you call a constructor a second time to restore a class's data members to their default values? :wave:
Printable View
Can you call a constructor a second time to restore a class's data members to their default values? :wave:
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
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).
I am not sure I understand how are you going to do it... :confused:
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.
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?
Hi Mybowlcut!
I recommend you to read these faqs C++ Constructors
Best regards,
Laitinen
How is it called twice ? ... the constructor is only called once, so Initialize(0Quote:
Originally Posted by Mybowlcut
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.
constructor != initializer
The constructor can do anything you want, including initialize. I think the advise already posted here should suit your needs.
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?
Yes, you have a create a member function to initialize for the second time
Thankyou, I can understand that. Thanks to everyone else who tried to explain it to me as well.
Quote:
Originally Posted by Mybowlcut
Completely identical - NO.
MYObject::MyObject()
{
Initialize();
}
MyOBject::Initialize()
{
//Variable assignments here
}
No need to create function IDENTICAL to constructor, just make your constructor to call initializer:Quote:
Originally Posted by Mybowlcut
CheersCode: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
EDIT: Doh. Late again :)
[ Redirected thread ]
Q: Can I call a constructor twice?
A: Yes, you can (although you may not).
Try this little program
Of course, the previous answers are good. I have posted that just as an aside "curiosity"... ;)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;
}
Could you explain this bit?
Code:CFoo foo;
new(&foo)CFoo; // constructor called twice