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.
Well, you delete
d the copy constructor of class Auteur
, which means that a construction like
Auteur a1("Victor Hugo");
Auteur a2(a1);
is now illegal, since a2
will be copy-constructed from a1
.
The initializer list of a class' constructor isn't different from this. You're constructing o1
with several parameters, one of which happens to be a1
. Now well, within Oeuvre
's constructor, you're initializing (ie constructing) member auteur
from it, which is using the copy constructor -- which you delete
d!
There are two ways of working around this:
1 - Don't delete
Auteur
's copy constructor. Since this class doesn't appear to manage any resource, that's fine.
2 - Give Oeuvre
's constructor the parameters it needs to properly initialize an Auteur
. I wouldn't go with this one, because it certainly doesn't concern this class to do such thing.
A couple of things:
Given those things:
#include <string>
class Auteur
{
public:
Auteur(std::string const& nom, bool prime=false)
: nom_(nom), prime_(prime)
{}
Auteur(Auteur const&) = delete;
private:
std::string nom_;
bool prime_;
};
.
class Oeuvre
{
public:
Oeuvre(std::string const& titre, std::string const& langue, Auteur& auteur)
: titre_(titre), langue_(langue), auteur_(auteur)
{ }
Oeuvre(Oeuvre const&) delete;
public:
const Auteur &auteur_;
std::string langue_;
std::string titre_;
};
.
int main()
{
Auteur a1("Victor Hugo"),
a3("Raymond Queneau", true);
Oeuvre o1("Les Misérables" , "français", a1),
o2("L'Homme qui rit" , "français", a1);
return 0;
}
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.