Const & Pointer

int const *ptr 等同于const int *ptr, 都表示指向整形常量的指针, 指针指向的地址可变,但是指向的地址里面的内容不可变。
int* const ptr表示指向整形变量的指针常量,指针指向的地址不可变,但是指向的地址里面的内容可变。
const int* const ptr 则表示指向整形常量的指针常量,指针指向的地址不可变,但是指向的地址里面的内容也不可变。

const * ---- 表示指向的量是常量。

*const ---- 表示指针本身是常量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码运行时显示munmap_chunk(): invalid pointer,应该怎么修改?#include<iostream> #include<string> using namespace std; class Creature { public: Creature(const int& hands, const int& legs):_hands(hands),_legs(legs) {cout << "A Creature has been created!" << endl; cout << "It has " << hands << " hand(s)!" << endl; cout << "It has " << legs << " leg(s)!" << endl;} ~Creature() {cout << "Creature object exiled!" << endl;} int GetHands() const {return _hands;} int GetLegs() const {return _legs;} private: int _hands; int _legs; }; class Beast:virtual public Creature { public: Beast(const int& hands, const int& legs, const string& name) :Creature(hands,legs),_name(name){cout << "Its beast name is " << _name << endl;} ~Beast() {cout << "Beast object exiled!" << endl;} string GetName() const {return _name;} private: string _name; }; class Human:virtual public Creature { public: Human(const int& hands, const int& legs, const string& name):Creature(hands,legs),_name(name) {cout << "Its human name is " << _name << endl;} ~Human() {cout << "Human object exiled!" << endl;} string GetName() const {return _name;} private: string _name; }; class Orc:public Human,public Beast { public: Orc(const int& hands, const int& legs, const string& beast_name, const string& human_name):Creature(hands, legs),Beast(hands,legs,beast_name),Human(hands,legs,human_name){} ~Orc() {cout << "Orc object exiled!" << endl;} string GetBeastName() const {return Beast::GetName();} string GetHumanName() const {return Human::GetName();} };
最新发布
05-26
这段代码中存在一个 Diamond 继承问题,即 Orc 类同时继承了 Human 和 Beast 类,而这两个类又都继承了 Creature 类,导致 Orc 类中存在两份 Creature 对象。为了解决这个问题,可以将 Creature 类的继承方式改为虚继承,即在 Creature 类继承声明中加上 virtual 关键字: ``` class Beast : virtual public Creature { public: Beast(const int& hands, const int& legs, const string& name) :Creature(hands,legs),_name(name){ cout << "Its beast name is " << _name << endl; } ~Beast() { cout << "Beast object exiled!" << endl; } string GetName() const {return _name;} private: string _name; }; class Human : virtual public Creature { public: Human(const int& hands, const int& legs, const string& name):Creature(hands,legs),_name(name) { cout << "Its human name is " << _name << endl; } ~Human() { cout << "Human object exiled!" << endl; } string GetName() const {return _name;} private: string _name; }; class Orc : public Human, public Beast { public: Orc(const int& hands, const int& legs, const string& beast_name, const string& human_name) :Creature(hands, legs), Beast(hands,legs,beast_name), Human(hands,legs,human_name) {} ~Orc() { cout << "Orc object exiled!" << endl; } string GetBeastName() const {return Beast::GetName();} string GetHumanName() const {return Human::GetName();} }; ``` 另外,如果该程序还是出现 munmap_chunk(): invalid pointer 错误,可能是由于其他部分的代码引起的内存错误,需要进一步排查。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值