Help with these warnings. [Lot]

I have a set of code that mimics the cataloging system of a base library. There is a base class called items, which defines common identifiers id, title and year and 3 other derived classes (DVD, Book and CD).

Base [Elements]

Produced [DVD, book, CD].

Programs start, however I get the following warnings, I'm not sure how to fix them.

> "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
+5
source share
3 answers

When you declare member variables in a class, they are initialized in the order in which you declare them. But you can write them in any order in the list of constructor initializers. For instance,

struct foo {
   int a;
   int b;

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

a, b, , .

, . ,

struct foo {
    int a;
    int b;

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

a undefined.

+28

. , , , : -

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

    private:
        member1 member1_;
        member2 member2_;
}

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

. , ++ , (base1, base2), - . , , - , ( , ), .

, , , ++ , , , , - "", , . , .

+6

, , . :.

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

Foo() a b . (, , ).

+3

All Articles