构造函数中的 C++ 初始化列表

标签 c++ constructor ctor-initializer

我正在尝试使用另一个名为“List”的类的构造函数中的初始化列表来初始化一个名为“Winery”的类的实例。问题是,当我将一个酒厂交给 Winery 构造函数进行复制时,它无法复制信息。

这是 Winery 类的头文件:

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

这是我的 List 类头文件的相关部分:

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

这是我的 List 类中的构造函数:

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

在我看来,我似乎正在做我需要做的一切。我传递给构造函数的酿酒厂的数据成员是私有(private)的,所以我试图通过获取信息的函数来获取它们。它们的顺序和一切都是正确的。

指针在我初始化后工作得很好,但是信息不在那里,所以我真的不知道在这里做什么。如果你想知道,这是一个赋值,我们必须使用初始化列表(我试过没有它们但也不起作用所以我真的不知道该怎么做).

这是我的 Winery 构造函数:

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

最佳答案

从外观上看,这些行:

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

基本上什么都不做,因为 locationname 字符串没有分配甚至写入,这可能是问题的根源。但是,您的 acresrating 应该已正确构建。

这是我创建的一个工作版本(也是 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());
}

输出:

Mission Hill Winery
Kelowna, BC, Canada
646
4

关于构造函数中的 C++ 初始化列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26310397/

相关文章:

c++ - 更改 CMake 项目的 Qt Creator 项目名称

c++ - OpenGL Stenciling,将引用与写入的值分开?

c++ - 将 AMD OpenCL 与 mingw 结合使用

c++ - 从没有默认构造函数的虚拟基类派生类

c++ - 使用多个标志在 Boost Property Tree 1.50 中使用 read_xml() 时

c# - 如何在 C# 中访问其成员函数时防止调用一个类的构造函数?

java - 创建在其外部类的构造函数中使用的类的实例

模板方法构造函数中的 C++ 奇怪行为

c++ - 在构造函数初始化器中初始化成员数组

c++ - 我可以调用虚函数来初始化基类子对象吗?