0

I have two classes. In the main class I have a process that is step by step and can be stepped though, I have implemented this as a state machine. To help keep the machine easy/friendly I created another class that holds information about the current state of the machine. In the main class I then added a reference to it as such

CAnimateMachine *m_AniMach;

After some actions happen I call a function in the main class to instantiate variables within my state machine object. Each call to this function AniInit() should basically "reset" the state machine by instantiating the variables to the "initial state".

My issue is I am not sure how to instantiate my m_AniMach properly. I am used to C# where I can just do

m_AniMach = new CAnimateMachine(); 

to "erase" the old object and instantiate a new one. Though, from what I have read, I can not be so cavalier in C++. What is the proper way to "re-instantiate" this variable in my init method?

Could I use the new operator m_AniMach = new CAnimateMachine() and then in the main class' deconstrutor do delete &m_AniMach?

EDIT: juanchopanza's answer makes sense to me. Though I keep getting an error when trying to compile. I am not exactly sure what the error is trying to tell me, I think it is telling me that my class isn't public when it is? I reviewed the C2248 MSDN article but I do not see how it relates to my situation.

error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
include\afx.h(562) : see declaration of 'CObject::operator ='
include\afx.h(532) : see declaration of 'CObject'
occurred in the compiler generated function 'CAnimateMachine &CAnimateMachine::operator =(const CAnimateMachine &)'

Here is my CAnimateMachine class

class CAnimateMachine
{
public:
    CAnimateMachine();

    int startX,startY;
};

And this is how I instantiate it

m_AniMach = CAnimateMachine();

And how it is defined

CAnimateMachine m_AniMach;
Community
  • 1
  • 1
AnotherUser
  • 1,311
  • 1
  • 14
  • 35
  • If you do have to use a pointer (for example, polymorphism) you can use `std::unique_ptr`. It will delete the old object when assigned a new one. – Neil Kirk Aug 12 '14 at 16:27
  • Don't use raw pointers in the first place - you'll cause yourself endless pain, especially coming from a language with GC. Ideally go with juanchopanza's answer, but even if you wanted to use pointers, you should use smart ones. Eg, `std::unique_ptr m_AniMach;` and `m_AniMach.reset(new CAnimateMachine);` [edit - beaten by @NeilKirk] – Useless Aug 12 '14 at 16:27
  • Forget what you know about using C#. C++ is a very different language, and is much better about helping you correctly manage the lifetime of all resources (not just memory). If you're using a CObject class and trying to use dynamically allocated objects in a lot of places, you're fighting against the language. Get a good book like _Programming -- Principles and Practice Using C++_ and understand the way C++ does it. – Rob K Aug 12 '14 at 18:18
  • I wish I had time to read a book on each programming language that I write in, but it just will never happen. – AnotherUser Aug 12 '14 at 19:05

1 Answers1

4

There seems to be no reason to use a pointer. You can use an object instead:

CAnimateMachine m_AniMach;

in which case it will get default initialized when an object of the type that holds it gets instantiated. To "re-initialize" it, you can say

m_AniMach = CAnimateMachine();

If you do this, you will not have to worry about things like following the rule of three or other dynamic allocation pitfalls.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 2
    this is also much safer than using a pointer, as the object will always be destructed at the end of scope, even if an exception is thrown. – Mgetz Aug 12 '14 at 16:20
  • @AnotherUser That means `CAnimateMachine` is not assignable, so you can't "re-initialize" as I have shown. You will need to find another way of doing that. Without knowing the details of that class, I can't really help. – juanchopanza Aug 12 '14 at 16:43
  • Hmm, might I need to write an overloaded `=` operator? // The entire class `CAnimateMAchine` is given above if that helps any? – AnotherUser Aug 12 '14 at 16:44
  • @AnotherUser That class definition doesn't match the error. Are you sure it doesn't inherit from, or contain a `CObject`? That is the type that is unassignable. – juanchopanza Aug 12 '14 at 16:49
  • Ah, it does contain something referring to `CObject`. I have two pointers in the class, both point to two other classes both derived from `CObject` // Sorry to sound needy/novice but what does this mean, that it holds a `Cobject` pointer, why does that make any difference? // What might I be able to do to fix this? – AnotherUser Aug 12 '14 at 16:58