473,398 Members | 2,335 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,398 software developers and data experts.

Switching obect's type--sort of.

I was wondering if there was any way to do the following:

If I have a class Base, and multiple Derived classes which all have
some different behavior on some function foo()...is there any good way
to have an object that can switch between these various
functionalities..using a pointer to the generic class?

as follows:

class Status
{
Status(){}

Base *mode;
Derived1 derived1;
Derived2 derived2;
};

class Base
{
virtual foo();
};

class Derived1 : public Base
{
foo(){cout<<"One";}
};

class Derived2 : public Base
{
foo(){cout<<"Two";}
};

Such that I can change *mode so that I can call mode->foo(), and can
expect to be able to choose whether mode->foo() is calling Derived1's
foo, or Derived2's foo. Is there any way to create such behavior?

Thanks!
Jul 22 '05 #1
7 1971


Joseph Cook wrote:

I was wondering if there was any way to do the following:

If I have a class Base, and multiple Derived classes which all have
some different behavior on some function foo()...is there any good way
to have an object that can switch between these various
functionalities..using a pointer to the generic class?

as follows:

class Status
{
Status(){}

Base *mode;
Derived1 derived1;
Derived2 derived2;
};

class Base
{
virtual foo();
};

class Derived1 : public Base
{
foo(){cout<<"One";}
};

class Derived2 : public Base
{
foo(){cout<<"Two";}
};

Such that I can change *mode so that I can call mode->foo(), and can
expect to be able to choose whether mode->foo() is calling Derived1's
foo, or Derived2's foo. Is there any way to create such behavior?


Not sure I understand what you want, but

