Operator & and * in functions

Asked

Viewed 334 times

1

What is the meaning of this & in front of the matrix (my function only works with &, I am overloading Cout<<(matriz1 + matriz2))

And the meaning also of the * in front of the void?! Grateful

matriz& operator + (matriz b){

    for(int i = 0; i < b.data.size(); i++)  

        for(int j = 0; j < b.data[i].size();j++)    
            b.data[i][j] += this->data[i][j];   
    }

    return b;           
    }



void * func(){
cout<<"Teste"<<endl;
}
  • 3

    Look into the questions that already exist here. This one, for example, contains a very extensive explanation about pointers and references, which are your question: http://answall.com/questions/56470/qual-a-diff%C3%A7a-between-pointer-and-refer%C3%Aancia

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

7

The question gives the impression that it is learning in an unstructured way and this does not usually work. It is important to understand all concepts, to know why each thing exists and why to use it. The question seems to refer to this, but the way it originated is worrying. Trying to do something to see if it works doesn’t help understanding. A quality book is recommended to go step by step.

To understand the basics of the subject you have the question already mentioned by C. E. Gesser in comment.

In this context these symbols are not operators, they are part of the type name. Let’s say that if they didn’t use them and the language preferred to have explicit type names in full, it would be something like this: reference matriz and pointer void.

Note that this has nothing to do with the function or the operator (which in the background is a function as well. They’re actually being used as a return, but this is about the kind of data that every function return should have. It’s just a type statement to be returned.

These codes are weird, or even wrong. At first the syntax makes no sense. The second is not even returning anything, let alone a pointer, does not make sense. That is precisely what concerns me in the question. Random codes were placed, you can’t learn right (I’m not going to put it right because it’s not the focus of the question, but can be seen in the OS, in another question). At last there’s too much error there.

The reference only exists in C++. It indicates that the content there will be a pointer managed in some way (a pointer is a very simple mechanism has more information on links here, read to understand how the reference always ends up using this mechanism). In practice there are guarantees that it will not be null (other than in senseless code) and one cannot manipulate it as the pointer allows. More details on What is the difference between pointer and reference?.

The void* is used to indicate that any type of since C is a weakly typed language and interprets what is there according to the convenience of the programmer. A example of the use of void* can be found in another question. This technique is considered obsolete in C++ for most effects (but not for everyone, if you want to spend time have some examples in the comments, even though having some cases does not invalidate that it should not be used in most cases, would long list everything that can be used in place and is not the focus of the question).

In general raw pointers should be avoided as much as in C++. They can be replaced by simple references or by smart pointers (example of use).

Read more on:

  • The conversation was moved to the chat. If someone wants to read or add something, just click on the link.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.