CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [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?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    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

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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! :)

  5. #5
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  6. #6
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Can you call a constructor twice

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

  8. #8
    Join Date
    Dec 2003
    Location
    Middletown, DE
    Posts
    67

    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.

  9. #9
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  10. #10
    Join Date
    Sep 2004
    Posts
    156

    Re: Can you call a constructor twice

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

  11. #11
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

  13. #13
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Can you call a constructor twice

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

  14. #14
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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"...
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  15. #15
    Join Date
    Dec 2003
    Location
    Middletown, DE
    Posts
    67

    Re: [RESOLVED] Can you call a constructor twice

    Could you explain this bit?
    Code:
    CFoo foo;
    new(&foo)CFoo; // constructor called twice

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured