473,788 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1494
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.co mwrote:
>
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.co mwrote:
>>
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*******@rocke tmail.com
Jun 27 '08 #4
alexl <al**********@g mail.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 ChangeableObjec t;

class Base {
protected:
ChangeableObjec t* self;
public:
static Base* factory(Changea bleObject* mySelf){return new Base(mySelf);}
Base(Changeable Object* mySelf):self(my Self){}
virtual void doSomething(voi d){ cout<<"I am a base "<<self<<en dl; }
};
class ChangeableObjec t {
private:
Base* implementation;
public:
typedef Base*(*Factory) (ChangeableObje ct*);
ChangeableObjec t(){ changeClass(Bas e::factory); }
void changeClass(Fac tory factory){ implementation= factory(this); }
void doSomething(voi d){ implementation->doSomething( ); };
};

class Derived:public Base {
public:
static Base* factory(Changea bleObject* mySelf){return new Derived(mySelf) ;}
Derived(Changea bleObject* mySelf):Base(my Self){}
virtual void doSomething(voi d){ cout<<"I am a derived "<<self<<en dl; }
};

int main(void){
ChangeableObjec t* o=new ChangeableObjec t();
o->doSomething( );
o->changeClass(De rived::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.co mwrote:
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 objektorientier ter Datenverarbeitu ng
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
4345
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 inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
2
2159
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, when recomposing these objects, I first read the base class data, and can create a base object. When I find the inherited class marker, can I call have the inherited class use the prior base object to population the inherited class's base...
7
5760
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: Fruit lemon = new Fruit(); Product prod = (Product)lemon; // Now prod is a Fruit Type!! I want to get a Product reference from a Fruit object, is it possible? thank you.
5
3164
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
5
2008
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" Gpa="3.50"> <Person Id="1" FirstName="FirstName0" LastName="LastName0" MiddleInitial="W"/> </Student> In my code, person is the base class and student extends it. When I
7
2249
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
2
1485
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 the members present in the Base object are assigned to the corresponding members in the Top object, with the remaining values set to null? Thanks for your help,
19
2236
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 modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
12
2170
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
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.