in the code below, I get a std::bad_cast exception thrown when casting derived.properties_ from BaseProperties to DerivedProperties. Looking at the code, it seems to me like something is going wrong when intialising the BaseProperties with a reference to the DerivedProperties in the Base class constructor.
What I am trying to achieve is a simple UI where Base is essentially my Component interface and Derived is anything that is considered a Component. It seems like a fair assumption that Components may have different properties but have some similar ones, such as size and position.
Can anyone offer any suggestions as to how best to achieve my aim?
#include <iostream> #include <string> // Properties for all objects of Base type class BaseProperties { public: BaseProperties( std::string baseProperty ): baseProperty_( baseProperty ) { } virtual ~BaseProperties( ) { } std::string getBaseProperty( ) { return baseProperty_; } protected: std::string baseProperty_; }; // Properties specific to objects of Derived type class DerivedProperties: public BaseProperties { public: DerivedProperties( std::string baseProperty, std::string derivedProperty ): BaseProperties( baseProperty ), derivedProperty_( derivedProperty ) { } std::string getDerivedProperty( ) { return derivedProperty_; } private: std::string derivedProperty_; }; class Base { public: Base( BaseProperties& properties ): properties_( properties ) { } virtual ~Base( ) { } protected: BaseProperties& properties_; }; class Derived : public Base { public: Derived( DerivedProperties properties ): Base( properties ) { } friend std::ostream & operator << ( std::ostream& out, const Derived& derived ); }; std::ostream & operator << ( std::ostream& out, const Derived& derived ) { return out << derived.properties_.getBaseProperty( ) << ", " << dynamic_cast< DerivedProperties& >( derived.properties_ ).getDerivedProperty( ); } int main( ) { Derived derived( DerivedProperties( "BaseProperty", "DerivedProperty" ) ); std::cout << derived << std::endl; return 0; }
DerivedPropertiesyou are passing in.dynamic_casthere seems suspicious. You should not require a derived property in a context where you only have access to a base property. Either provide a virtual interface that will allow you to solve the task with just using the base property, or make the derived property directly available in the context where it's needed. Resorting todynamic_castis almost always a worse option.dynamic_castto make the derived property available. I have a function that returns the property via adynamic_casthowever I ommitted it here to try and keep the code shorter, unless there is another way to do so..dynamic_cast, even if it's hidden inside some nice getter function. See if you can think about a way that you can avoid the need for the cast in your current program altogether. If you have a hard time figuring it out, consider paying the fellows at CodeReview a visit, they are usually great at helping out with problems like this.dynamic_castI can see, is by using astatic_cast, since I know the type is what I expect it to be.