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

Run-time typing...

I have an object ObjA of class ClassA. However, depending on the contents
of ObjA's data fields (which I do not know until the program runs), I would
like this object to be also of class ClassB. ClassB has member functions
which I need but which are relevant only if ObjA has specific contents in
its data fields.

Given that C++ is a statically-typed language, I don't know of a
straight-forward way of implementing this. Can it be done?
Aug 16 '07 #1
3 1237
barcaroller wrote:
I have an object ObjA of class ClassA. However, depending on the
contents of ObjA's data fields (which I do not know until the program
runs), I would like this object to be also of class ClassB. ClassB
has member functions which I need but which are relevant only if ObjA
has specific contents in its data fields.

Given that C++ is a statically-typed language, I don't know of a
straight-forward way of implementing this. Can it be done?
Let ClassB derive from ClassA. Have polymorphic behaviour in them,
and the choice of the behaviour leave to ClassA's non-virtual member
function which will check your data fields.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '07 #2
On Wed, 15 Aug 2007 22:10:01 -0400, "barcaroller"
<ba*********@music.netwrote:
>I have an object ObjA of class ClassA. However, depending on the contents
of ObjA's data fields (which I do not know until the program runs), I would
like this object to be also of class ClassB. ClassB has member functions
which I need but which are relevant only if ObjA has specific contents in
its data fields.

Given that C++ is a statically-typed language, I don't know of a
straight-forward way of implementing this. Can it be done?
Example:

class ClassA {
int m_Number;
public:
int getNumber() const { return m_Number; }
virtual void neededMemberFunction() { }
};

class ClassB : public ClassA {
public:
virtual void neededMemberFunction()
{
// do the important stuff here
}
};
// A free function
void doStuffWithAObjects(std::vector<ClassA*>& v)
{
std::vector<ClassA*>::iterator p = v.begin();
while(p != v.end()){
ClassA* aPtr = *p;
// The next line is basically unnecessary
// It is only needed, if, for some reason, you want to make sure
// an object is also an instance of ClassB, before calling the
// function. True polymorphism doesn't need this.
if(dynamic_cast<ClassB*>(aPtr))
aPtr->neededMemberFunction();
++p;
}
}
greets,
Alex
Aug 16 '07 #3
Alexander Dünisch wrote:
On Wed, 15 Aug 2007 22:10:01 -0400, "barcaroller"
<ba*********@music.netwrote:
>I have an object ObjA of class ClassA. However, depending on the
contents of ObjA's data fields (which I do not know until the
program runs), I would like this object to be also of class ClassB.
ClassB has member functions which I need but which are relevant only
if ObjA has specific contents in its data fields.

Given that C++ is a statically-typed language, I don't know of a
straight-forward way of implementing this. Can it be done?

Example:

class ClassA {
int m_Number;
public:
int getNumber() const { return m_Number; }
virtual void neededMemberFunction() { }
};

class ClassB : public ClassA {
public:
virtual void neededMemberFunction()
{
// do the important stuff here
}
};
// A free function
void doStuffWithAObjects(std::vector<ClassA*>& v)
{
std::vector<ClassA*>::iterator p = v.begin();
while(p != v.end()){
ClassA* aPtr = *p;
// The next line is basically unnecessary
// It is only needed, if, for some reason, you want to make sure
// an object is also an instance of ClassB, before calling the
// function. True polymorphism doesn't need this.
if(dynamic_cast<ClassB*>(aPtr))
aPtr->neededMemberFunction();
Given your hierarchy and the implementation of the virtual function
in ClassA (which does nothing), there's no need for the dynamic_cast.
At all.
++p;
}
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '07 #4

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

Similar topics

4
by: Ed | last post by:
Hello, I took a course in asp about 2 years ago and I was practicing with IIS 5.0. Then I put it down for a while. Now trying to get back to it. I can't run asp files from subdirectories of...
4
by: Primo | last post by:
Hi, This problem has been frustrating me for days and I hope you experts can help me out. I am trying to run a command, which I would normally run from the command line, from within my C#...
6
by: orekin | last post by:
Hi There I have been trying to come to grips with Application.Run(), Application.Exit() and the Message Pump and I would really appreciate some feedback on the following questions .. There are...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
3
by: emman_54 | last post by:
Hi every one, I am trying to run a batch file using my asp.net application. I am using the Process class to run the batch file. When I run my web application, In the task manager, i could see...
19
by: Bryan | last post by:
How can i run a bit of code straight from the IDE? Right now i make a temporary button and put the code behind that, then i run debug mode and click on the button. Is there a way to highlight...
9
by: Brett Wesoloski | last post by:
I am new to VS2005. I changed my program.cs file to be a different form I am working on. But when I go to run the application it still brings up the form that was originally declared as new. ...
7
by: Lee Crabtree | last post by:
I remember when I was first getting into .NET Forms programming that there was a rather emphatic rule about not constructing a form before calling Application.Run with it. So this: ...
8
by: David Thielen | last post by:
Hi; In our setup program how do I determine if I need to run "aspnet_regiis –i" and if so, is there an API I can calll rather than finding that program on the user's disk and calling it? --...
3
by: traceable1 | last post by:
Is there a way I can set up a SQL script to run when the instance starts up? SQL Server 2005 SP2 thanks!
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: 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
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
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
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,...
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...
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,...

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.