-
- Forum
- Visual C++ & C++ Programming
- C++ (Non Visual C++ Bug)
- [RESOLVED] Can y'all call a constructor twice
Thread: [RESOLVED] Can you call a constructor twice -
February eighth, 2007,08:38 AM #1 [RESOLVED] Can you call a constructor twice Tin you telephone call a constructor a 2nd time to restore a course's data members to their default values? -
February 8th, 2007,08:43 AM #2 Re: Tin you phone call a constructor twice Why dont yous just write a function that initializes the data members. Then you tin call this part from your constructor and at whatsoever other time! Regards, Laitinen -
February 8th, 2007,08:44 AM #3 Re: Tin can you phone call a constructor twice No. Create an Initialize() member function. Call it from within the constructor and whenever you lot want to set up the class members to their default value. Edit : Laitinen was faster (once again). -
February 8th, 2007,08:44 AM #4 Re: Tin yous call a constructor twice I am non sure I understand how are you going to do information technology... The obvious was to restore default values is to implement a public method in the class and motion all "default" assignment from ctor to this method, and then you could call this method from ctor and from whatever other place equally well. Last edited by VictorN; February eighth, 2007 at 08:45 AM. Reason: ****! I was as well late again! :) -
Feb eighth, 2007,08:l AM #v Re: Can you lot call a constructor twice What the hell... y'all only said that I tin can't call a constructor twice. The purpose of a constructor in my case is to initialise some variables. Only if I did what you said and called an Initialise() member part from within a constructor, that's calling a constructor twice.... call back almost it! *confused* I use the constructor to initialise variables, have another office that changes and messes around with those variables, then I demand something to restore them back to their default. If I called a member function from inside a constructor later I've left the constructor, I'chiliad effectively calling the constructor twice? -
Feb 8th, 2007,08:56 AM #6 Re: Tin can you lot telephone call a constructor twice Hello Mybowlcut! I recommend you lot to read these faqs C++ Constructors Best regards, Laitinen -
February 8th, 2007,08:57 AM #7 Re: Tin y'all telephone call a constructor twice Originally Posted by Mybowlcut If I called a member part from within a constructor after I've left the constructor, I'm finer calling the constructor twice? How is it called twice ? ... the constructor is only called in one case, so Initialize(0 would only exist called once from within the constructor. Edit: OK , possibly I meet what you are maxim. Aye there tin 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 annihilation you desire, including initialize. I call up the advise already posted here should adjust your needs. -
February 8th, 2007,09:03 AM #9 Re: Tin can you telephone call a constructor twice No... no no no... why would I phone call a role to initiliase variables when I'm in a constructor already? create class > constructor gets called, initiliases data members and eventually finishes > call course's function from chief to mess around with information technology'south data members > telephone call constructor to intialise values back to their default ^^ for that can I call a constructor twice (as in once automatically and one time manually)? Or exercise I have to create a fellow member office 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 lot have a create a fellow 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 empathize that. Thanks to anybody 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'southward data members > telephone call constructor to intialise values back to their default ^^ for that can I telephone call a constructor twice (as in once automatically and once manually)? Or do I accept to create a fellow member role 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: Tin you call a constructor twice Originally Posted by Mybowlcut for that tin I phone call a constructor twice (as in once automatically and once manually)? Or practise I have to create a fellow member part completely identical to a constructor to initialise values once i've left the constructor? No need to create function IDENTICAL to constructor, merely make your constructor to phone 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 Thanks EDIT: Doh. Late again Final edited by Hobson; Feb 8th, 2007 at 09:59 AM. B+! 'At that place is no cat' - A. Einstein Use [code] [/code] tags! Did Y'all 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: Yep, you can (although you may not). Try this petty 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 render 0; } Of course, the previous answers are good. I have posted that simply as an aside "marvel"... -
February 8th, 2007,08:25 PM #fifteen Re: [RESOLVED] Can y'all telephone call a constructor twice Could you lot explain this bit? Lawmaking: CFoo foo; new(&foo)CFoo; // constructor called twice Posting Permissions - Yous may not mail service new threads
- You may non post replies
- Yous may non post attachments
- You lot may not edit your posts
- BB code is On
- Smilies are On
- [IMG] code is On
- [VIDEO] lawmaking is On
- HTML code is Off
Forum Rules | Click Here to Aggrandize Forum to Full Width |
0 Response to "How to Call the Constructor of Class Again"
Post a Comment