Need a method to compare types of data objects at runtime
If I have a abstract class with two classes that implement it how can I
make a check that tests for the type of the implementation classes?
For example:
class Base {
public:
virtual void run (){}
virtual void stop (){}
};
class Gas_Pedal : public Base {
public:
virtual void run (){}
void set_rate (unsigned int);
private:
unsigned int m_rate;
};
class Brake_Pedal : public Base {
public:
virtual void stop (){}
void set_pressure (unsigned int);
private:
unsigned int m_pressure;
};
When I have a client that receives a Base object I want to be able to
check at run-time. So that if I am expecting a Gas_Pedal object and I
receive something else I can perform some recovery action. The software
I am designing contains an untold number of arrangements of items. Each
item expect a distinctive input type. If it receives something that it
does not know how to handle or need I want to throw an error.
In order to do this I would like to do something like:
void process (Base* sobj)
{
if (! (isExpectedType(sobj))
{
throw NotForMeException ();
}
Brake_Pedal* bp = dynamic_cast
On Sun, 24 Apr 2005 14:46:22 -0400, Stephen Torri wrote
...snip...
Each item expect a distinctive input type. If it receives something that it does not know how to handle or need I want to throw an error.
In order to do this I would like to do something like:
void process (Base* sobj) { if (! (isExpectedType(sobj)) { throw NotForMeException (); }
Brake_Pedal* bp = dynamic_cast
(sobj); bp.set_pressure (50); } So perhaps my design is flawed that I am running into this situation.
I'd say so. You might want to read about the Liskov Substitution Principle: http://www.objectmentor.com/resources/articles/lsp.pdf Jeff
It seems Gas_Pedal and Brake_Pedal should not share the same base class. Any reasons why you want to do this? -delfin
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Stephen Torri Sent: Sunday, April 24, 2005 11:46 AM To: boost Subject: [Boost-users] Need a method to compare types of data objects atruntime
If I have a abstract class with two classes that implement it how can I make a check that tests for the type of the implementation classes?
For example:
class Base { public: virtual void run (){} virtual void stop (){} };
class Gas_Pedal : public Base { public: virtual void run (){} void set_rate (unsigned int); private: unsigned int m_rate; };
class Brake_Pedal : public Base { public: virtual void stop (){} void set_pressure (unsigned int); private: unsigned int m_pressure; };
When I have a client that receives a Base object I want to be able to check at run-time. So that if I am expecting a Gas_Pedal object and I receive something else I can perform some recovery action. The software I am designing contains an untold number of arrangements of items. Each item expect a distinctive input type. If it receives something that it does not know how to handle or need I want to throw an error.
In order to do this I would like to do something like:
void process (Base* sobj) { if (! (isExpectedType(sobj)) { throw NotForMeException (); }
Brake_Pedal* bp = dynamic_cast
(sobj); bp.set_pressure (50); } So perhaps my design is flawed that I am running into this situation. I am attempting to keep the interface for the Base class simple. The underlying types could have different data and access methods because they differ so widely in what they will do. This example was given to keep things simple.
Stephen
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Delfin Rojas
-
Jeff Garland
-
Stephen Torri