简体   繁体   中英

C++ Copy construct base class pointer

Searched around and couldn't find any advice to my problem. I'm trying to make a copy constructor for a class that has a private variable that includes a pointer to an Abstract Base Class.

#include "BaseClass.hh"

ClassA::ClassA()
{ }
/* Copy construct a ClassA object */
ClassA::ClassA(const ClassA& obj)
{
    std::map<std::string, BaseClass*>::const_iterator it;
    //ClassA copy = obj;

    for(it = obj.ind.begin(); it != obj.ind.end(); it++)
    {
        copy.ind[it->first]=(it->second);
    }
}

//in .hh file
private:
std::map<std::string, BaseClass*> ind;

Am I even close? If not, how could I fix this?

There are couple of issues here.

  1. ++it; repeated in the for loop.
  2. ClassA copy = obj; Once you return from the copy constructor, the variable copy is destroyed. So, you are not doing any copy here.
  3. If you wish to put the value in the map as a pointer, then you need to allocate memory for the pointer variable.
  4. Since you have are having the value in the map as BaseClass pointer, you need to know the exact type you wish to allocate memory for. The key could help here.

I'm taking the liberty of C++11 tag here.This is just for illustration purpose. Take the idea and implement it as it fits your needs. If you observe, I did not release the memory here.Left it for you.

class BaseA
{
public:
    virtual void Print() = 0;
};

class Derived1A : public BaseA
{
    virtual void Print() 
    {
        std::cout << "Derived1A\n";
    }
};

class Derived2A : public BaseA
{
    virtual void Print() 
    {
        std::cout << "Derived2A\n";
    }
};


std::map<std::string, std::function<BaseA*()>> factory;


class ClassA
{

public:
    ClassA()
    {
        for (auto it = factory.begin(); it != factory.end(); ++it)
        {
            typedef std::pair<const std::string, BaseA*> Pair;
            mapType_m.insert(Pair(it->first, it->second()));
        }
    }

    ClassA(const ClassA& other)
    {
        for (auto it = other.mapType_m.begin(); it != other.mapType_m.end(); ++it)
        {           
            typedef std::pair<const std::string, BaseA*> Pair;
            mapType_m.insert(Pair(it->first, factory[it->first]()));
        }
    }

    void Print()
    {
        for (auto it = mapType_m.begin(); it != mapType_m.end(); ++it)
        {
            std::cout << "key:" << it->first << "\tValue:";
            it->second->Print() ;
            std::cout << "\n";
        }
    }

private:
    std::map<std::string, BaseA*> mapType_m;

};


int main()
{
    factory["Derived1A"] = []() { return new Derived1A(); };
    factory["Derived2A"] = []() { return new Derived2A(); };


    ClassA c1;
    ClassA c2 = c1;
    c2.Print();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM