Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have this kind of set-up; a custom classes
class Base {
//    public:
//        virtual void funcBase();
};

class DeriveOne : public Base {
    public:
        char* funcDeriveOne();
//        void funcBase() final override;
};

Class DeriveTwo : public Base {
    public:
        bool funcDeriveTwo();
//        void funcBase() final override;
};


What I have tried:

we have this func that accept Base or Derive classes as an arguments and without returning anything.
void func(const Base& base) {
    std::cout << (DeriveOne&)base.funcDeriveOne() << std::endl;
    std::cout << (DeriveTwo&)base.funcDeriveTwo() << std::endl;
}
Posted
Updated 22-May-22 3:00am
v3

1 solution

Don't use C-like casts in C++ code.
C++ features dynamic_cast. Try
C++
#include <iostream>
using namespace std;

class Base
{
public:
  virtual void funcBase() = 0;
  virtual ~Base(){}
};

class DeriveOne : public Base
{
public:
  const char* funcDeriveOne(){  return "hi";}
  void funcBase() final override
  {
  }
  virtual ~DeriveOne(){}
};

class DeriveTwo : public Base
{
public:
  bool funcDeriveTwo() { return true;}
  void funcBase() final override
  {
  }
  virtual ~DeriveTwo(){}
};


void func( Base & base)
{
  try
  {
    cout << dynamic_cast<DeriveOne & >(base).funcDeriveOne() << endl;
  }
  catch (exception & e)
  {
    cout << e.what() << " (DeriveOne)" << endl;
  }
  try
  {
    cout << dynamic_cast<DeriveTwo & >(base).funcDeriveTwo() << endl;
  }
  catch(exception & e)
  {
    cout << e.what() << " (DeriveTwo)" << endl;
  }
}

int main()
{
  Base * pdo = new DeriveOne();
  Base * pdt = new DeriveTwo();

  func(*pdo);
  cout << "--------" << endl;
  func(*pdt);

  delete pdt;
  delete pdo;
}



Quote:
is there another way to cast it down in a traditional or legacy sort of way?
Of course, you can:
C++
void func_c_cast( Base & base )
{
  cout << (*(DeriveOne *)&base).funcDeriveOne() << endl;
  cout << (*(DeriveTwo *)&base).funcDeriveTwo() << endl;
}
However, you are looking for 'unexpected behaviour'.
 
Share this answer
 
v2
Comments
Phoenix Liveon 19-May-22 4:57am    
Thank you, I am grateful for the solution; Um... is there another way to cast it down in a traditional or legacy sort of way?
Phoenix Liveon 19-May-22 5:00am    
Or a link to it, I'd love to know what they are and why they shouldn't be used.
CPallini 19-May-22 5:20am    
You may find many interesting pages (and flame wars as well) on C-like and C++ type casting.
In any case you should well aware of what are you doing, and think twice, before choose the right casting.
Phoenix Liveon 19-May-22 6:22am    
Thank you once more. I now understand what it is and why it is important. The recommended one can catch an exception, whereas the other one need to handle it in some kind of conditional statement.
CPallini 19-May-22 6:23am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900