mardi 25 février 2020

C++ What syntax for avoid duplicated declarations?

I'm learning C++ and especially OO Programming.

My program use pointers to deal with Dynamic memory allocation.

While creating my default constructor, I was boring about repeat myself with

myInt = new int;
myOtherInt = new int; 

etc..

So my question is : is there a way to write something like :

myInt, myOtherInt = new int;

Here's my constructor code :

Annonce::Annonce(string Titre, long double Prix, string Intro, string Description, vector<vector<string>> ImgUrls) {

    titre = new string;
    intro = new string;
    description = new string;
    imgUrls = new vector<vector<string>>;
    prix = new long double;

    id = new size_t;
    *id = nbAnnonces;

    *titre = std::move(Titre);
    *prix = Prix;
    *intro = std::move(Intro);
    *description = std::move(Description);
    *imgUrls = std::move(ImgUrls);

}

Aucun commentaire:

Enregistrer un commentaire