class Product
{
...
}
class Perishable : public : Product
{
public:
int getday();
}
int main()
{
Product *temp;
//due to some coding
//temp could point to either Perishable object or Product object that is determine //during runtime
cout<< ((Perishable*)temp)->getday() ;// is there other way to achieve this typecasting seems dangerous
このコードの問題は、tempがProductオブジェクトを指す場合、temp-> getday()が無効で、これを防ぐ方法がわかりません。いくつかの状況のために、私は腐敗可能な製品ではなく、製品ではないgetday()を持つことが許可されている場合、tempが壊れやすいオブジェクトまたはProductオブジェクトを指しているかどうかを確認する方法はありますか?C++の基本的な多態性
いくつかの助けをいただければ幸いです/
}
これは多型性の目的を破るようです。または私はあなたの質問を誤解しています。 – grep
「ダウンキャスティング」について質問していると思います。http://en.wikipedia.org/wiki/Downcast? – reuben