Assigning a Link Pointer

I am using a library with a draw function that references a circle. I want to call this function, but I have a pointer to a circle object. Can I pass this to an object on a drawing object? If not, why not?

Thanks,

Barry

+4
source share
4 answers

Yes, you can.

You have this function

void DoSth(/*const*/Circle& c) { /// } 

You have this pointer

 /*const*/ Circle* p = /*...*/; 

You call it

 DoSth(*p); 

I suggest you read a good book in C ++ . This is truly fundamental material.

+14
source

It would be easier if you put some code for your problem. But basically it will work as follows:

 void func(Circle& circle) { // do something } ... Circle *pCircle = new Circle(); func(*pCircle); 
+4
source

As long as the pointer is not destroyed, while the link is still in use, this is normal.

 int *p = new int; int &r = *p; 
+3
source

Yes, you can just dereference a pointer, for example:

 void functionToCall(Circle &circle); //in your code: Circle *circle = new Circle(); functionToCall(*circle); 
+3
source

All Articles