Luke Knoble
Luke Knoble

Reputation: 33

In C++, the * is used for what three things?

I know the * operator is overloaded for multiplication and pointing. Is the 3rd use uniary? If so, could someone elaborate?

Upvotes: 1

Views: 746

Answers (2)

space_voyager
space_voyager

Reputation: 2034

From what I know:

  1. First use: to declare a pointer:

    int * ap;
    

    ap is an integer pointer.

  2. To dereference a pointer. *ap gives the value stores at the address pointer to by the pointer ap.
  3. Multiplication, e.g. int a = 5*2;

Upvotes: 0

Xavier Imbs
Xavier Imbs

Reputation: 212

When used as the pointer dereference operator it is a unary operator.

This is because in that context, it only takes one argument, namely the pointer.

You do see * in 5 contexts (other than quoted strings):

  1. as a multiplication operator
  2. as pointer deference
  3. as multiplication assignment *=
  4. to form part of a type; e.g. int*
  5. as part of /* and */ comment blocks

In (1) and (2) and (3), it is acting as an operator. C++ allows you to overload operators.

Upvotes: 3

Related Questions