void Status::bar()
{
mode = &derived1;
mode->foo(); // calls Derived1::foo

mode = &derived2;
mode->foo(); // calls Derived2::foo
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Quoting Joseph Cook <jo******@mail.com>:
| Such that I can change *mode so that I can call mode->foo(), and can
| expect to be able to choose whether mode->foo() is calling Derived1's
| foo, or Derived2's foo. Is there any way to create such behavior?

I'm a bit baffled, since you almost have it. Do you fully understand the
notion of virtual functions? Briefly, virtual functions enable _old_ code to
call _new_ code. This is all in the FAQ, in the section on inheritance and
virtuals:

http://www.parashift.com/c++-faq-lite/

In Status, just skip the derived1 and derived2 members:

class Status {
Base* mode;
public:
Status() : mode(0) {
}

void switchmode(Base* m) {
mode = m;
}

void dosomething() {
if ( mode ) mode->foo();
}
};

Then you could use your code like this:

Status s;
Derived1 d1;
Derived2 d2;
s.switchmode(d1);
s.dosomething(); // calls d1::foo()
s.switchmode(d2);
s.dosomething(); // calls d2::foo()

--
Christian Stigen Larsen -- http://csl.sublevel3.org
Jul 22 '05 #3

"Joseph Cook" <jo******@mail.com> wrote in message
news:b6**************************@posting.google.c om...
I was wondering if there was any way to do the following:

If I have a class Base, and multiple Derived classes which all have
some different behavior on some function foo()...is there any good way
to have an object that can switch between these various
functionalities..using a pointer to the generic class?

as follows:

class Status
{
Status(){}

Base *mode;
Derived1 derived1;
Derived2 derived2;
};

class Base
{
virtual foo();
};

class Derived1 : public Base
{
foo(){cout<<"One";}
};

class Derived2 : public Base
{
foo(){cout<<"Two";}
};

Such that I can change *mode so that I can call mode->foo(), and can
expect to be able to choose whether mode->foo() is calling Derived1's
foo, or Derived2's foo. Is there any way to create such behavior?

Thanks!


I guess what you're looking for is not the object's type but the
implementation. In principle this problem is tackled by the "Strategy Design
Pattern" of which Karl Heinz has given you a basic example. I'd recommend to
check out any decent Design Pattern book or try my friend google.

HTH
Chris
Jul 22 '05 #4


Chris Theis wrote:


I guess what you're looking for is not the object's type but the
implementation. In principle this problem is tackled by the "Strategy Design
Pattern" of which Karl Heinz has given you a basic example.


Honestly I don't understand what the OP is asking about.
It's all there in his example:
* a pointer to base
* virtual functions.
all that's missing is the assignment, but that seems to be so trivial
that I think we are barking up the wrong tree and the OP has a completely
different problem but didn't express himself clearly enough.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5


Karl Heinz Buchegger wrote:

Honestly I don't understand what the OP is asking about.
It's all there in his example:
* a pointer to base
* virtual functions.
all that's missing is the assignment, but that seems to be so trivial
that I think we are barking up the wrong tree and the OP has a completely
different problem but didn't express himself clearly enough.


I think he wants to call either of the derived class methods from base
irrespective of the actuall object type, but I could be wrong.

Jul 22 '05 #6

"lilburne" <li******@godzilla.com> wrote in message
news:bp*************@ID-179504.news.uni-berlin.de...


Karl Heinz Buchegger wrote:

Honestly I don't understand what the OP is asking about.
It's all there in his example:
* a pointer to base
* virtual functions.
all that's missing is the assignment, but that seems to be so trivial
that I think we are barking up the wrong tree and the OP has a completely different problem but didn't express himself clearly enough.


I think he wants to call either of the derived class methods from base
irrespective of the actuall object type, but I could be wrong.


I guess this really calls for some clarification of the OP!

Chris
Jul 22 '05 #7

"Joseph Cook" <jo******@mail.com> wrote in message
news:b6**************************@posting.google.c om...
I was wondering if there was any way to do the following:

If I have a class Base, and multiple Derived classes which all have
some different behavior on some function foo()...is there any good way
to have an object that can switch between these various
functionalities..using a pointer to the generic class?

as follows:

class Status
{
Status(){}

Base *mode;
Derived1 derived1;
Derived2 derived2;
};

class Base
{
virtual foo();
};

class Derived1 : public Base
{
foo(){cout<<"One";}
};

class Derived2 : public Base
{
foo(){cout<<"Two";}
};

Such that I can change *mode so that I can call mode->foo(), and can
expect to be able to choose whether mode->foo() is calling Derived1's
foo, or Derived2's foo. Is there any way to create such behavior?

Thanks!


Your example does not seem to match what you've stated that you want.

In your example, you have three separate objects: one is of the base type,
the others are of the other two derived types. In the example, you could
just call derived1->foo() or derived2->foo(), based on whatever condition
you chose. The mode pointer isn't even needed, unless you also want to call
the Base class' foo function (via mode->foo()).

But, from your comments, it seems like you don't want three objects, but
rather a single object that can act as any of the derived classes. If you
actualy want to change an existing Base object from behaving like a Derived1
object to behaving like a Derived2 object, that's not (generally speaking)
possible, because Derived1 and Derived2 are not related.

However, you can create an instance of a Derived1 class and assign it to a
pointer-to-Base class variable (a Base*), and it will then behave as a
Derived1 object. (but you'll want to be sure that the Base class destructor
and the foo() function are both virtual). Then you can delete that
instance, and create a new one based on Derived2 and it will behave as a
Derived2 object.

Like this:

Base* mode = new Derived1();
mode->foo(); // calls Derived1::foo
delete mode;
mode = new Derived2();
mode->foo(); // calls Derived2::foo
delete mode;

But you can't create an object of type Derived1 and "switch it" to behave
like a Derived2 object. There's simply no mechanism for that which is safe
and reliable. Casting is "possible", but it is not guaranteed to work. For
example:

Base* mode = new Base();
(Derived1*)mode->foo();
(Derived2*)mode->foo();

This may work. It may blow up horrendously. And, it may work for you but
fail for others! You just can't say for sure, because you have not created
instances of Derived1 and Derived2...you are just lying to the compiler and
telling it that you have!
-Howard


Jul 22 '05 #8

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

Similar topics

3
by: Dennis Wheeler | last post by:
I'm trying to find a commandline solution for switching projects. Currently I have to modify the IIS virtual directory path to the source files, and then open the solution file in .Net to be...
3
by: MLH | last post by:
I seem to remember a post in the distant past discussing how to switch to another open application window. IE, if WordPad is running, some API function that'll allow me to switch the focus to that...
2
by: Johann Blake | last post by:
The following is a bug I have discovered using tab pages and threads and I am looking for a workaround. Create a new Windows Forms application and add a tab control with two tab pages. Add a...
6
by: Nicky | last post by:
hi,all We are going to develop a program and when it is running, we need it full screen and also, user can not switch to other place before exit our program. I am thinking, we can make a window...
5
by: JRB | last post by:
Hi, I'm creating a small C#program that communicates through the serial port. I have a separate thread that continuously takes in data from an external device in a while loop. The problem is that...
4
by: sashan | last post by:
Hi Is there a way to enable Emacs/Vim style buffer switching in the VC++ editor? For those that don't know this is where you hit a shortcut key in Emacs/Vim and then type the partial name of the...
4
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired =...
2
by: Frank Swarbrick | last post by:
I had asked a question a few weeks ago about having problems at times accessing DB2 Express-C 9.1, and getting "SQL1032N No start database manager command was issued. SQLSTATE=57019" even when the...
1
by: Dave Rado | last post by:
Hi A while ago I discovered a way of creating css pseudo-frames, that offer users the important benefits of real frames (i.e. the navigation remains visible when you scroll down the page), but...
4
by: adlloyd | last post by:
Hi all, I've got an application that's written in C++ making use of MFC (VS6). Its purpose is to process SMS messages received from a GSM modem connected via a serial port (USB connection). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.