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..

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.