StarmanDeluxe
StarmanDeluxe

Reputation: 13

Understanding C++ function with return type void *

I am working in Unreal Engine C++ and wish to fetch the vertex normals of a static mesh. To do this I am using the GetTangentData() method which belongs to the FStaticMeshVertexBuffer class (link).

The GetTangentData() method is defined two ways in the docs (link1, link2):

void * GetTangentData()

const void * GetTangentData() const

My understanding is that this is a getter function. However, I am unsure why it has a void pointer return type. The following line compiles but I am unsure how to access the data:

void* TangentData = StaticMeshVertexBuffer->GetTangentData();

Q1. What is the reason to have a void pointer return type?

Q2. How can one access the data from such a return?

Upvotes: 0

Views: 263

Answers (1)

user20341797
user20341797

Reputation:

Q1. What is the reason to have a void pointer return type?

A void* is a pointer to something, but that something is not known. Thus, there is a degree of flexibility to void*, resulting in the ability to be cast into many types of pointers. For example, malloc has a return type of void* because it's supposed to be used to allocate memory for all types of pointers. In C++, we can code function overloads because templates exist, so void* is more of a C thing. It is used in C++ to maintain backwards compatibility with C for libraries that are designed for usage with both C++ and C.

Q2. How can one access the data from such a return?

You typecast it:

void* void_ptr = func_that_returns_void_ptr();
char* char_ptr_typecast = (char*) void_ptr;
std::cout << char_ptr_typecast << '\n';

Upvotes: 0

Related Questions