G
Guest
In C++ we can not overload the explicit typecast operator like
static_cast, dynamic_cast, const_cast and reinterpret_cast. The only
we can do is the implicit typecast operator like operator
MyClass::MyDistType().
I agree there are maybe no meaning to overload dynamic_cast/const_cast/
reinterpret_cast, except the static_cast. If we enable the overloading
of static_cast, we could be able to force the user doing type casting
explicitly.
There are two ways to implemation this:
Suggestion 1:
class B;
class A
{
public:
friend B operator static_cast<B>(const A &);
};
A a;
// B b=a; //Error, no type casting operator
B b=static_cast<B>(a); //Pass, using the friend casting operator
Suggestion 2:
class B;
class A
{
public:
explicit operator B();
};
A a;
// B b=a; //Error, the type casting operator decalred as explicit
B b=static_cast<B>(a) //Pass, using explicit type casting
static_cast, dynamic_cast, const_cast and reinterpret_cast. The only
we can do is the implicit typecast operator like operator
MyClass::MyDistType().
I agree there are maybe no meaning to overload dynamic_cast/const_cast/
reinterpret_cast, except the static_cast. If we enable the overloading
of static_cast, we could be able to force the user doing type casting
explicitly.
There are two ways to implemation this:
Suggestion 1:
class B;
class A
{
public:
friend B operator static_cast<B>(const A &);
};
A a;
// B b=a; //Error, no type casting operator
B b=static_cast<B>(a); //Pass, using the friend casting operator
Suggestion 2:
class B;
class A
{
public:
explicit operator B();
};
A a;
// B b=a; //Error, the type casting operator decalred as explicit
B b=static_cast<B>(a) //Pass, using explicit type casting