简体   繁体   中英

C++ How to use a class in another class with initialising list

Hi I am new to constructors in C++. I have a class Auteur with the following constructor, copy constructor and destructor:

 class Auteur 
    {   public: 
            Auteur( string nom,  bool prime=false)          : nom_(nom), prime_(prime)  {}
            Auteur( const Auteur& a)=delete;    
            ~Auteur()   {}                  
        private:
            string nom_;
            bool prime_;
    };

Now I want to create a new class that uses the class Autheur along with 2 other parameters. I tried like this but it didn`t work. Does anybody have an idea how to write the correct constructor for Oeuvre??

 class Oeuvre
    {   public: 
            Oeuvre(string titre,   Auteur const& auteur_, string langue)
            :titre_(titre), **auteur(nom, prime)**,langue_(langue)  


            Oeuvre(Oeuvre const& o) =delete;
            :titre_(o.titre_), auteur_(o.auteur_),langue_(o.langue_)        {}

            ~Oeuvre()   {}  

            public:
                Auteur auteur_;
                Auteur auteur;
                string langue_;
                string titre_;
        };

Thank you for your answers. I don`t want to change the copy constr. But I still have a problem when I try to create a new Oeuvre in main:

int main()
{
  Auteur a1("Victor Hugo"),
         a3("Raymond Queneau", true);

  Oeuvre o1("Les Misérables"  , a1, "français" ),
         o2("L'Homme qui rit" , a1, "français" );

return 0;
}

Since the program needs 4 Parameters for the constructor (Oeuvre) and in the main() I create some Objects with only 3 Parameters I get an error. ( I don`t want to change the main() )

What can I do about this?

Thanks for your answers.

Your Oeuvre constructor wants to initialize the Auteur object by copying the passed auteur . However, you have deleted the compiler generated copy-constructor.

You have 2 alternatives:

  • Enable the copy constructor and initialize the Auteur by copy
  • Use the user defined Auteur constructor by forwarding the arguments.

The second option looks like this:

Oeuvre(string titre,  string nom, bool prime, string langue)
        :titre_(titre), auteur_(nom, prime),langue_(langue)  
{}

My answer would be to delete ( I mean literally delete your copy constructor declarations), and simply allow the default ones to work. Both classes have attributes whose types can already be copy constructed or assigned. Therefore, I am confused as to why you ever needed to delete the copy constructor in the first place.

class Auteur 
    {   public: 
            Auteur( string nom,  bool prime=false)          : nom_(nom), prime_(prime)  {}
            // Get rid of the below statement
            /*Auteur( const Auteur& a)=delete;    */
            ~Auteur()   {}                  
        private:
            // types are easily copyable so there is no reason to define your own or delete 
            // the defaults
            string nom_; // string can easily deep copy itself
            bool prime_; // bool is built in type
    };

The same is true for the other class. Simply allow them to be copyable by use of the defaults.

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