2
class A
{
public:
    void display()
    {
        cout << "class A\n";
    }
};

class B
{
public:
    void show()
    {
        cout << "class B\n";
    }
};

int main()
{
    A* aPtr = new A;
    B* bPtr = new B;
    B* bPtr2 = (B*) aPtr;

    return 0;
}

In the above code why in C++ it is allowed to cast a pointer of one class type to another. Since the two class are not related still why B* bPtr2 = (B*) aPtr; not throws an compile time error for casting the pointers of unrelated types.

Krishna Oza
  • 1,390
  • 2
  • 25
  • 50
  • 3
    That's a C style cast, for which the compiler assumes you know what you're doing .... try `static_cast` or `dynamic_cast` instead and see what happens. – Roger Rowland Jul 04 '14 at 05:03
  • that's what my question was, a c++ compiler should not allow this and should throw an compile time error. – Krishna Oza Jul 04 '14 at 05:05
  • See also: http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used – Ilya Jul 04 '14 at 05:06
  • also dynamic_cast cant be used here since the class A is not a polymorphic type – Krishna Oza Jul 04 '14 at 05:07
  • 2
    But you're not using C++, those are C style casts. C++ has casting operators such as `static_cast`, `dynamic_cast`, `reinterpret_cast`, etc. – Roger Rowland Jul 04 '14 at 05:07
  • 1
    `The C++ compiler should not allow valid C syntax` wat. FWIW you can always compile with `-Wold-style-cast` – user657267 Jul 04 '14 at 05:08
  • 3
    @Surfing_SO there are lots of things that cause undefined behaviour but do not require the compiler to give an error. That's just the way things are. Partly for historical reasons, but a lot of the time for practical reasons. The language tools exist to help you with not causing undefined behaviour, in this case the C++-style casts. But if you are determined to shoot yourself in the foot, C++ does not provide armor-plated boots. – M.M Jul 04 '14 at 05:09
  • @RogerRowland using `reintrepret_cast` here would cause no compiler error, so I guess the C++-style cast is not a panacea – M.M Jul 04 '14 at 05:11
  • @MattMcNabb Yes, `reinterpret_cast` is the C++ equivalent of "leave me alone, I know what I'm doing". – Roger Rowland Jul 04 '14 at 05:11
  • @Surfing_SO note that the cast itself is actually not a problem; it is only undefined behaviour if you dereference `bPtr2` – M.M Jul 04 '14 at 05:11
  • 1
    If the C++ compiler did not compile C-style casts, it would no longer be possible to compile C code using the C++ compiler, and C++ would lose one of its major features (and they'd have to rename it to something else ;)) – Jeremy Friesner Jul 04 '14 at 05:12
  • @MattMcNabb agreed. But this code after casting will call class B's method, is this behavior normal or it depends on compiler. – Krishna Oza Jul 04 '14 at 05:13
  • 1
    @Surfing_SO You are invoking undefined behavior if you try to use `bPtr2`. – T.C. Jul 04 '14 at 05:14
  • What consequences can be there if I further use bPtr2. – Krishna Oza Jul 04 '14 at 05:26
  • 1
    Demons may fly out of your nose – M.M Jul 04 '14 at 05:27

2 Answers2

6

The C cast syntax will do different things depending on the types involved. One of the things it can do is a reinterpret_cast.

This is one of the reasons you should not use the C cast syntax, and instead stick with C++ casts. With C++ casts you get the operation you asked for instead of possibly something else.

a c++ compiler should not allow this and should throw an compile time error.

C compatibility is one of the reasons C++ has been successful. C shouldn't have had this 'polymorphic' cast syntax, but it did and C++ needed to be compatible. If you're writing new C++ code you should simply not use the old syntax.

On some compilers you can disable C style casts using something like: -Werror=old-style-cast.

bames53
  • 86,085
  • 15
  • 179
  • 244
1

The example you show is called an "unsafe" or "unchecked" cast, and quite rightly so. It is a low-level construct carried over from backward compatibility with C. You should not be using this in modern C++ code.

The proper way to do a cast is to use the operations static_cast, dynamic_cast or their relatives. See the related question When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? for more details.

You can force GCC to show a warning for these casts by using the option -Wold-style-cast. To convert all warning into errors, you can add the option -Werror.

Community
  • 1
  • 1
metacubed
  • 7,031
  • 6
  • 36
  • 65