简体   繁体   中英

C++ initialization list in constructor

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 info.

This the 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 info 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). I would greatly appreciate any help! Thank you!

EDIT: 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];
}

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 (ideone here -> http://ideone.com/v98zpq )

#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

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