简体   繁体   English

什么是最好的以及如何在C ++函数中声明const

[英]what is the best and how declare const in function of c++

I have these code 我有这些代码

#include <iostream>
using namespace std;
class Ex;
class Exx;

class Ex {
public:
    int _val;
    int const *_p_val;
    void setPtrVal(int * const &val) {
        _p_val=val;
    }
};

class Exx {
public:
    Ex const *_ex;
    void setEx(Ex const &ex)
    {
        _ex=&ex;
    }
};

int main()
{
    Ex x;
    int i=10;
    x.setPtrVal(&i);
    Exx xx;
    xx.setEx(x);
    int y=20;
    cout<<*(xx._ex->_p_val)<<endl;
    x.setPtrVal(&y);
    cout<<*(xx._ex->_p_val)<<endl;
    cout<<*x._p_val<<endl;
    return 0;
}

1: you can see, Ex x is not a const of Ex class . 1:您可以看到, Ex x不是Ex类的const。 And Ex const *_ex; Ex const * _ex; is a pointer point to only Ex const . 是仅指向Ex const的指针。 why everything above are ok? 为什么上面的一切都还好?

2: Is const in void setEx(Ex const &ex) just means you can't modify ex in function body? 2:const in void setEx(Ex const&ex)是否仅意味着您不能在函数体中修改ex?

3: how fix setter function for member pointer variable if I want prototype like above (suppose for sercurity reason)? 3:如果我想要上述原型(假设出于安全原因),如何修复成员指针变量的setter函数?

OK. 好。 if Ex const *_ex; 如果Ex const * _ex; become Ex *_ex; 成为Ex * _ex; So, in setter function, I want a prototype do not modify argument object, just like above. 因此,在setter函数中,我希望原型不像上面那样修改参数对象。 How function body become? 机体如何变成?

  1. A pointer (or reference) to const does not have to point to a const object. 指向const指针(或引用)不必指向const对象。 It just means that it can't be used to modify the object it points to; 它只是意味着它不能用于修改它指向的对象; so it can safely point to either a const or non- const object. 因此它可以安全地指向const或非const对象。

  2. Yes. 是。 Specifically, it means that the reference passed as the function argument can't be used to modify the object. 具体来说,这意味着作为函数参数传递的引用不能用于修改对象。

  3. It's better to include const if you don't need to use it for modification. 如果您不需要使用const进行修改,则最好包含const That will prevent accidental modification, and allow you to point to const objects. 这样可以防止意外修改,并允许您指向const对象。

First, you're actually right, writing void setEx(Ex const &ex) means inside setEx , the parameter &ex is cv-qualified. 首先,您实际上是对的,将void setEx(Ex const &ex) setEx ,意味着参数&ex是cv限定的。

That doesn't mean what's pointed at (or referenced in your case) was necessarily something already const in the calling code. 这并不意味着所指向的(或在您的情况下引用的)一定是在调用代码中已经常量的东西。

Then, onto what is the best, well it depends what you actually want. 然后,什么才是最好的,这取决于您的实际需求。 If you write Ex const* ex; 如果您写Ex const* ex; for your member, it means that once it is set through your object initialization, the object itself (through its member function) cannot modify the data pointed at . 对于您的成员,这意味着一旦通过对象初始化对其进行设置,则对象本身(通过其成员函数)无法修改所指向的数据。 It still can assign a new value to the pointer (make it point elsewhere), but that means it can't modify what's pointed at. 它仍然可以为指针分配一个新值(使其指向其他位置),但这意味着它无法修改所指向的内容。

Finally, it comes down to what you want. 最后,归结为您想要的。

  • If the data shouldn't be modified : Ex const * ex; 如果不应该修改数据: Ex const * ex; will enforce that. 将强制执行。
  • If the pointer shouldn't be modified : Ex * const ex; 如果不应修改指针: Ex * const ex; will enforce that. 将强制执行。
  • If both the pointer and the data must remain untouched : Ex const * const ex; 如果指针和数据都必须保持不变: Ex const * const ex; will do. 会做。

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

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