简体   繁体   English

类对象的常量性如何影响该类的成员?

[英]How the const-ness of a class object affects members of that class?

AFAIK a const object of a class type causes its all members to be const as well. AFAIK 类类型的const对象导致其所有成员也是const And a pointer to a plain object cannot point to a const object.指向普通对象的指针不能指向const对象。

In this example I am trying to understand Pointer to Member of class:在这个例子中,我试图理解指向类成员的指针:

struct Foo{
    int value_ = 1024;
};

int main(){

    int Foo::* ptr = &Foo::value_; // ptr is a pointer to any non-const non-static integer member data
    Foo f;
    ++(f.*ptr);// ok

    Foo const cf;
    // ++(cf.*ptr); // error. OK
    std::cout << cf.*ptr << '\n';

    std::cout << "\ndone!\n";
}

As you can see ptr is a pointer to a non-static non-const member data of class Foo of type int , which means it cannot point to a const integer member data.正如你所看到的, ptr是一个指向int类型的类Foo的非静态非常量成员数据的指针,这意味着它不能指向一个const整数成员数据。

  • cf is a const object of type class Foo , and we know that members of a constant object are themselves constant so why is this allowed: cfFoo类型的const对象,我们知道常量对象的成员本身是常量,所以为什么允许这样做:

     std::cout << cf.*ptr << '\\n'; // why allowed?
  • cf 's value_ is now constant because cf is const so why it is allowed to bind the pointer ptr to that constant member data? cfvalue_现在是常量,因为cfconst那么为什么允许将指针ptr绑定到该常量成员数据?

Pointers to members of a class are rarely used features and are very different from normal pointers.指向类成员的指针是很少使用的特性,与普通指针有很大不同。

  • They only hold the offset to the member within the class他们只持有类内成员的偏移量
  • You cannot increment or decrement the pointer itself as you can do with a normal pointer您不能像使用普通指针一样增加或减少指针本身

The way I look at these pointers is that they just create a new name or a new way to access the member of the class if they happen to be public members.我看待这些指针的方式是,如果它们碰巧是公共成员,它们只是创建一个新名称或访问类成员的新方法。

With your pointer to a member of a const object, the compiler knows that it is currently pointing to a member of a const object.使用指向 const 对象成员的指针,编译器知道它当前指向的是 const 对象的成员。 And you will be fine, as long as you are only accessing the data member and not modifying it.只要您只访问数据成员而不修改它,您就可以了。

Modifying your case as below, where the data element itself is a const, in this case also you will be able to read the data member, but the compiler will not allow you to change the data member.如下修改您的情况,其中数据元素本身是一个常量,在这种情况下,您也可以读取数据成员,但编译器不允许您更改数据成员。 Also, note that you will need to declare the class member pointer to be pointing to a const int.另外,请注意,您需要将类成员指针声明为指向 const int。

struct Foo
{
    const int value_ = 1024;
};

int main()
{
    const int Foo::* ptr = &Foo::value_;
    Foo f;
    //++(f.*ptr);// Error
    std::cout << f.*ptr << '\n';  //OK, works

    Foo const cf;
    // ++(cf.*ptr); // error. OK
    std::cout << cf.*ptr << '\n';

    std::cout << "\ndone!\n";
}

These being said, you should rarely ever find the need to use pointers to data members of a class.话虽如此,您应该很少会发现需要使用指向类数据成员的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM