帮助解决这些警告。 [遗产]

发布于 2024-08-30 21:11:44 字数 964 浏览 6 评论 0原文

我有一组代码,它模仿基本的图书馆编目系统。有一个名为 items 的基类,其中定义了常规 id、title 和year 变量以及其他 3 个派生类(DVD、Book 和 CD)。

基础[物品]

衍生[DVD、书籍、CD]。

程序运行,但是我收到以下警告,我不知道如何解决这些问题。

>"C:\Program Files\gcc\bin/g++"  -Os -mconsole -g -Wall -Wshadow -fno-common mainA4.cpp -o mainA4.exe
In file included from mainA4.cpp:5:
a4.h: In constructor `DVD::DVD(int, std::string, int, std::string)':
a4.h:28: warning: `DVD::director' will be initialized after
a4.h:32: warning:   base `Items'
a4.h:32: warning:   when initialized here
a4.h: In constructor `Book::Book(int, std::string, int, std::string, int)':
a4.h:48: warning: `Book::numPages' will be initialized after
a4.h:52: warning:   base `Items'
a4.h:52: warning:   when initialized here
a4.h: In constructor `CD::CD(int, std::string, int, std::string, int)':
a4.h:66: warning: `CD::numSongs' will be initialized after
a4.h:70: warning:   base `Items'
a4.h:70: warning:   when initialized here
>Exit code: 0

I have a set of code, which mimics a basic library cataloging system. There is a base class named items, in which the the general id,title and year variables are defined and 3 other derived classes (DVD,Book and CD).

Base [Items]

Derived [DVD,Book,CD].

The programs runs, however I get the following warnings, I'm not sure how to fix these.

>"C:\Program Files\gcc\bin/g++"  -Os -mconsole -g -Wall -Wshadow -fno-common mainA4.cpp -o mainA4.exe
In file included from mainA4.cpp:5:
a4.h: In constructor `DVD::DVD(int, std::string, int, std::string)':
a4.h:28: warning: `DVD::director' will be initialized after
a4.h:32: warning:   base `Items'
a4.h:32: warning:   when initialized here
a4.h: In constructor `Book::Book(int, std::string, int, std::string, int)':
a4.h:48: warning: `Book::numPages' will be initialized after
a4.h:52: warning:   base `Items'
a4.h:52: warning:   when initialized here
a4.h: In constructor `CD::CD(int, std::string, int, std::string, int)':
a4.h:66: warning: `CD::numSongs' will be initialized after
a4.h:70: warning:   base `Items'
a4.h:70: warning:   when initialized here
>Exit code: 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-09-06 21:11:44

当您在类中声明成员变量时,它们会按照您声明的顺序进行初始化。但是您可以在构造函数的初始值设定项列表中以任何顺序编写它们。例如,

struct foo {
   int a;
   int b;

   foo(): b(5), a(3) {}
};

将构造 athen b,即使看起来您正在以其他顺序初始化它们。

编译器会发出警告,因为您可能会欺骗自己编写不正确的代码。例如,

struct foo {
    int a;
    int b;

    foo(): b(5), a(b) {}
};

a 的值将是未定义的。

When you declare member variables in a class, they're initialized in the order you declare them. But you may write them in any order in your constructor's initializer list. For example,

struct foo {
   int a;
   int b;

   foo(): b(5), a(3) {}
};

will construct a and then b, even though it appears that you're initializing them in the other order.

The compiler issues a warning because you can trick yourself into writing incorrect code. For example,

struct foo {
    int a;
    int b;

    foo(): b(5), a(b) {}
};

the value of a will be undefined.

兔姬 2024-09-06 21:11:44

您需要查看构造函数和成员初始化列表。在不看代码的情况下很难判断,但发生的情况是您有这样的代码:-

class my_class : public base1, public base2
{
    public:
        my_class();

    private:
        member1 member1_;
        member2 member2_;
}

my_class::my_class() 
    : member2_(...)
    , member1_(...)
    , base2_(...)
    , base1_(...)
{ }

这将产生类似的警告。原因是在C++中,构造函数总是按照基类列表中所示的顺序构造基类(base1,然后是base2),然后在类定义中从上到下构造成员变量。它执行此操作时不考虑您在成员初始化列表中指定的顺序 - 该顺序将被忽略,但如果它与某些编译器(似乎包括您的编译器)不匹配,则会向您发出警告。

顺便说一句,它这样做的原因是 C++ 严格要求以与构造函数相反的顺序调用析构函数,因此,如果它按照成员初始化列表的顺序执行操作,则必须以某种方式“记住”哪个构造函数具有被调用,以便它可以按正确的顺序调用析构函数。它不会这样做,而是始终使用相同的顺序。

You need to take a look at your constructors and member initialization lists. It is tricky to tell without seeing the code, but what is happening is that you have code like this:-

class my_class : public base1, public base2
{
    public:
        my_class();

    private:
        member1 member1_;
        member2 member2_;
}

my_class::my_class() 
    : member2_(...)
    , member1_(...)
    , base2_(...)
    , base1_(...)
{ }

This will produce similar warnings. The reason is that in C++ the constructor always constructs the base classes in the order shown in the base class list (base1 followed by base2), then it constructs the member variables from top to bottom in the class definition. It does this without regard for the order you specify things in your member initialization list - this order is ignored, but if it doesn't match some compilers (including yours it seems) will warn you about it.

The reason it does this by the way is that C++ has a strict requirement that destructors be called in the reverse order of constructors, so if it did things in the order of the member initialization lists it would have to somehow "remember" which constructor had been called so that it could call the destructors in the right order. It doesn't do this, but instead just always uses the same order.

你在我安 2024-09-06 21:11:44

在构造函数中初始化类成员时,请按照声明它们的顺序对其进行初始化。例如:

class Foo {
  public:
    int a;
    int b;
    Foo() : a(0), b(0) {}
};

Foo() 构造函数中,交换 ab 的顺序会导致警告。初始化基类也是如此(如果显式调用其构造函数,则应在任何数据成员初始值设定项之前调用)。

When you initialize class members in the constructor, initialize them in the same order in which they are declared. E.g.:

class Foo {
  public:
    int a;
    int b;
    Foo() : a(0), b(0) {}
};

In Foo() constructor, switching the order of a and b would lead to your warnings. Same goes for initializing the base classes (whose constructors, if called explicitly, should be called before any data member initializers).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文