Consider the method below:
template <class T>
void Entry<T>::setValue(T * value) {
int size = ?;
this->key = new T();
memcpy(this->value, value, size);
}
How I could find the length for value
? If it was a char*
i could use something like that: strlen(value)
, but it is not. What the alternstive here?
I try use this:
template <class T>
void Entry<T>::setValue(T * value) {
this->value = value;
}
but the value was staying empty after the method call.
UPDATE
I have tried this, but do not compile:
template <class T>
void Entry<T>::setValue(T * value) {
int size = sizeof(value)/sizeof(value[0]);
cout << "size = " << size << endl;
this->key = new T();
memcpy(this->value, value, size);
}
the error is:
Entry.cpp:35:13: error: cannot convert ‘JSON_Value*’ to ‘char*’ in assignment
this->key = new T()
UPDATE 2
Another try, this time the code compiles but the execution gives me a Segmentation fault in the second line:
template <class T>
void Entry<T>::setValue(T * value) {
T temp = *value;
*value = *this->value;
*this->value = temp;
}