我不了解dynamic_cast或什么?

时间:2014-09-26 20:42:36

标签: c++ inheritance dynamic-cast

我在c ++中有点新手,但为什么这段代码打印出来的时候队列项目不是孩子,而我是在孩提时代创建的:

class Parent
{
public: virtual const char* getName() { return "Who is your daddy?"; }
};

class Child : public Parent
{
public: virtual const char* getName() { return "La gente esta muy loca?"; }
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::deque<Parent> queue;   
    queue.push_back(Child());
    Parent* p = &queue.back();
    if (dynamic_cast<Child*>(p) == 0) { std::cout << "I am not child"; }
    else { std::cout << "Welcome, son"; }
    std::cin >> c;
    return 0;
}

0 个答案:

没有答案