[英]Self-made list in c++
list.h
class Link {
public:
string value;
Link(const string& v, Link* p = 0, Link * s = 0):
value(v), prev(p), succ(s) {}
Link* insert(Link* n); // insert n before this object
Link* add(Link* n); // insert n after this object
Link* erase(); // remove this object from list
Link* find(const string& s); // find s in list
Link* const find(const string& s) const;
Link* advance(int n) ; // get the nth successor
Link* next() const { return succ; }
Link* previous() const { return prev; }
private:
Link* prev;
Link* succ;
};
Can you tell me why we need two versions of find()
你能告诉我为什么我们需要两个版本的
find()
Link* find(const string& s); // find s in list
Link* const find(const string& s) const;
in this self-made double-linked list and what should the main difference be between these two versions? 在这个自制的双链表中,这两个版本之间的主要区别是什么?
The reason you want a const
and a non- const
version of the same member function is so you can call them on a const
and non- const
object: 你想要
const
和非const
版本的相同成员函数的原因是你可以在const
和非const
对象上调用它们:
Link link = ...;
link.find("hello"); // calls non-const find(), returns Link*
const Link clink = ...;
clink.find("goodbye"); // calls find() const
Although, your const
version returns Link* const
. 虽然,你的
const
版本返回Link* const
。 That is a const
pointer to Link
. 这是一个指向
Link
的const
指针。 It would make more sense to return a const Link*
, a pointer to const Link
: 返回一个
const Link*
,指向const Link
的指针会更有意义:
Link* find(const std::string& );
const Link* find(const std::string& ) const;
In this way, your const
Link
could still find entries - just not modify them. 通过这种方式,您的
const
Link
仍然可以找到条目 - 只是不修改它们。 That would maintain const
-ness. 这将保持
const
。 It makes sense to be able to look for things in a const collection as long as you don't modify them. 只要不修改它们,就可以在const集合中查找内容。
From what I can tell from those two function declarations, you don't need both. 从我从这两个函数声明中可以看出,你不需要两者。 The
const
version of the function would actually cover all possible use cases even if the non- const
version didn't exist. 即使非
const
版本不存在,函数的const
版本实际上也会涵盖所有可能的用例。
In fact, the best declaration would probably be this: 事实上,最好的声明可能是这样的:
Link* find(const string& s) const;
The first const
in this line: 这一行中的第一个
const
:
Link* const find(const string& s) const;
just means that the returned pointer cannot be assigned to point so something else; 只是意味着返回的指针不能被指定为指向其他东西; however, since the returned pointer would be either used directly or instantly assigned to some variable, that first
const
is unnecessary. 但是,由于返回的指针可以直接使用,也可以立即分配给某个变量,因此第一个
const
是不必要的。
Perhaps what you're looking for is this setup: 也许你正在寻找的是这个设置:
Link* find(const string& s);
const Link* find(const string& s) const;
Putting const
next to the base type (Link) instead of the asterisk (which means that it's a pointer), you are now providing a method that can either give a pointer to some Link that either does or doesn't allow you to modify that Link depending on whether the Link you're calling it on is const
or not. 将
const
放在基本类型(链接)而不是星号(这意味着它是指针)旁边,您现在提供的方法可以指向某个链接,该链接可以或不允许您修改链接取决于您调用它的链接是否为const
。
Can you tell me why we need two versions of find()
你能告诉我为什么我们需要两个版本的find()
Yes. 是。 Consider this code:
考虑以下代码:
const Link x{ "x" };
auto y = x.find("x");
// y will be a Link * const, meaning you cannot change the pointer
// address (not interesting) but you can change the object at that address.
versus: 与:
Link x{ "x" };
auto y = x.find("x");
// y will be a Link *, meaning you can change both the pointer address
// contained in y (not interesting) and the object at that address
The const version can be called on const instances (and const references and pointers to const instances). 可以在const实例(以及const引用和指向const实例的指针)上调用const版本。 The non-const version will only be callable on non-const instances.
非const版本只能在非const实例上调用。
Either way, the signature of the const version is incorrect: you should return a pointer to a const value, not a const pointer to a non-const value. 无论哪种方式,const版本的签名都是错误的:你应该返回一个指向const值的指针,而不是一个指向非const值的const指针。
That is, the function should be: 也就是说,功能应该是:
Link const * const find(const string& s) const;
// ^^^^^ ^^^^^
or 要么
Link const * find(const string& s) const;
// ^^^^^
but not: 但不是:
Link * const find(const string& s) const;
// ^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.