有* const *或没有* const *的类方法签名? [英] class method signature with *const* or without *const*?

查看:84
本文介绍了有* const *或没有* const *的类方法签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试编译(c ++)时,在Eclipse中出现以下错误

../CardDeck.cpp:17:22:错误:将"const CardDeck"作为"int CardDeck :: size()"的"this"参数传递而放弃了限定词[-fpermissive]

如果我将 int size()方法更改为 int size()const ,则错误消息已消失并已编译.我不知道为什么?

.H 文件如下:

  #include"Card.h"#include< vector>使用命名空间std;CardDeck类{私人的:向量< Card *>甲板;上市:int size();CardDeck();CardDeck(const CardDeck& rhs);CardDeck&运算符=(const CardDeck& rhs);卡和画();卡和最佳();bool isEmpty();void clear();int value();CardDeck&运算符+ =(const CardDeck& rhs);///不知道是否要返回refCardDeck&运算符+(const CardDeck& rhs);朋友CardDeck&运算符*(unsigned int num,CardDeck& rhs);朋友CardDeck&运算符*(CardDeck&lhs,无符号整数);布尔运算符< =(const CardDeck& rhs);布尔运算符> =(const CardDeck& rhs);布尔运算符<(const CardDeck& rhs);布尔运算符>(const CardDeck& rhs);布尔运算符==(const CardDeck& rhs);布尔运算符!=(const CardDeck& rhs);卡*运算符[](int i);}; 

C ++ 文件是:

  #include"CardDeck.h"int CardDeck :: size(){返回this-> deck.size();}CardDeck :: CardDeck(){};CardDeck :: CardDeck(const CardDeck& rhs){this-> clear();我对于(i = 0; i< rhs.size(); i ++){卡* current_card = rhs.deck [i];卡* new_copy =新卡(* current_card);this-&d; deck.push_back(new_copy);}}卡* CardDeck :: operator [](int i){返回this-> deck [i];}无效CardDeck :: clear(){vector< Card *> :: iterator it;for(it = this-> deck.begin(); it!= this-> deck.end(); ++ it){卡*温度= *它;this-> deck.erase(it);delete(temp);}} 

解决方案

在复制构造函数 CardDeck :: CardDeck(const CardDeck& rhs)中, rhs 是参考到 const CardDeck 对象.

因此,除非明确将 size()标记为 const ,否则 rhs.size()不会编译.那就是您的编译器告诉您的.

优良作法是使代码尽可能为 const -正确,因为这样可以防止对类中的成员数据进行错误的更改.实际上, isEmpty()以及可能的 value()也应标记为 const ,所有重载的关系运算符也应标记为.

I get the following error in Eclipse when trying to compile (c++)

../CardDeck.cpp:17:22: error: passing ‘const CardDeck’ as ‘this’ argument of ‘int CardDeck::size()’ discards qualifiers [-fpermissive]

if I change int size() method to int size() const the error msg is gone and its compiled. I dont know why ?

the .H file is the following :

  #include "Card.h"
#include <vector>

using namespace std;
class CardDeck{
    private:
        vector<Card*> deck;

    public:

        int size();
        CardDeck();
        CardDeck(const CardDeck& rhs);
        CardDeck& operator=(const CardDeck& rhs);
        Card& draw();
        Card& top();

        bool isEmpty();
        void clear();
        int value();
        CardDeck& operator+=(const CardDeck& rhs); /// not sure if to return ref
        CardDeck& operator+(const CardDeck& rhs);
        friend CardDeck&  operator*(unsigned int num,CardDeck& rhs);
        friend CardDeck&  operator*(CardDeck& lhs,unsigned int num);
        bool operator<=(const CardDeck& rhs );
        bool operator>=(const CardDeck& rhs);
        bool operator<(const CardDeck& rhs);
        bool operator>(const CardDeck& rhs);
        bool operator==(const CardDeck& rhs);
        bool operator!=(const CardDeck& rhs);
        Card*  operator[](int i);
};

and the C++ file is :

#include "CardDeck.h"
int CardDeck::size() {
    return this->deck.size();
}
CardDeck::CardDeck(){};
CardDeck::CardDeck(const CardDeck& rhs){
    this->clear();
    int i;
    for (i=0;i<rhs.size();i++){
        Card* current_card = rhs.deck[i];
        Card* new_copy = new Card(*current_card);
        this->deck.push_back(new_copy);
    }


}
Card* CardDeck::operator[](int i) {
    return this->deck[i];
}



void CardDeck::clear(){
    vector<Card*>::iterator it ;
    for(it=this->deck.begin();it != this->deck.end();++it){
        Card* temp = *it;
        this->deck.erase(it);
        delete(temp);
    }
}

解决方案

In your copy constructor CardDeck::CardDeck(const CardDeck& rhs), rhs is a reference to a const CardDeck object.

So rhs.size() will not compile unless size() is explicitly marked as being const. That's what your compiler is telling you.

It's good practice to have your code as const-correct as possible as this prevents errant changes to the member data in a class. Really, isEmpty(), and possibly value() should be marked const too, as should all the overloaded relational operators.

这篇关于有* const *或没有* const *的类方法签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