c++ - Use of const pointer to class in function -
i using std::shared_ptr liberally in code. have few functions want call myclass using "this" have declared these functions (for example)
int anotherclass::foo(const myclass *obj) { }
i want const make clear obj not going changed since passing raw pointer. inside foo have
int anotherclass::foo(const myclass *obj) { int number = obj->num_points()-1; }
and compiler complaining "the object has type qualifiers not compatible member function". num_points simple function declared , defined in header:
class myclass { public: ... int num_points(){return _points.size();} ... private: std::vector<mypoint> _points; };
what's best way around this? can rid of const requirement in foo rigidity imposes. many in advance!
make member function const
:
int num_points() const // <--- { return _points.size(); }
this way can call on const
objects. in habbit qualify every function doesn't alter object's state.
Comments
Post a Comment