What is the best way of handling a code like
-
Hi, I would like to know if there is a better way of writing a particular pattern of the code as mentioned below. I have been encountering this type quite frequently and do not like the way it is implemented. The code piece is as follows class Address { public: Address():m_address("") {} Address(const CString& addr) : m_address(addr) {} CString GetAddress()------------------ (1) { return m_address; } CString GetAddress() const ------------------ (1a) { return m_address; } const CString& GetAddress() const -----(2) { return m_address; } private: CString m_address; }; Now somewhere in main method Address addr("Living somewhere on Earth"); further down a call is made into a third party library which only takes char* and has a method like void FormatAddress(char* address); -------(3) Now if I make a call to this method, I must do this FormatAddress(const_cast<char*>(addr.GetAddress().GetString()); -------------(4) So my query is whether the whole piece of code is correct, sure it does compile and gives the result as expected but I am looking to improve my programming skills and want to write a better and cleaner code. Out of the methods 1 and 1a which is more correct? Does it makes sense to make the m_address variable as mutable. Thanks and Regards :) :)
-
Hi, I would like to know if there is a better way of writing a particular pattern of the code as mentioned below. I have been encountering this type quite frequently and do not like the way it is implemented. The code piece is as follows class Address { public: Address():m_address("") {} Address(const CString& addr) : m_address(addr) {} CString GetAddress()------------------ (1) { return m_address; } CString GetAddress() const ------------------ (1a) { return m_address; } const CString& GetAddress() const -----(2) { return m_address; } private: CString m_address; }; Now somewhere in main method Address addr("Living somewhere on Earth"); further down a call is made into a third party library which only takes char* and has a method like void FormatAddress(char* address); -------(3) Now if I make a call to this method, I must do this FormatAddress(const_cast<char*>(addr.GetAddress().GetString()); -------------(4) So my query is whether the whole piece of code is correct, sure it does compile and gives the result as expected but I am looking to improve my programming skills and want to write a better and cleaner code. Out of the methods 1 and 1a which is more correct? Does it makes sense to make the m_address variable as mutable. Thanks and Regards :) :)