samedi 7 octobre 2023

How to create constructor both performantly and safely?

everyone.I meet below question.

example:

struct Header
{                   
    std::string name;
    std::string value;
};

Possible usage:

std::string a = "a";
std::string a = "b";
Header("a", "b"); 
Header(a, b);
Header(a, "b");
Header("b", a);
Header(std::string("a"), b);
Header(std::string("a"), "b");
Header(std::string("a"), std::string("a"));

requirment:

  • char * --- don't change and use it to init member variable. like 'std::string m(const char *)'
  • std::string --- don't change and refrenece it to init member variable. like 'std::string m(const std::string &)'
  • std::string() --- move it to member variable. like 'std::string m(std::string &&)'

version: c++11

Aucun commentaire:

Enregistrer un commentaire