What Does The Star (*) After UTexture Mean?

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

IIRC It means a Pointer to a Variable of type UTexture.

I believe it creates a pointer to a variable of a specific type, but you can change what specific object it points to, AKA it doesn’t have to be implemented straight away.

I believe it reserves a specific amount of memory at a specific location so you can come back to it later and you can always reference it in your code, even if there’s nothing technically “there”. Don’t quote me on this though, I’m by no means an expert.

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.

I think most people usually put it before instead of after, though I’m not entirely sure it makes a difference. Once again though, I could be talking rubbish :smiley:

Page 63

The concept of pointers is well discussed :wink:

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.

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;