C++ 虚函数 小例子

考虑下列代码,

class Animal {

private:

    std::string m_strName;

protected:

    Animal(const std::string& strName): m_strName(strName){}

public:

    const std::string& GetName() { return m_strName; }

    virtual const char* Speak()  {return "???"; }

};


class Cat: public Animal {

public:

    Cat (const std::string& strName): Animal(strName){}

    const char* Speak(){return "Meow";}

};


class Dog : public Animal {

public:

    Dog(const std::string& strName) : Animal(strName){}

    const char* Speak() {return "woof"; }

};



int main() {

    Cat cCat("Fred");

    Animal *pAnimal = &cCat;

    cout << "Animal is named " << pAnimal->GetName() << " , and it says " << pAnimal->Speak() << endl;

    return 0;

}



注意点1:

输出为Animal is named Fred, and it says ??? 

这个是因为,首先panimal是一个指向Animal的指针,所以pAnimal能访问到的只有animal class中的成员函数。这样的话,他是看不到他的derived class cat里面说的话的。 所以这个时候如果在Speak 前面加一个virtual, class Animal就会产生一个Vtable, Vtable里就会指向正确的,应该调用的函数,也就是Cat里面的Speak. 


注意点2: 

最开始我很不明白,为什么Animal的构造函数是string &str, 但是我传进去的只是一个值 "Fred",按理说这样应该报错啊。毕竟一个单独的字符串是没有地址的,后来发现,构造函数的参数不是string &str,而是const string &str。 所以如果加了const之后,C++可能会为了保持const的特性,而分配一个固定的内存给传入的参数,所以"Fred"就有了他自己的地址。所以我们也就可以通过Cat cCat("Fred")来构造函数。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值