Thread: Reinitialize constructor in class

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    Reinitialize constructor in class

    I have code in the constructor in my class. Yet when I want the game Im doing restart I would like to reset the class.
    in C# it would be CMyClass class;
    class = new CMyClass();

    What is the way to do this in c++

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Make a function which reinitializes all of the variables in your class.
    2) Destroy the instance of the class and make a new one. (Assuming you've allocated it dynamically.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Try creating a copy constructor and/or assignment operator, and then doing:
    Code:
    MyClass obj(whatever arguments you pass to constructor);
    obj = MyClass(whatever arguments you pass to constructor the second time around);
    Probably not the most efficient, since it involves constructing a new object and then copying it into obj. More efficient would be, as quzah said, just make a function that reinitializes everything outside of the constructor.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    Or you could have a member function reset() in your CMyClass which resets the class as you want.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anykey
    Or you could have a member function reset() in your CMyClass which resets the class as you want.
    I guess you didn't read the 1) in my reply.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    I guess you didn't read the 1) in my reply.
    Oh yes, I missed that.
    Great minds think alike, huh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM