Download

What Does The Star (*) After UTexture Mean?

I know what it would mean before it, but what does it mean after?

It’s a Pointer to a Variable of type UTexture.

1 Like

Sounds good to me – thanks! The funny thing is, I searched high and low on Google for the answer, but nothing about it. It must be such an easy concept no one even talks about it, lol.

Page 63

The concept of pointers is well discussed :wink:

1 Like

Consider lines:



UTexture* MyTexture; // defines a pointer to an object of type UTexture
MyTexture = NewObject<UTexture>();// instantiates the object (allocates memory and calls the constructor)


defines a variable called MyTexture that is a pointer to an object of type UTexture. A pointer is a variable that holds the ADDRESS of the object you are pointing to in memory. No memory is allocated for the object itself on the first line above.

See standard C or C++ docs for more details on pointers.

1 Like

That’s the weirdest thing for me – I get that it’s a pointer, but why does it come after the variable in Unreal C++, but seemingly always before in regular C++ examples online?

You can write

void* MyPointer;

or void * MyPointer;

or void *MyPointer;

Thats all fine, but if you write it directly after the type name its more clear that you define a pointer to that type.

But take care, if you create a list its better to put the star before the variable name, as

int* pt1, pt2, pt3;

…creates one pointer and two normal variables.

So you’d write

int *pt1, *pt2, *pt3;

Those might help explain it.

Make sure you don’t confuse it with a dereferencing operator also, which looks like *VariableName.

It doesn’t come after the variable, it comes after the type.



struct Type {  // define a type
  int foo;
};

Type * variable = NULL;  // define a variable as pointer-to-type

variable = new Type();  //  assign a value to a variable

(*variable).foo = 2;    // dereference a pointer variable (style 1)
assert(variable->foo == 2);    //  dereference a pointer variable (style 2)


Style 1 and style 2 are equivalent unless very peculiar operator overloading is involved.

As jwatte said, it comes after the type, not the variable(name/label).

One of the major reason for this is C++ likes to reuse the hell out of operators, with * tacking triple duty… declaration of a pointer, dereference a pointer and multiplication, so this totally valid (and ugly) C++ code for example:


int *i, *j, k = 42;
	i = j = &k;
	std::cout << *i * *j << std::endl;

As jwatte said, it comes after the type, not the variable. The astericks operator pulls triple duty in C++, declaring a pointer, dereferencing a pointer and multiplication operation. Making the following horrid code valid:


	int *i, *j, k = 42;
	i = j = &k;
	std::cout << *i * *j << std::endl;


http://www.cplusplus.com/doc/tutorial/pointers/