简体   繁体   中英

how to assign a shared pointer of type structure to a shared pointer of class type

In the below function, info is a class which contains getHDDList function and HDDList is a structure. but when I assign the output of getHDDList to hdd, it says no operator "=" matches this operands :

HDDList* smc_getHDDList(Authorization* autho)
{
    HDDList retVal;
    //HDDList* hdd = new HDDList;
    std::shared_ptr<info> inf (new info(autho));
    std::shared_ptr<HDDList> hdd ;
    try
    {
        //inf.get()->getHDDList();
        //hdd = inf.get()->getHDDList();
    }
    catch (const std::invalid_argument& e)
    {
        THROW_EXCEPTION
    }
    return hdd;
}

this is my HDDList structure :


struct HDDList
{
    const char* name;
    double totalSize;
    double freeSize;
    double usedSize;
};

and this is getHDDList function :


HDDList* info::getHDDList()
{
    int num;
    num = std::stoi(numberOfHDDs());
    HDDList* list = new HDDList[num];
    for (int i = 0; i < num; i++)
    {
        list[i].name = listOfHDDs(i);
    }
    return list;
}

I have used shared pointer because when I was using raw pointers and delete pointers before returning, I had problem in receiving the structure( it didn't return first member of structure).
How can I assign getHDDList 's output to hdd ?

Could probably try something like :

hdd.reset(inf->getHDDList());

You'd need to update the return type if you take this route.

There are a couple of issues with this code, so let me see if we can get this right.

First of all, in your struct HDDList you have a const pointer name; where does this point to?

I think you would be better off if you declared it as a std::string so that it is self-contained when the node is destroyed.

struct HDDList
{
  std::string name;
  double totalSize;
  double freeSize;
  double usedSize;
};

Otherwise if the name points to some allocated string you will need to handle that as well a destructor in HHDList or in a custom deleter for unique_ptr.

You want to create an array of HDDList in your function getHDDLists()

HDDList* info::getHDDList() {...}

Notice that you are not returning the size of the array. So the caller has no way of knowing how long the array is because it just gets a pointer to where the array starts.

(If you don't have some secret marker in your data that you haven't mentioned).

So let's add the size and return an array wrapped in a unique_ptr:

std::unique_ptr<HDDList[]> info::getHDDList(int& num)
{
  num = std::stoi(numberOfHDDs()); 
  auto list = std::make_unique<HDDList[]>(num);
  for (int i = 0; i < num; ++i)
  {
    list[i].name = listOfHDDs(i);
  }
  return list;
}  

In your caller function you have now the information of how big the array plus the array wrapped in a smart pointer:

HDDList* smc_getHDDList(Authorization* autho)
{
  auto inf = std::make_unique<info>(autho);
  std::unique_ptr<HDDList[]> hdd;
  try
  {
    int hddSize{0};
    hdd = inf->getHDDList(hddSize);
    ...
  }
  catch (const std::invalid_argument& e)
  {
    // write out exception
    // rethrow if you want
  }
  return hdd;
}  

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