473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

can I turn base object into inherited one?

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
Jun 27 '08 #1
5 1478
alexl wrote:
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.

--
Ian Collins.
Jun 27 '08 #2
morph! that's exactly what I want to do ;-)

On Apr 13, 8:15*pm, Ian Collins <ian-n...@hotmail.comwrote:
>
No, a Base is a Base. *You can't morph it into something else.

--
Ian Collins.
Jun 27 '08 #3
alexl wrote:
morph! that's exactly what I want to do ;-)

On Apr 13, 8:15 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>
No, a Base is a Base. You can't morph it into something else.
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.
--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #4
alexl <al**********@gmail.comwrites:
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
*/
--
__Pascal Bourguignon__
Jun 27 '08 #5
On Apr 14, 2:15 am, Ian Collins <ian-n...@hotmail.comwrote:
alexl wrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
2
by: Josh Mcfarlane | last post by:
I'm doing recomposition of objects from binary streams, and the best way for me to write them out is to write base class data first, forward to inherited classes, pointer class values, etc. Now,...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
2
by: bg_ie | last post by:
Hi, Lets say I have an class called Base which is inherited by a class called Top. Now lets say I want to assign an object of type Base to an object of type Top, how can I do this so that all...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
12
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.