How to Call the Constructor of Class Again

Thread: [RESOLVED] Can you call a constructor twice

  1. #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?

  2. #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


  3. #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).


  4. #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! :)

  5. #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?

  6. #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


  7. #7

    Re: Tin y'all telephone call a constructor twice

    Quote 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.

  8. #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.


  9. #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?


  10. #10

    Re: Can you call a constructor twice

    Yes, you lot have a create a fellow member function to initialize for the second time

  11. #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.

  12. #12

    Re: Can you call a constructor twice

    Quote 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
    }


  13. #13

    Re: Tin you call a constructor twice

    Quote 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 ?


  14. #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"...

  15. #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

Click Here to Aggrandize Forum to Full Width



freunderated.blogspot.com

Source: https://forums.codeguru.com/showthread.php?414189-RESOLVED-Can-you-call-a-constructor-twice

0 Response to "How to Call the Constructor of Class Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel