1

In my code I have naturally, without thinking about it first, type-casted a derived struct into a base struct in order to call an overloaded function and later realized that it might cause problems in some cases (though I honestly don't know if and which).

My code goes as follows:

struct Base
{
    //some variables
}

struct Derived : Base
{
    //some more variables
}

bool func(const Base &base1, const Base &base2)
{
    //do some stuff and return true or false
}
bool func(const Base &base, const Derived &derived)
{
    if(func(base, (Base)derived))
        return true;
    else
        //do some more stuff and return true or false
}

The code compiles and the functions work as intended.

I have found tons of questions on SO and elsewhere about up- and down-casting, but they all involve using pointers instead of directly typecasting to the base class. In this link the person directly typecasts, however, to the derived class not the base. There are no questions I found that actually deal with this problem directly, but I might simply not know what to look for, so please point me in the right direction in that case!

I assume (and have read online) that typecasting involves creating a copy, so (Base)derived will not simply return a reference to derived, but using the first function. Are there any other downsides or issues that I might run into?

Community
  • 1
  • 1
philkark
  • 2,417
  • 7
  • 38
  • 59
  • Why are you casting to `Base` and not to `const Base&`? – Elohim Meth Aug 22 '15 at 22:54
  • You don't need to cast here at all. – David G Aug 22 '15 at 22:54
  • There will be endless recursion without cast. – Elohim Meth Aug 22 '15 at 22:57
  • @ElohimMeth I didn't know I could cast like (const Base&) to be honest. But if I can, will it actually forward the reference? – philkark Aug 22 '15 at 22:58
  • @0x499602D2 As Elohim Meth already said, yes there will be an endless recursion without the cast. This is actually an issue I ran into because I accidentally forgot the cast in the first case... – philkark Aug 22 '15 at 22:59
  • yes, simply static_cast(derived). – Elohim Meth Aug 22 '15 at 23:00
  • I assume static_cast is the C++11 standard to typecasting now? Is there any difference to just using (const Base&)? – philkark Aug 22 '15 at 23:01
  • @phil13131 `static_cast` is actually available in C++03. If you see people using C-style casts it's because they have a bad teacher!! – user5254963 Aug 22 '15 at 23:04
  • No there will be no difference in this case. It's considered a good style to use C++ style casts instead old C style casts. static_cast is not new to C++11. – Elohim Meth Aug 22 '15 at 23:06
  • @user5254963 Thank you for the answer. I will look into that and find out the differences. So far I never stumbled upon any problems with C-style casts, but I assume there are some obvious ones. – philkark Aug 22 '15 at 23:06

1 Answers1

6

Use static_cast<const Base&>(derived) instead of (Base)derived. It will forward reference, and correct overload resolution.

Elohim Meth
  • 1,777
  • 9
  • 13