0

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

4

1 回答 1

3

从您的代码的部分阅读中 - 您在WrapperMultiContainer

看,如果你不写复制构造函数,就会使用默认值。这将复制指针。而且由于现在两个类将具有相同的指针,并且都将在析构函数上将其删除-您会遇到分段错误。

但同样,只阅读了一半的代码。

编辑:operator=出于同样的原因也做一个。

于 2013-09-25T15:32:03.707 回答