C++ basic question.

Asked 2 years ago, Updated 2 years ago, 18 views

class Person{
public:

    Person *GetThis() {
        return this;
    }
    void *VgetThis() {
        return this;
    }
};
int main() 
{

    Person *p1 = new Person();
    cout << p1 << endl;
    cout << p1->GetThis() << endl;
    cout << p1->VgetThis() << endl;
    return 0;
}

Person We organized a function that declares GetThis and VgetThis functions as internal functions of the class to output the address value of the object. Even though I declared it as a void function, I could send the address value using a return Can you tell me the difference between the two functions?

c++

2022-09-21 15:29

1 Answers

void *VgetThis()
{
    return this;
}

The return type of the VGetThis function is void*, not void.

void* is a pointer type that is not possible to reverse-reference but clearly stores address values.

So it's completely different from void, which means "there's no return value.

For reference, functions such as malloc and calloc used for memory allocation return void*.


2022-09-21 15:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.