I am a bit perplexed. I am creating a custom "Variant" class, but am running into a strange issue with std::string member.
When I try to assign it from another Variant instance, I get an exception that the string is a bad pointer. I am not sure I understand why this would happen because the string should be an instance when the class is constructed.
Here is my code:
class Variant : ObjectBase {
public:
Variant() {};
Variant(const Value& node) { parse(node); };
ValueType Type = ValueType::Unknown;
Variant& operator= (bool value)
{ Type = ValueType::Boolean; value_.vBool = value; return *this; };
Variant& operator= (std::string value)
{ Type = ValueType::String; value_string_ = value; return *this; };
Variant& operator= (double value)
{ Type = ValueType::Double; value_.vDouble = value; return *this; };
Variant& operator= (int32_t value)
{ Type = ValueType::Int32; value_.vInt32 = value; return *this; };
Variant& operator= (int64_t value)
{ Type = ValueType::Int64; value_.vInt64 = value; return *this; };
Variant& operator= (uint32_t value)
{ Type = ValueType::Uint32; value_.vUint32 = value; return *this; };
Variant& operator= (uint64_t value)
{ Type = ValueType::Uint64; value_.vUint64 = value; return *this; };
Variant& operator= (const Value& node) { parse(node); };
private:
union Any {
int32_t vInt32;
int64_t vInt64;
double vDouble;
bool vBool;
uint32_t vUint32;
uint64_t vUint64;
Any() : vDouble(0) {};
} value_;
std::string value_string_;
};
The exception occurs when I do this:
Variant v1 = "Hello";
Variant v2 = v1; // <--- Here is where it occurs
Now, if I create a manual = operator overload, I can see that v2 value is a bad pointer, but I am not sure why. Here is my overload:
Variant& operator= (const Variant value) {
value_ = value.value_;
value_string_ = value.value_string_;
Type = value.Type;
return *this;
}
Maybe I am tired or something, but it is not obvious to me what am I missing here.
Aucun commentaire:
Enregistrer un commentaire