Please help to understand why Typecasting base to derived works.!!!

when we explicit typecaste the base to derived, it is working,

as understand the base object passed has no details of the derived class, then how can the derived class function gets called after typecasting base pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
using namespace std;


class a
{
        int a0;
        int a1;
public:
 void af() { cout<<"In a::af\n"; }
};
class b: public a
{

        int a2[100];
        int a3;
        int a4;
public:
void bf() { cout<<a2[50]<<"In b::bf\n"; }
};

class c: public b
{

public:
void cf() { cout<<"In c::cf\n"; }

};


void fun(a*p)
{
        ((c*)p)->cf();
}

main()
{
        a t;
        fun(&t);
}



output:

In c::cf
I believe this is undefined behavior. The C-style cast appears to be doing a reinterpret_cast rather than dynamic_cast.

Try substituting dynamic_cast< c* >(p)->cf(); on line 33 and see what the compiler says.
Nobody has any information on anything. The thing on the left of -> on line 33 is a pointer to c, because it has been forcefully cast to c*, so a member function of c will be called. If you want the function called to depend on the actual type, consider virtual functions. If you want a conversion which fails if it is impossible, consider dynamic_cast.
It worked because cf() doesn't access any member fields of class c. C++ compilers put the methods in a section in memory and this section is shared by all instances of the corresponding class. When a method is called, the 'this' pointer is defined and then the method is called with this 'this' pointer.

Conclusion: Yes, it is the equivalent of reinterpret_cast, and it works only because it doesn't access data members that are specific to class c. If you were to add a member field to class c and access it in cf(), then you would see how this blows up.
This is not undefined.
C++ standard section 5.4 part 5 wrote:
The conversions performed by
— a const_cast (5.2.11),
— a static_cast (5.2.9),
— a static_cast followed by a const_cast,
— a reinterpret_cast (5.2.10), or
— a reinterpret_cast followed by a const_cast,
can be performed using the cast notation of explicit type conversion. The same semantic restrictions and
behaviors apply. If a conversion can be interpreted in more than one of the ways listed above, the interpretation
that appears first in the list is used, even if a cast resulting from that interpretation is ill-formed.<...>
Dynamic cast isn't even in the list.
Last edited on
so we can casting to another type as long as they have "relationship" but we can access their specific member? is there's another type have this kind of behavior?
I wouldn't count on it.
I wouldn't count on it.
Just in case that lacks emphasis, try this:
1
2
3
4
5
6
7
struct Apple{
   int foo(){ return 5; }
};

int main() {
   return ((Apple*)0)->foo();
}

There are cases when this works, but those are the cases when that function be static or a member of the base class.
Topic archived. No new replies allowed.