C ++ pointing question

still works in C ++, but it appeared in my book, and I don’t understand what it is for:

MyClass * FunctionTwo (MyClass *testClass) {
    return 0;
}

My question is what is the meaning of the first indirectness operator

(MyClass *[<- this one] FunctionTwo(MyClass *testClass))?

I tried to make such a function in code blocks with and without the first *, and I did not see any difference in how it was launched or displayed:

int *testFunc(int *in) {
    cout << in << endl;
    cout << *in << endl;
    return 0;
}

int testFuncTwo(int *in) {
    cout << in << endl;
    cout << *in << endl;
    return 0;
}

I could not find anywhere, explaining about it in my book.

Thank,

Charles

+3
source share
5 answers

MyClass * means that this function returns a pointer to an instance of MyClass.

Since the code does indeed return 0 (or NULL), this means that any caller of this function returns a NULL pointer to the MyClass object.

I think the following examples:

int *testFunc(int *in) {
    cout << "This is the pointer to in " <<  in << endl;
    cout << "This is the value of in "  << *in << endl;
    return in;
}

int testFuncTwo(int *in) {
    cout << "This is the pointer to in " <<  in << endl;
    cout << "This is the value of in "  << *in << endl;
    return *in;
}


void test() {
  int a = 1;

  cout << "a = " << a;

  int output = *testFunc(&a); // pass in the address of a
  cout << "testFunc(a) returned " << output;

  output = testFuncTwo(&a); // pass in the address of a
  cout << "testFuncTwo(a) returned " << output;

}

, ++ , .

+10

, .

-:

int x = 123; // x is a memory location (lvalue)
int* y = &x; // y points to x
int** z = &y; // z points to y

z y, x, , 123.

x- > y- > z [123] ( , )

y , , NULL, , z , , NULL, .

, , ? : , . , - , , :

void kill(Player* p);

, . :

void kill(Player p);

, . .

/ NULL ( NULL, 0), , - ( -, - ).

, , , , . pointee ( ).

. , , , . , , , .

+2

, ++:

return_type function name (    parameter_type parameter_name )

return_type - MyClass *, " MyClass" , _type MyClass *.

, :

typedef MyClass *PointerToMyClass;
PointerToMyClass FunctionTwo (PointerToMyClass testClass)
{
 return 0;
}

?

+2

:

int *testFunc(int *in)

, int.

int testFuncTwo(int *in)

int.

Zero - (int*) 0, - 0. , , MyClass*, MyClass - operator int MyClass, .

+1

- int * Function(), . MyClass * , MyClass. ++ , , , , , .

, , , , , - , ( ), .

+1

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


All Articles