Help! iterator problem

Started by
12 comments, last by kauna 10 years, 3 months ago


You cannot return a non-const pointer to internal data from a function that is const. The non-const poiner allows you to alter the internal state of the object via a member function that promised that no observable change is allowed. In this case, you're not returning a copy of an internal pointer value, you are returning a pointer to internal data.

I am having trouble finding anything in the standard to back that up. In fact, there is no way for the language to enforce that, since it would require semantic knowledge about non-local usage of a copied value.

I can see that if you do use the returned copy of the pointer to alter the internal state of a const object, you could be treading the grounds of undefined behaviour. I see nothing in C++ that disallows you from doing that.

If you have a member of type T in a class, then just make it of type const T in a const member function so you cannot convert it to a T*, only const T*, by taking its address. That would make sure that you cannot modify the internal state via the pointer returned by the const member function.

Advertisement


You cannot return a non-const pointer to internal data from a function that is const. The non-const poiner allows you to alter the internal state of the object via a member function that promised that no observable change is allowed. In this case, you're not returning a copy of an internal pointer value, you are returning a pointer to internal data.

I am having trouble finding anything in the standard to back that up. In fact, there is no way for the language to enforce that, since it would require semantic knowledge about non-local usage of a copied value.

I can see that if you do use the returned copy of the pointer to alter the internal state of a const object, you could be treading the grounds of undefined behaviour. I see nothing in C++ that disallows you from doing that.

If you have a member of type T in a class, then just make it of type const T in a const member function so you cannot convert it to a T*, only const T*, by taking its address. That would make sure that you cannot modify the internal state via the pointer returned by the const member function.

Isn't that what actually happens if you try to circumvent const protections?

struct X {
  X(int data) : data(data) {
  }
 
  int data;
 
  int *get_data() const {
    return &data;
  }
};

The compiler barfs like this:

kk.cpp: In member function ‘int* X::get_data() const’:
kk.cpp:8:13: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]

Or are we talking about something else?

Yes, that's what I'm saying. The expression &data returns a const int * in a const member function and cannot be converted to an int *. The const on the return type is necessary.

Sorry,

I didn't notice that the missing * in the original post.

If the container contained pointers to objects such as std::vector<Star*> unsortedStars; then there shouldn't be a problem.

Cheers!

This topic is closed to new replies.

Advertisement