class to sub class transformation

Hi everybody,

I have just started programming in C++ because a simulation environment I'm working with requires it. I am experienced in OO programming but have run into a problem nonetheless to which I don't have an answer. Any help is greatly appreciated.

What I want to do is the following: I have a class Node with two subclasses ActiveNode and PassiveNode. I want all object to start out as Nodes and then at some point determine (through a method in Node) whether to become an ActiveNode or PassiveNode (and be able to use the methods specific to this class). My question is:
Can an object of a class be transformed into an object of one of its subclasses through a method within the parent class?

thanks in advance, if I need to clarify any of this please reply and I will do so
[828 byte] By [dabikkel] at [2007-11-18 17:37:51]
# 1 Re: class to sub class transformation
I think you will need to make a new instance of ActiveNode or PassiveNode.

You will probably need to create a constructor that takes a Node (baseclass) as a parameter and copy the relevant information.
TheRogue at 2007-11-9 0:30:52 >
# 2 Re: class to sub class transformation
This sounds like a job for the State pattern:

class NodeState;

class Node
{
public:
enum State { Active, Passive };
void ChangeState(State state);
Node();

private:
NodeState* state_;
};

class NodeState
{
// whatever
};

class NodeStateNone : public NodeState
{
// etc
};

class NodeStateActive : public NodeState
{
// etc
};

class NodeStatePassive : public NodeState
{
// etc
};

Node::Node() : state_(new NodeStateNone)
{
}

void Node::ChangeState(State state)
{
NodeState* ns;
switch (state)
{
case Active:
ns = new NodeStateActive;
break;
case Passive:
ns = new NodeStatePassive;
break;
}
delete state_;
state_ = ns;
}

Of course you can always use the existing state_ object to initialise the new one if necessary
Graham at 2007-11-9 0:31:54 >
# 3 Re: class to sub class transformation
first of all guys, thanks for the quick input, it's really apreciated!

Graham, your "state pattern" is a very nice implementation and indeed seems like the way to go.
I've spent most of my time thinking of a way to change the "this" pointer. I did come up with something eventually. There is a compiler command line option (-fthis-is-variable) that allows the changing of "this" for backward compatibility reasons.
However I had already discarded this option because it's not in line with the current C++ practice. So thanks again for your solution. Just one more question: I understand your code but would like to dig a little deeper into this state pattern approach. Would you happen to have a url or name of a book that covers this subject?

thanks again
dabikkel at 2007-11-9 0:32:52 >
# 4 Re: class to sub class transformation
Description of State pattern ( http://home.earthlink.net/~huston2/dp/state.html)

More patterns ( http://home.earthlink.net/~huston2/dp/patterns.html)

The indispensible book ( http://www.amazon.com/exec/obidos/tg/detail/-/0201633612/102-5560786-5546552?v=glance)
Graham at 2007-11-9 0:33:51 >
# 5 Re: class to sub class transformation
just what I was looking for!

thanks a million!
dabikkel at 2007-11-9 0:34:52 >