Can we pass pointer to function contains const reference variable

I am getting an error cannot convert argument 1 from 'Cars*' to 'const Cars&', for the following snippet.. Cant we send pointer to a function contains const ref as argument?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Set<Cars*> ObjectList;
....
...
...
for (auto& Object : ObjectList)
 {
   UpdatetData(Object);
 }
...
...
 void UpdatetData(const Cars& Object)
 {

 }


it worked when i replaced reference with pointer
void UpdatetData(const Cars& Object) with void UpdatetData(const Cars* Object)

I dont understand whats happening here and reasons behind it
Last edited on
p = &x is a pointer: the address-of operator.
type &x = value is a reference.
cars& object is a reference parameter , not a pointer.
cars *object would be a pointer.
so call it with
*objectlist.find(something) etc ... get a value out of the set, that is a pointer, dereference it (the * in front) and its a thing again, which is what the function wants.
A pointer is not the same thing as a reference.

The problem is not const. The problem is that you're trying to pass a Cars pointer to a function that expects a Cars reference. What you need to do is to use the * operator on the pointer. That'll give you a reference to the object that the pointer points to.

 
UpdatetData(*Object);
Last edited on
Topic archived. No new replies allowed.