class MyControl : public BaseControl
{
}
class Base
{
  proctected: 
    BaseControl &mControl;
}
class MyClass : public Base
{
  public:
    MyClass( MyControl &control );
    void ReadControl();
}
MyClass::MyClass( MyControl &control )
            :Base(control)
{
}
void MyClass::ReadControl( void )
{
  MyControl &control = (MyControl &)mControl;  // <=== ???
  control.DoSomething();
}

Hi all,
Can anyone tell me how to cast the BaseControl reference to a MyControl reference ?? The above code is basicly what I have.
Thanks in advance..

Dani AI

Generated

A short, practical summary tied to the replies from and :

A safe downcast requires RTTI and a polymorphic base (at least one virtual function). A runtime-checked cast is the correct tool when BaseControl is polymorphic: the pointer form returns nullptr on failure, while the reference form throws std::bad_cast. static_cast or a C-style cast performs no runtime check and can produce undefined behavior if the dynamic type doesn't match.

Prefer design changes that remove the need to downcast. Expose the operation through the base interface or add a small virtual query method that returns a derived pointer; that makes intent explicit and keeps client code clean. Example pattern (illustrative only):

struct BaseControl {
    virtual ~BaseControl() {}
    virtual MyControl* AsMyControl() { return nullptr; }
};

struct MyControl : BaseControl {
    MyControl* AsMyControl() override { return this; }
    void DoSomething(); // MyControl-specific
};

A few additional, practical cautions:

  • When storing a reference in Base, ensure Base initializes it and the referenced object's lifetime exceeds Base's lifetime; references cannot be reseated. Use a pointer or std::reference_wrapper if reseating or ownership flexibility is needed.
  • Verify RTTI is enabled in the build (some projects disable it with e.g. -fno-rtti or /GR-).
  • If multiple or virtual inheritance is present, prefer dynamic_cast because it performs the correct pointer adjustments.

Given 's follow-up that the control was dynamic, the runtime-checked approach was appropriate. For longer-term robustness, prefer adding the needed virtual interface to BaseControl instead of relying on repeated downcasts.

Recommended Answers

All 4 Replies

Make it a pointer instead of a reference.

Make it a pointer instead of a reference.

Sorry but that is not an option...

if MyControl is a polymorphic type:

// ....
try
{
  MyControl& control = dynamic_cast<(MyControl&>( mControl ) ;
  control.DoSomething();
}
catch( const std::bad_cast& )
{
  // TODO: cast failed; handle it
}
// ...

if not:

// ....
  MyControl& control = static_cast<(MyControl&>( mControl ) ;
  // it is the programmer's (this means your) responsibility to make sure
  // that this cast is correct.
  control.DoSomething();
// ...

It was of the dynamic type !
vijayan121 thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.