Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you assign a value to the pointer 'this' in C++

Tags:

In a function, how to you assign this a new value?

like image 253
Astra Meyers Avatar asked Nov 20 '12 15:11

Astra Meyers


People also ask

Can you assign a value to a pointer?

You can assign a value to a void pointer, but you must cast the variable to point to some specified type before you can dereference it. Pointer arithmetic is also not valid with void * pointers.

How do you assign the value of a pointer to a variable in C?

Declare a pointer variable with the same type as the normal variable. Initialize the pointer variable with the address of normal variable. Access the value of the variable by using asterisk (*) - it is known as dereference operator.

How do you store the value of a pointer?

When you place an ampersand in front of a variable you will get it's address, this can be stored in a pointer vairable. When you place an asterisk in front of a pointer you will get the value at the memory address pointed to.

How do you assign an integer to a pointer?

You can assign 0 into a pointer: ptr = 0; The null pointer is the only integer literal that may be assigned to a pointer.


1 Answers

You can assign the object this points at:

*this = XY; 

But you can't assign the direct value of this:

this = &XY;   // Error: Expression is not assignable 
like image 75
qwertz Avatar answered Nov 18 '22 15:11

qwertz