0

I'm trying to initialize an instance of a class called "Winery" using an initialization list in the constructor for another class called "List." The problem is that when I hand the Winery constructor a winery to copy, it fails to copy the information.

This the header file for the Winery class:

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;
};

Here is the relevant part of the header file for my List class:

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

Here is the constructor in my List class:

List::Node::Node(const Winery& winery) :
    item(winery.getName(),
         winery.getLocation(),
         winery.getAcres(),
         winery.getRating()),
    nextByName(nullptr),
    nextByRating(nullptr)
{
}

From what I see, it looks like I'm doing everything I need to be doing. The data members of the winery that I'm passing to the constructor are private, so I'm trying to get them via the functions that get information. They're in the proper order and everything.

The pointers work just fine after I initialize them, but the information isn't there, so I really don't know what to do here. If you're wondering, this is for an assignment and we have to use initialization lists (I've tried it without them and that doesn't work either so I really don't know what to do).

Here is my Winery constructor:

Winery::Winery(const char * const name,
               const char * const location,
               const int acres,
               const int rating) :
    acres(acres),
    rating(rating)
{
    char *newName = new char[sizeof(name) + 1];
    char *newLocation = new char[sizeof(location) + 1];
}
2
  • You have to define what you mean by "fails to copy the info" as this could mean anything. What does the destructor look like? My guess is that the destructor is deleting the newName and newLocation strings. Is there a reason you're not using const char* instead of std::string? Commented Oct 11, 2014 at 3:10
  • I mean that when the constructor has gone through the initialization list, I look at item, nextByName, and nextByRating in memory. nextByName and nextByRating have been initialized to NULL, but all four parts of item still have the Visual Studio default memory values in them (0xcdcdcdcd, 0xfeeffeef, etc.) And the reason I'm using const char* is because we have been disallowed from using std::string. Commented Oct 11, 2014 at 3:20

1 Answer 1

2

From the looks of it, these lines:

char *newName = new char[sizeof(name) + 1];
char *newLocation = new char[sizeof(location) + 1];

do essentially nothing, as the location and name strings are not assigned or even written, which is probably the root of the problem. Your acres and rating should have been properly constructed, however.

Here is a working version I've created (also at Ideone):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class Winery
{
public:
    Winery(const char * const name, const char * const location, const int acres, const int rating) :
        name(strdup(name)),
        location(strdup(location)),
        acres(acres),
        rating(rating)
    {
    }

    virtual ~Winery(void)
    {
        free(name);
        free(location);
    }

    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;
};

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

Node::Node(const Winery& winery) :
    item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating())
{
}

int main()
{
    Winery winery("Mission Hill Winery", "Kelowna, BC, Canada", 646, 4);

    Node node(winery);

    printf("%s\n", node.item.getName());
    printf("%s\n", node.item.getLocation());
    printf("%i\n", node.item.getAcres());
    printf("%i\n", node.item.getRating());
}

Output:

Mission Hill Winery
Kelowna, BC, Canada
646
4
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.