can I turn base object into inherited one?

A

alexl

I have 2 classes,

class base {
public:
some virtual functions

}

class derived : public base {
public:
derived(base* b);

}

is there a simpler way to turn a base object into derived than the
following

base* b = new base();

b->do some stuff

now he becomes derived

base* d = new derived(b);


thx
 
I

Ian Collins

alexl said:
I have 2 classes,

class base {
public:
some virtual functions

}

class derived : public base {
public:
derived(base* b);

}

is there a simpler way to turn a base object into derived than the
following

base* b = new base();

b->do some stuff

now he becomes derived

base* d = new derived(b);
No, a Base is a Base. You can't morph it into something else.
 
J

Jim Langston

alexl said:
morph! that's exactly what I want to do ;-)

But you can't do it. b is an instance of base and only has information
about base, it knows nothing of derived.

base* d = new derived(b);
should be working because it's creating a new derived using the instance of
b to create the base portion of it.

Think of it this way, say base is animal, derived is cat. If you create an
instance of animal how can you create a cat out of it without giving it the
additional information? How to meow, etc.. Which is what you're doing by
creating a new cat.
 
P

Pascal J. Bourguignon

alexl said:
I have 2 classes,

class base {
public:
some virtual functions

}

class derived : public base {
public:
derived(base* b);

}

is there a simpler way to turn a base object into derived than the
following

base* b = new base();

b->do some stuff

now he becomes derived

base* d = new derived(b);

There is no simplier way, in C++.



( You are wanting CLOS (Common Lisp Object System), where an object can
( change of class on the run.
(
( (defclass base () ())
( (defmethod do-something ((self base)) (print `(I am a base ,self)))
( (defclass derived (base) ())
( (defmethod do-something ((self derived)) (print `(I am a derived ,self)))
(
( (let ((b (make-instance 'base)))
( (do-something b)
( (change-class b 'derived)
( (do-something b)
( (values))
(
( Which prints:
(
( (I AM A BASE #<BASE #x00033477AFB8>)
( (I AM A DERIVED #<DERIVED #x00033477AFB8>)
(
( Note that's the same object, at the same address.



But in C++, there's no simplier way than to explicitely define a
conversion method that will instanciate a new object. To solve the
identity problem, you have to implement a 'State' design pattern.
http://en.wikipedia.org/wiki/State_pattern

#include <iostream>
using namespace std;

class ChangeableObject;

class Base {
protected:
ChangeableObject* self;
public:
static Base* factory(ChangeableObject* mySelf){return new Base(mySelf);}
Base(ChangeableObject* mySelf):self(mySelf){}
virtual void doSomething(void){ cout<<"I am a base "<<self<<endl; }
};


class ChangeableObject {
private:
Base* implementation;
public:
typedef Base*(*Factory)(ChangeableObject*);
ChangeableObject(){ changeClass(Base::factory); }
void changeClass(Factory factory){ implementation=factory(this); }
void doSomething(void){ implementation->doSomething(); };
};



class Derived:public Base {
public:
static Base* factory(ChangeableObject* mySelf){return new Derived(mySelf);}
Derived(ChangeableObject* mySelf):Base(mySelf){}
virtual void doSomething(void){ cout<<"I am a derived "<<self<<endl; }
};

int main(void){
ChangeableObject* o=new ChangeableObject();
o->doSomething();
o->changeClass(Derived::factory);
o->doSomething();
return(0);
}

/*
-*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
Compilation started at Mon Apr 14 10:11:03

g++ -o state-pattern state-pattern.c++ && ./state-pattern
I am a base 0x603010
I am a derived 0x603010

Compilation finished at Mon Apr 14 10:11:03
*/
 
J

James Kanze

No, a Base is a Base. You can't morph it into something else.

You can simulate this sort of behavior, however, with a
variation of the letter envelop idiom. Typically, of course,
this idiom is only used to give polymorphic behavior to an
object with value semantics, but since value semantics include
assignment, with the target object acquiring the dynamic type of
the assigned object, the potential is there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top