【发布时间】:2021-02-01 05:03:08
【问题描述】:
我是 C++ 的新程序员。我遇到了一个问题,我无法理解这一点。你能帮我弄清楚吗?这是本书的一个例子,
class TextQuery {
public:
using line_no = std::vector<std::string>::size_type;
TextQuery(std::ifstream &);
QueryResult query(const std::string &) const;
private:
std::shared_ptr<std::vector<std::string>> file;
std::map<std::string,std::shared_ptr<std::set<line_no>>> wm;
//static std::string cleanup_str(const std::string &); // the book example uses 'static'
std::string cleanup_str(const std::string &); // I think 'static' key word is not needed.so i remove it.
};//class declearation ends here.
std::string TextQuery::cleanup_str(const std::string &word) {
string ret;
for(auto it = word.begin();it != word.end();++it){
if(!ispunct(*it))
ret += tolower(*it);
}
return ret;
}
QueryResult TextQuery::query(const std::string &sought) const
{
static shared_ptr<set<line_no>> nodata(new set<line_no>); //line 66
auto loc = wm.find(cleanup_str(sought)); //line 67
if(loc == wm.end())
return QueryResult(sought,nodata,file);
else
{
return QueryResult(sought,loc->second,file);
}
}
我不能不规范删除“静态”关键字的版本与不删除的版本之间的区别。编译错误是:
passing 'const TextQuery' as 'this' argument discards qualifiers [-fpermissive],67
the object has type qualifiers that are not compatible with the member function "TextQuery::cleanup_str" -- object type is: const TextQuery,67
我尝试了两种可以正常工作的方法:
- 将
'static'添加到函数cleanup_str。我无法理解为什么它可以通过。 - 我尝试的另一种方法是:删除函数
QueryResult TextQuery::query(const std::string &sought) const的最后一个'const'关键字,使其变为:QueryResult TextQuery::query(const std::string &sought)。而且这种方法有效,我也无法理解这个原因。
【问题讨论】:
-
由于
cleanup_str没有对TextQuery类的任何实例做任何事情,似乎没有任何理由让它成为非静态类成员函数。这个决定有什么原因吗? -
编译器告诉你的是
query是const成员函数,不能调用非常量成员函数。所以要么将cleanup_str也设为const,或者在这种情况下最好设为static,因为它没有充分的理由成为非静态的。 -
@DavidSchwartz 这是教科书中的一个例子。对我来说,我最初认为没有必要让这个函数成为静态成员函数。所以我删除它并且无法编译这个程序。没有其他原因。
-
@zenos 使它成为静态成员函数的原因是它可以是静态的。如果没有理由使它成为非静态的,那么它应该是静态的。为什么需要一个对象并在没有理由的情况下需要一个非常量对象?
-
@DavidSchwartz 你的意思是,所有函数成员都可以是静态的?对吧?我无法理解您的问题---“为什么需要一个对象并在没有理由要求的情况下需要一个非常量对象?” .可以举个例子吗?