我有類似下面的代碼:如何避免這種重複
#include <string>
class A{
public:
std::string &get(){
return s;
}
const std::string &get() const{
return s;
}
std::string &get_def(std::string &def){
return ! s.empty() ? s : def;
}
// I know this might return temporary
const std::string &get_def(const std::string &def) const{
return ! s.empty() ? s : def;
}
private:
std::string s = "Hello";
};
我想知道有沒有簡單的方法來避免重複的代碼中的get()函數?
討厭吧?我很想用const或nothing創建一個宏,但它不是非常類似C++的。 –
我甚至沒有辦法在沒有const_cast的情況下重用這些函數,或者使其成爲可變的 – Nick
Ew @右對齊的&符號;) –