confusion about const

In my program there is a function:

1
2
3
4
5
void findLeaves1 (const Node <Comparable> * & t) const
{ ....
  ... Node <Comparable> * u1 = t; 
  .....v.push_back(u1);
}


I get an error "cannot convert const Node <Comparable> * to Node <Comparable> *
. Why is it so? I am not trying to change the t, just copy it.

THanks
Then do:

1
2
3
4
5
void findLeaves1 (const Node <Comparable> * t) const
{
    const Node <Comparable> * u1 = t; 
    v.push_back(u1);
}


I also found no sense/use to having a pointer to a reference to a node so I removed that. I'm not even sure that such a thing compiles.
I think that maybe you need to cast away your constness before doing that. Have you tried
const_cast? on your t argument?

http://www.cplusplus.com/doc/tutorial/typecasting/
Do not use const_cast. Either the pointer should point to a const Node or it shouldn't.
Last edited on
Agreed. The use of const_cast is pure evil. :-P Maybe someday when programming a very complex framework that uses one other framework you'll be forced to do such thing.... maybe. But 99.9999% of the times you should not even try to use const_cast.
Topic archived. No new replies allowed.