简体   繁体   English

指向指向对象

[英]reference to the pointed object

Dereferencing pointers can make the code very hard to read. 取消引用指针会使代码很难阅读。 What I usually do is putting a reference to the pointed object and working with the reference. 我通常要做的是对指向的对象进行引用并使用该引用。 Example: 例:

shared_ptr<std::vector<int> > sp = get_sp_to_vector();
std::vector<int>& vec = *sp;
...
vec.push_back(5);

I wonder if it's a good practice. 我不知道这是一个好习惯。 Does it have any drawback? 有什么缺点吗?

Update: To complete the example, I'd define get_sp_to_vector() the following way: 更新:为了完成示例,我将通过以下方式定义get_sp_to_vector()

shared_ptr<std::vector<int> >  get_sp_to_vector()
{
    // create a vector and send back a shared pointer pointing at it
    shared_ptr<std::vector<int> >  sp(new std::vector<int>);
    sp->push_back(1);  sp->push_back(3);
    return sp;
}

I wouldn't consider this a good practice. 我认为这不是一个好习惯。 Pointers are a main-stay of C/C++ development and b/c of that, C/C++ coders are comfortable with the de-reference syntax needed by pointers. 指针是C / C ++开发的主要内容,而b / c则是C / C ++编码人员对指针所需的取消引用语法感到满意。 I do try and avoid the use of pointers when possible (not for syntactical reasons though), but sometimes it's just the best tool for the job. 我会尽力避免使用指针(尽管不是出于语法原因),但是有时它只是完成工作的最佳工具。 If you have code that is gnarly b/c you're using a pointer, I would typically de-reference it into a function that passes the object by reference which essentially does what you're doing, but in my opinion it does so in a more elegant way. 如果您的代码使用的是肮脏的b / c,那么我通常会将其取消引用到一个函数中,该函数通过引用传递对象,这实际上完成了您正在做的事情,但我认为这样做是更优雅的方式 So instead of: 所以代替:

shared_ptr<std::vector<int> > sp = get_sp_to_vector();
std::vector<int>& vec = *sp;
...ugly stuff...
vec.push_back(5);

I would do: 我会做:

void func(std::vector<int> &vec)
{
     ... previously ugly stuff...
     vec.push_back(5);
}

shared_ptr<std::vector<int> > sp = get_sp_to_vector();
func(*sp);

EDIT: 编辑:
This isn't to say that you should create a function simply to create nicer syntax for pointers, as it is to state that if you don't like pointer syntax and your code uses clean and concise functions, you can simply make the functions take references and de-reference your pointer when calling the function. 这并不是说您应该创建一个函数只是为了为指针创建更好的语法,而是要声明,如果您不喜欢指针语法,并且代码使用简洁的函数,则只需使函数采用调用函数时引用和取消引用指针。 For situations where you have only a few calls to *p or p->x, it seems silly to create a function with a reference or to create a reference in order to call px Just use the pointer syntax in these cases as this is C/C++ syntax. 对于仅对* p或p-> x进行几次调用的情况,使用引用创建函数或创建引用以调用px似乎很愚蠢,在这些情况下只需使用指针语法即可,因为这是C / C ++语法。

Others have brought up using references within loops where a pointer may have to be de-referenced many times. 其他人提出在循环中使用引用,在循环中可能必须多次取消对指针的引用。 I do agree that in these cases a reference would be beneficial. 我同意在这种情况下参考会很有帮助。

Using local references is common, especially inside loop bodies, but there is one requirement: only do it when the reference will obviously live as long as the target object. 使用局部引用是很常见的,尤其是在循环体内,但是有一个要求:仅当引用显然与目标对象一样长时,才使用局部引用。

This usually isn't hard to guarantee, but here are some bad examples: 通常这并不难保证,但是这里有一些不好的例子:

shared_ptr<X> p = get_p();
X& r = *p;
p.reset(); // might result in destroying r
use(r); // oops

// or:
shared_ptr<X> p = get_p();
X& r = *p;
p = get_some_other_p(); // might result in destroying r
use(r); // oops

IMHO, I think this is an acceptable practice (with some caveats - See Roger Pate's post). 恕我直言,我认为这是可以接受的做法(有一些警告-请参见Roger Pate的帖子)。 If you're adding a line of code just for the push_back() call, then I don't think that is acceptable. 如果只为push_back()调用添加一行代码,那么我认为这是不可接受的。

However, if you find you are dereferencing the pointer many times, then not only is dereferencing it once into a reference object acceptable, it could be a performance win (depending on your compiler, the code in question, the phases of the moon, etc). 但是,如果你发现自己被取消引用指针很多次,那么不仅是一次提领它变成一个参照物可以接受的,它可能是一个性能取胜(取决于你的编译器,有问题的代码,月亮的阶段,等)。

If I am actually doing pointer manipulation, then I leave it a pointer. 如果我实际上在进行指针操作,则将其留给指针。 If my pointer is simply a nilable reference, then I leave it a pointer. 如果我的指针仅仅是一个空引用,那么我将其保留为指针。 If I am assured or wanting to assure that the pointer will always be non-zero (Ie if I am are returning the non-nil object from a method) then I make this explicit by using a reference. 如果我确信或希望确保指针将始终为非零(即,如果我要从方法返回非nil对象),则可以通过使用引用来使其明确。

Working back from your proposed code, I don't agree that: 从您提出的代码中回溯,我不同意:

shared_ptr<std::vector<int> > sp = get_sp_to_vector();
...
sp->push_back(5);

is "very hard to read". 是“很难读”。 It's also shorter than your code. 它也比您的代码短。 So although I don't think there's much wrong with defining and using a local reference, I also don't think you should make a rule to always do so. 因此,尽管我认为定义和使用本地引用没有多大问题,但我也不认为您应该制定规则来始终这样做。

Perhaps if your pointer code is very hard to read, it's because you're missing some trick which would make it simpler. 也许如果您的指针代码很难阅读,那是因为您缺少一些使之更简单的技巧。 Maybe if you ask another question with an example of bad pointer code that you'd currently fix with a reference, SOers would find alternative fixes. 也许如果您提出另一个问题,并给出了一个错误的指针代码示例,该错误代码示例目前正在使用参考进行修复,SOers会找到替代的修复方法。

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

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