0

I need to take a couple of dynamically allocated arrays from one instance of a class and copy them into another instance. The problem is that I've been disallowed from changing the header file and there's no opportunity to overload the assignment operator.

This is the header file for the Winery class. I'm not allowed to make any changes to this file:

class Winery
{
    public:
    Winery(const char * const name, const char * const location, const int acres, 
           const int rating);
    virtual ~Winery(void);

    const char * const getName() const { return name; }
    const char * const getLocation() const { return location; }
    const int getAcres() const { return acres; }
    const int getRating() const { return rating; }

    private:
    char    *name;
    char    *location;
    int     acres;
    int     rating;
};

Winery's constructor:

Winery::Winery(const char * const _name, const char * const _location, 
               const int acres, const int rating) :
    acres(acres),
    rating(rating)
{
    name = new char[sizeof(name) + 1];
    location = new char[sizeof(location) + 1];

    strcpy(name, _name);
    strcpy(location, _location);
}

Here's where I need to do the copy. This is using linked lists, and both headByName and newNode are structs with 1 instance of Winery (called item) and 2 pointers to other nodes:

void List::insert(const Winery& winery)
{
    if (headByName == nullptr)
    {
        Node *newNode = new Node(winery);
        headByName = newNode;   
        return; 
    }
}

Finally, the Node struct and its members. These are the only nodes I am allowed to use and I can't make any changes to this file:

private:

    struct Node
    {
        Node(const Winery& winery);     
        Winery item;                            
        Node *nextByName;               
        Node *nextByRating;             
    };

    Node *headByName;                   
    Node *headByRating;                 
};

The arrays in the instance of Winery that I'm handed are deallocated immediately after handing them to me, so I need to get the arrays out of winery and into headByName.

user3698112
  • 171
  • 2
  • 8
  • You must be taking the same class as this person: http://stackoverflow.com/questions/26321728/c-getting-access-to-nodes-in-a-linked-list-after-passing-the-list-as-an-argume – John Zwinck Oct 12 '14 at 04:15
  • 1
    Your constructor is broken; `sizeof` doesn't work with pointers: http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array – ApproachingDarknessFish Oct 12 '14 at 04:21
  • @remyabel - He wrote the header file, yes. and to ValekHalfHeart - Thanks, I didn't know that! – user3698112 Oct 12 '14 at 04:24

0 Answers0