Reputation: 520
I have adapted a custom wrapper to contain diverse type of DataTypes, but when i make an std::make_pair(customClass,customClass)
it crashes, I have debugged using gdb but I do not see the problem, besides I declared this variables and I tested if it created and it have values.
I have digg into the std::make_pair()
function and what it does it only construct the std::pair object, but my class are declared and not are pointers. i dont see the problem...here is the code
#include <iostream>
#include <string>
#include <vector>
#include <memory>
class any_type
{
public:
virtual ~any_type() {}
virtual void print() = 0;
};
template <class T>
class concrete_type : public any_type
{
public:
concrete_type(const T& value) : value_(value)
{}
virtual void print()
{
std::cout << value_ << '\n';
}
T & get()
{
return dynamic_cast<concrete_type<T>&>(*this).value_;
}
//recently added
concrete_type(const concrete_type<T>& other) : value_(other.value_)
{}
T value_;
private:
};
class WrapperMultiContainner
{
public:
WrapperMultiContainner():mAnyType(0)
{
mAnyType=new concrete_type<int>(-1);
}
//recently added
WrapperMultiContainner(const WrapperMultiContainner & aCopy)
{
//recently added
mAnyType=new concrete_type<decltype(*(aCopy.mAnyType))>(*(aCopy.mAnyType));
//*mAnyType=aCopy.mAnyType;
}
const WrapperMultiContainner & operator=(const WrapperMultiContainner &)
{ return *this;}
template<typename T>
WrapperMultiContainner(T const & aValue= T()):mAnyType(0)
{
mAnyType=new concrete_type<T>(aValue);
}
~WrapperMultiContainner()
{
delete mAnyType;
}
template<typename T>
T & get()
{
return dynamic_cast<concrete_type<T>&>(*mAnyType).value_;
}
template<typename T>
void get(T & aValue,
int & aErrorCode)
{
try{
aValue=dynamic_cast<concrete_type<T>&>(*mAnyType).value_;
aErrorCode=0;
}
catch(...)
{
aErrorCode=-1;
}
//return dynamic_cast<concrete_type<T>&>(*mAnyType).value_;
}
any_type * getAnyType() const
{
return mAnyType;
}
template<typename T>
void set(T const & aGenericValue = T())
{
if(mAnyType)
{
delete mAnyType;
mAnyType=0;
}
mAnyType=new concrete_type<T>(aGenericValue);
}
private:
any_type * mAnyType;
};
int main()
{
std::cout<<"creando el opciones para el builder de comandos"<<std::endl;
//Creacion de las estructuras que tienen las opciones para la creacion de los comandos
std::string aKeyName("idcompdestiny");
WrapperMultiContainner aKey(aKeyName);
aKey.getAnyType()->print();
WrapperMultiContainner aValue(3000);
aValue.getAnyType()->print();
std::pair<WrapperMultiContainner,WrapperMultiContainner> aPair;
aPair=std::make_pair(aKey,aValue);
return 0;
}
The line that create the std::make_pair crash. Thx ind advance!
PD: I have added the copy constructor but still crash
Upvotes: 0
Views: 1636
Reputation: 2934
From partial read of your code - you are missing a copy constructor in WrapperMultiContainer
See, if you don't write the copy constructor, the default will be used. This copies the pointer. And since now two classes will have the same pointer and both will delete it on destructor - you get segmentation fault.
But again, only read half the code.
Edit: also do an operator=
for the same reasons.
Upvotes: 3