[英]Method signature C++
Having trouble understanding method signature in C++. 无法理解C ++中的方法签名。 Looking for a second opinion.
寻找第二意见。
readPage(File* file, const PageId pageNo, Page*& page) {}
The above can be re-written as follows? 上面可以重写如下吗?
readPage(File *file, const PageId pageNo, Page *&page) {}
So, *file is pointer that is being passed in and *&page is something's address that is then converted to a pointer? 那么,* file是要传入的指针,而*&page是要转换为指针的东西的地址吗? I'm confused with the combination of
*&
我对
*&
的组合感到困惑
file
is a pointer to a File
, which means that any changes to what it points to, like file
是指向File
的指针,这意味着对其指向的内容进行任何更改,例如
file->doSomething(); // the caller will see this change
will be seen by the calling side, but if one overwrites the pointer, that not only the new value of the pointer not going to be seen by the caller, but any changes to the file after that will also not be observed, because it's a pointer to a different File
now, and because the pointer itself is passed by value. 会在调用方看到,但是如果覆盖指针,不仅调用者不会看到指针的新值,而且此后对文件的任何更改也不会被观察到,因为它是一个现在指向另一个
File
的指针,因为指针本身是通过值传递的。
file = new File(); // the caller will not see this change
file->doSomething(); // neither will it see this one
Now, page
is a reference to a pointer to a Page
. 现在,
page
是对指向Page
的指针的引用。 In other words, it behaves like a pointer, so 换句话说,它的行为就像一个指针,因此
page->doSomething(); // the caller will see this change
is still observed by the caller. 仍然被呼叫者观察到。 But now if you overwrite the pointer, the caller's pointer will also be changed, and hence all consecutive changes will also be seen:
但是现在,如果您覆盖指针,则调用者的指针也将被更改,因此所有连续的更改也将被看到:
page = new Page(); // caller also sees this change
page->doSomething(); // and this too
This is because the pointer itself is passed by reference, so any changes to the pointer itself are observed by the calling side. 这是因为指针本身是通过引用传递的,因此调用者可以观察到指针本身的任何更改。
(and yes, the way you place spaces does not matter, so the two ways you provided are identical) (是的,放置空间的方式无关紧要,因此您提供的两种方式是相同的)
Page *&是指向页面的指针
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.