无法使用“this” const 成员函数中的指针

发布于 2024-09-03 17:13:02 字数 563 浏览 4 评论 0原文

我正在经历:有关继承的 C++ 常见问题 和决定实现它只是为了学习它:

#include "Shape.h"
 
 void Shape::print() const
 {
     float a = this->area();  // area() is pure virtual
     ...
 } 

现在,一切(好吧,几乎)都按照常见问题解答中的描述工作,除了 print()const 所以它不能访问 this 指针。一旦你取出const,它就起作用了。

C++ FAQ 已经存在一段时间了,而且通常都非常好。这是一个错误吗? 是我写错了还是有错别字?如果我错了,我想知道如何在 const 函数中访问 this 指针。

I was going through: C++ FAQs about inheritance and decided to implement it just to learn it:

#include "Shape.h"
 
 void Shape::print() const
 {
     float a = this->area();  // area() is pure virtual
     ...
 } 

Now, everything (well, almost) works as described in the FAQ except that print() is const and so it can't access the this pointer. As soon as you take out const, it works.

The C++ FAQ has been around for a while and is usually pretty good. Is this a mistake?
Does it have a typo or am I wrong? If I'm wrong, I would like to know how is it possible to access the this pointer in a const function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风吹过旳痕迹 2024-09-10 17:13:02

问题是area()函数不是const。如果该函数是 const,则可以正常编译。由于您在 const 函数内部,因此 this 指针是 const,因此您只能在其上调用 const 函数。

The problem is that the area() function is not const. If that function was const, then this would compile fine. Since your inside a const function, the this pointer is const and therefore you can only call const functions on it.

不乱于心 2024-09-10 17:13:02

为什么无法访问this?重点是,只能使用 const 成员和方法。因此,如果将 Shape::area() 声明为 const,则没有问题。此外,您可以自由读取数据成员值,但不能分配给它们:

class Shape 
{
  int i;
  void print() const;
  virtual float area() const = 0;
  virtual void modify() = 0;
};

void Shape::print() const
{
  float a = this->area(); // call to const member - fine
  int j = this->i;        // reading const member - fine

  this->modify();         // call to non const member - error
  this->i++;              // assigning to member - error
}

Why couldn't this be accessed? The point is, only const members and methods can be used. So if Shape::area() is declared const, there is no problem. Also, you can freely read data member values, but not assign to them:

class Shape 
{
  int i;
  void print() const;
  virtual float area() const = 0;
  virtual void modify() = 0;
};

void Shape::print() const
{
  float a = this->area(); // call to const member - fine
  int j = this->i;        // reading const member - fine

  this->modify();         // call to non const member - error
  this->i++;              // assigning to member - error
}
何必那么矫情 2024-09-10 17:13:02

如果 print 被定义为 const,那么它可以使用 this 访问 const 成员,但不能访问非const 成员:因此当且仅当 area 方法声明为 时,它可以调用 this->area() >const (即,如果 area 方法承诺如果调用它,则不会改变其 Shape 实例)。

If print is defined as const, then it can use this to access const members but not to access non-const members: so it can invoke this->area() if-and-only-if the area method is declared as const (i.e. if the area method promises that if it's invoked then it won't mutate its Shape instance).

孤独患者 2024-09-10 17:13:02

默认情况下将所有函数声明为 const 也是一个好习惯,只有在实际需要修改对象时才不要这样做。这也将有助于发现某些函数修改了不该修改的内容的错误。

It is also a good practice to declare all functions as const by default, and only not do so when you actually need to modify the object. This will also help finding errors of the kind where some function modifies something it is not meant to.

北方的巷 2024-09-10 17:13:02

您当然可以访问 this 指针,但在 const 函数中,它变成指向 const 对象的指针。请记住,this 指针不是真正的指针实例,因此它可以在程序中的不同点更改其类型。

You certainly can access the this pointer, but in a const function it becomes a pointer to a const object. Remember, the this pointer isn't a real pointer instance, so it can change its type at different points in the program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文