What does the * do with that class?

74 Views Asked by At

It might be a "dumb" question but i dont know what * do in that code.

ClassA* classa;

ClassA is a class, so what does the * actually do with a class? and how this ?operator? called?

2

There are 2 best solutions below

1
On BEST ANSWER

* does not do anything with a class, it changes the type of the declared variable by making it a pointer:

ClassA *classa; // classa is a pointer to ClassA

You can add multiple asterisks to make pointers to pointers, pointers to pointers to pointers, and so on:

ClassA **classa; // classa is a pointer to pointer to ClassA
ClassA ***classa; // classa is a pointer to pointer to pointer to ClassA

Note that when the same asterisk is used in an expression (as opposed to a declaration) it "reduces" the level of a pointer, so

ClassA *classa;
foo(*classa); // Dereferences the pointer, producing ClassA
0
On

Pointer declaration.

After this declaration, classa is a variable with type "Pointer-to-a-ClassA object". It can point to any ClassA object, or any object that inherits from ClassA