473,387 Members | 1,864 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,387 software developers and data experts.

Dynamic object costuming

I have an advanced question that I hope there is a possible answer
for...

Say that I have a list of base objects and I wish to allow them to be
passed to various routines which may wish to temporarily give them
extra functionality akin to an actor putting on a costume and taking on
a role. The base class would need to provide storage for the costume,
and the derived classes would have specific costume methods.

While I imagine this could be done with virtual member functions for
controlling the "costume" in the derived classes, operating upon a
"void *" placeholder in the base "actor" class, I'm not sure if that is
the best or most desirable method.

Feb 1 '06 #1
5 1398
Derek wrote:
I have an advanced question that I hope there is a possible answer
for...
Ask away.
Say that I have a list of base objects and I wish to allow them to be
This already sounds bad. "base" implies polymorphic behaviour. List of
base implies slicing.
passed to various routines which may wish to temporarily give them
extra functionality akin to an actor putting on a costume and taking on
a role. The base class would need to provide storage for the costume,
and the derived classes would have specific costume methods.
I think I follow. The base stores data and the derived operate on it?
Thats not a good design choice.
While I imagine this could be done with virtual member functions for
controlling the "costume" in the derived classes, operating upon a
Only if your list is of pointers to base.
"void *" placeholder in the base "actor" class, I'm not sure if that is
the best or most desirable method.


void* is rarely a good idea.

I see no question.

What are you actually trying to achieve?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 1 '06 #2
Yes, the list would be a list of pointers to base objects.

I guess it would have been better to frame the question from a "user"
point of view than an "actor" point of view, but it's all the same.

You have a system where you have a variable number of "user" objects in
a list.

These users can access various aspects of the system... they could be
anything... email, chat, file management, etc.

When a User object accesses the email module, it would be nice if that
user could then become an EmailUser (a derived class of User), and have
extra functionality, but keep a pointer to any email specific data
pointed to by a generic data pointer (void * or maybe a base data
object) within the User class, but when acting as an EmailUser, the
EmailUser specific methods would reference the EmailData object.

I imagine this is a somewhat common requirement, but I'm not sure about
the best way to go about doing this in C++.

I'm currently doing this with a lot of ugly typecasting, but I am
certain there is a better way.

Feb 2 '06 #3
Derek wrote:
Yes, the list would be a list of pointers to base objects.

I guess it would have been better to frame the question from a "user"
point of view than an "actor" point of view, but it's all the same.

You have a system where you have a variable number of "user" objects in
a list.

These users can access various aspects of the system... they could be
anything... email, chat, file management, etc.

When a User object accesses the email module, it would be nice if that
user could then become an EmailUser (a derived class of User), and have
extra functionality, but keep a pointer to any email specific data
pointed to by a generic data pointer (void * or maybe a base data
object) within the User class, but when acting as an EmailUser, the
EmailUser specific methods would reference the EmailData object.

I imagine this is a somewhat common requirement, but I'm not sure about
the best way to go about doing this in C++.

I'm currently doing this with a lot of ugly typecasting, but I am
certain there is a better way.


At first inspection, I'd say you're making the (very common) mistake of
trying to shoehorn inheritance into a situation where it doesn't
belong. Does the logic for email rely on the User class's protected
interface? If so, why? That's excessive coupling. As a rule, prefer
composition to inheritance.

The core problem is you're trying to break a fundamental assumption of
C++ -- that is, strong typing. What that means is, for the duration of
an object's existence, it is of the same type. It does not "become"
another type, or a derived type, or anything.

There are so many ways to solve the problem you describe without
inheritance. Ask yourself why you want to use inheritance. If you
can't find a good answer, maybe there isn't one. As soon as you stop
using that hammer for everything, you'll find that not every problem
looks like a nail, and you'll learn about the rest of the toolbox.

Luke

Feb 2 '06 #4
Derek wrote:
I have an advanced question that I hope there is a possible
answer for...


Seems like this is what you want to do:
http://c2.com/cgi/wiki?RoleObjectPattern
Martin

--
Quidquid latine scriptum sit, altum viditur.
Feb 2 '06 #5
Derek wrote:
Yes, the list would be a list of pointers to base objects.

I guess it would have been better to frame the question from a "user"
point of view than an "actor" point of view, but it's all the same.
In UML, "actors" are "users", actors don't have to be a person, they
could be a another class.
You have a system where you have a variable number of "user" objects in
a list.

These users can access various aspects of the system... they could be
anything... email, chat, file management, etc.

When a User object accesses the email module, it would be nice if that
user could then become an EmailUser (a derived class of User), and have
You can't do derivation at run time. It would be better if say, the
service (email, chat, file management) would hold a reference (think
boost::shared_ptr) to the user.
extra functionality, but keep a pointer to any email specific data
pointed to by a generic data pointer (void * or maybe a base data
object) within the User class, but when acting as an EmailUser, the
Email specific data should be it's own class, that both the user and the
emailService understand.
EmailUser specific methods would reference the EmailData object.

I imagine this is a somewhat common requirement, but I'm not sure about
the best way to go about doing this in C++.

I'm currently doing this with a lot of ugly typecasting, but I am
certain there is a better way.


Indeed. Ugly typecasting is bad.

It looks to me like you have services (email, chat) and users. The
services could inherit from an abstract base, that manage the user
connections/disconnections.

It does depend on your design though; which persists longer, the
instances of the users or the instances of the services?

Does it make sense for each service instance to have multiple users? Or
would a user each have their own instance?

From your description, I would say a System has services, and a list of
users. The services are somewhat fixed, and the users come and go.
Each service can be accessed by 0 or more users.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 2 '06 #6

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

Similar topics

1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
4
by: Tom | last post by:
Hi .Net Professional : In recent , I starting to learn about dynamic call .net object in vb.net. Before, some IT people teach me use the following method and its work !! Dim asm As =...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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:
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.