vendredi 25 novembre 2016

Overloading operator = trouble

I'm currently trying to simulate a folder hierarchy in C++ and I've got some issues when assigning one object to another one :

First, I've create a class, that I've named "Folder" :

class Folder{
      public:
         string name; 
         Folder * _previous;
         list<Folder> _next;

         Folder(const string name);
         Folder(const string name, Folder * _previous);
         ~Folder();

         Folder operator=(Folder F);
};

We assume that it currently doesn't need anymore attribute nor method for this project.

I'm actually trying to overload the "=" operator, which will be used in the main programm in order to update "currentFolder" (which is a Folder object, "pointing" at the current Folder object).

Here's my method :

Folder Folder::operator=(Folder F){
    name = F.name;
    this->_previous = F._previous;
    _next.clear();
    for(auto it = (F._next).begin(); it != (F._next).end(); it++)
                _next.push_back(*it);
    return (*this);
};

I've been trying a lot of things but I really have trouble in figuring out how to assign "_previous" and "_next" attributes.

I hope I've been clear and you could help me. Thanks ! Note : compilation works fine, I'm using -std=c++11 option in order to allow automatic iterator on the list.

Aucun commentaire:

Enregistrer un commentaire