472,353 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 1198
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...
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...
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...
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)...
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...
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...
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...
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...
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...
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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.