C ++ when you need / need to pass data like (* &)

I met people passing data objects like:

declaration:

DataObject * data = 0; 

calling it like:

 SomeMethod( data ); 

Somethod definition:

 void SomeMethod(SomeObject * & object) 

My obvious question is: when and why should you do this (& *)? Does the pointer pass as a reference?

+4
source share
5 answers

Yes exactly!

So your function SomeMethod can not only refer to the same data as the caller, but can actually change what the caller points to?

 int main() { SomeObject* data = new SomeObject(1); cout << data << " -> " << *data; // "0xfffee314 -> 1" SomeMethod(data); cout << data << " -> " << *data; // "0xeee32435 -> 2" } void SomeMethod(SomeObject*& object) { delete object; object = new SomeObject(2); } 
+3
source

Does the pointer pass as a link?

Yes, that’s exactly what he does.

This is useful if you want to change the pointer itself, and not the data that it points to. Remember that C ++ passes by value, so if you pass SomeObject* , you pass a copy of the pointer to SomeObject .

+6
source

Obviously, if you want to change the value of the pointer (vs pointee). An alternative would be a pointer to a pointer.

For example, SomeMethod can highlight an object, and now the caller has a pointer modified to point to this.

 void foo(Bar*& p) { p = new Bar(...); ... } 
+4
source

It passes the pointer as a reference. This is necessary if you intend to change the pointer that you pass to the function. For instance:

 void change(int *ptr) { ptr = new int; *ptr = 5; } void change2(int *&ptr) { ptr = new int; *ptr = 5; } int a = 4; int *p = &a; cout << *p << endl; change(p); cout << *p << endl; change2(p); cout << *p << endl; 

This will output:

 4 4 5 

Edit: in addition to this, change () is a memory leak.

+2
source

Consider the following C function:

 void giveme(char **p) { *p = "Hello, World!"; } 

This gives you a string. If we passed the string as a return value, we would only use the-to-char pointer: char *giveme() , but since we use the out parameter, we must have a pointer to what we want to return. In this case, a pointer to a pointer to a char.

In one case, when you come across such code, COM , where the main function of QueryInterface is defined as :

 HRESULT QueryInterface( [in] REFIID riid, [out] void **ppvObject ); 

In the following code, we want to somehow get IUnknown and want the pointer pWhatever point to the IWhatever interface of our object. Call QueryInterface as follows:

 IUnknown *pUnk = getIUnknownFromWherever(); IWhatever *pWhatever = NULL; HRESULT hr; hr = pUnk->QueryInterface(IID_IWHATEVER, &pWhatever); 

Since we want the QueryInterface to change the pointer , not the pointee (the object it points to), we need to point it to the pointer.

Now the whole point of C ++ links is to make life easier in some common scenarios when pointers are used in C. One of them is parameters. So, the previous two code fragments in C ++ could be:

 // Assume IUnknown::QueryInterface was defined as: HRESULT QueryInterface( [in] REFIID riid, [out] void *&ppvObject ); // Now using it could have been somewhat less verbose // and error prone thanks to references: IUnknown *pUnk = getIUnknownFromWherever(); IWhatever *pWhatever = NULL; HRESULT hr; hr = pUnk->QueryInterface(IID_IWHATEVER, pWhatever); // We're not passing pWhatever, but rather a reference to // it - but since it a C++ reference rather than a pointer, // we don't need to explicitly use the address-of(&) operator 

What is it. The transfer point of a pointer as a link is basically the same as passing a parameter as a non-constant link - if the caller modifies it. Here we want the pointer (and not int or double) to change, so we pass a reference to the pointer.

0
source

Source: https://habr.com/ru/post/1306948/


All Articles