C ++ member function constant for C shell

I have an object that at the most basic level looks like this:

#include <X11/Xlib.h> class x_link { public: x_link() { display_ = XOpenDisplay(NULL); } ~x_link() { XCloseDisplay(display_); } Display* display_ptr() const { return display_; } private: Display* display_; }; 

I was wondering how "const" x_link::display_ptr() should be in that case.

this old question if member functions are "const" if they affect a boolean state but not a bitwise state? , it seems that since my method does not work, t (itself) affects the logical or bitwise state of the object, const is the way to go.

but at the same time, providing Display* allows users to tear an object (for example, by calling XCloseDisplay() themselves), which would be very inconvenient.

any thoughts?

+4
source share
1 answer

This class looks like a simple wrapper class, the purpose of which is primarily to port the C interface. In this case, I advise you not to complicate your program using const.

I reserve the use of constants for clear cut cases when an object or function is read-only.

Const is one of many C ++ features that often trick programmers into making their programs unnecessarily complex.

+1
source

All Articles