Skip to content

Instantly share code, notes, and snippets.

@motonacciu
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save motonacciu/8c7a33efab7a19066cf8 to your computer and use it in GitHub Desktop.

Select an option

Save motonacciu/8c7a33efab7a19066cf8 to your computer and use it in GitHub Desktop.
enum class Type { TYPE1, TYPE2, TYPE3 };
class Object {
using LiteralPtr = std::unique_ptr<const char[]>;
Type m_type;
LiteralPtr m_msg;
static LiteralPtr make_str_copy(const char* str) {
const size_t size = std::strlen(str) + 1;
char* new_str = new char[size];
std::memcpy(new_str, str, size);
return LiteralPtr{new_str};
}
public:
Object(Type type, const char* msg) :
m_type{type}, m_msg{make_str_copy(msg)} { }
// non-copyable
Object(const Object&) = delete;
Object& operator=(const Object&) = delete;
// move-only semantics
Object(Object&& other) = default;
Object& operator=(Object&& obj) = default;
inline Type type() const { return m_type; }
inline const char* msg() const { return m_msg.get() }
};
int main(int argv, char* argc[]) {
std::vector<Object> src_objs;
src_objs.reserve(3);
src_objs.emplace_back( TYPE2, "1st message" );
src_objs.emplace_back( TYPE1, "2nd message" );
src_objs.emplace_back( TYPE1, "3rd message" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment