Run-time typing...

B

barcaroller

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?
 
V

Victor Bazarov

barcaroller said:
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
 
V

Victor Bazarov

Alexander said:
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.

V
 
A

Alexander Dünisch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top