Bir kullanıcı tanımlı sınıftan ilkel türden (int, kısa, vb.) Bir tür dökümü tanımlamanın bir yolu var mı? Ayrıca, bu tür bir mekanizma açık bir oyuncuya mı ihtiyaç duyar, yoksa örtük olarak mı çalışır?C++ Döküm tipinin tanımlanması
// simplified example class
class MyNumberClass
{
private:
int value;
public:
// allows for implicit type casting/promotion from int to MyNumberClass
MyNumberClass(const int &v)
{
value = v;
}
};
// defined already above
MyNumberClass t = 5;
// What's the method definition required to overload this?
int b = t; // implicit cast, b=5.
// What's the method definition required to overload this?
int c = (int) t; // C-style explicit cast, c=5.
// ... etc. for other cast types such as dynamic_cast, const_cast, etc.
Keep örneğin
akılda gizli dönüşümler, çoğu durumda, Kötü bir Şey vardır. Açık bir 'get() işlevi ya da bir şey yapmaktan daha iyi olursun. – GManNickG
Neden dolaylı bir dönüşümde sorun olur? –