5

Possible Duplicate:
why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const)

I have a function:

bool isCirclePolygonIntersection(const Point*, const int*, const Point*,
                                 const Point**, const int*);

and I'm trying to call it like this:

isCirclePolygonIntersection(p, &r, poly_coord, poly, &poly_size)

where poly defined like this:

Point** poly = new Point*[poly_size];

and there is a compiler error when I'm trying to compile it:

error C2664: 'isCirclePolygonIntersection' : cannot convert parameter 4 from 'Point **' to 'const Point **'
1>          Conversion loses qualifiers

from what I've learned is that you cannot give const argument to function when a function expects a non const argument, but it's fine otherwise. Does anyone knows what is the problem?? Thanks.

Community
  • 1
  • 1
MoonBun
  • 4,322
  • 3
  • 37
  • 69
  • It will work if you declare the argument `const Point *const *`. (Well, it will work in C++, not in C.) – Nemo Aug 18 '11 at 14:34

3 Answers3

4

You're correct; you can implicitly convert a T * to a const T *. However, you cannot implicitly convert a T ** to a const T **. See this from the C FAQ: http://c-faq.com/ansi/constmismatch.html.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4

An implicit conversion from Point** to const Point** would open a hole in the type system, hence there is no such conversion in C++.

For details, see Why am I getting an error converting a Foo**Foo const**?

Changing const Point** to const Point * const * will fix your problem.

By the way, why the additional indirection? That is, why do you use an array of pointers? I would suggest a std::vector<Point> instead. Or is Point a polymorphic base class?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
2

Parashift has a great explanation on why this is prohibited:

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17

David V
  • 11,531
  • 5
  • 42
  • 66