문제

I have a simple question, is it a good practise to implement getters and setters in the header file, like this ?

class WebsocketSession : public boost::enable_shared_from_this<WebsocketSession>{
public:
    WebsocketSession(boost::asio::io_service& io_service, WebsocketServer& server);

    tcp::socket& getSocket() { return socket_; } // <---  This
private:
    tcp::socket socket_;
    WebsocketServer& server_;
};
도움이 되었습니까?

해결책

Advantage

The getters and setters will be inlined so they will have no function overhead.

Disadvantage

You will be unable to make any changes to your getter and setter without having to recompile all code using your class. This is especially bad when the implementation of your class lies in a different DLL or .so file.

I personally do not like placing getters and setters in the header files.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top