//Hi all,could someone explain the make_shared(), is it used to substitute constructor?
//Here is an example
void DoAddStaff(vector<Ptr>& container)
{
cout << "\nEnter type of employee to add ('W' for waged, 'S' for salaried): ";
char c;
cin >> c;
cin.ignore();
if( c == 'w' || c == 'W' )
{
cout << "\nEnter waged employee's name: ";
string name;
getline(cin, name);
cout << "\nEnter waged employee's hourly rate of pay: ";
double rate;
cin >> rate;
cin.ignore();
container.push_back( make_shared<CWagedEmployee>(name, CreateEmplNum( container ), rate) );
}
else if( c == 's' || c == 'S' )
{
cout << "\nEnter salaried employee name: ";
string name;
getline(cin, name);
cout << "\nEnter employee's salary: ";
double salary;
cin >> salary;
container.push_back( make_shared<CSalariedEmployee>(name, CreateEmplNum( container ), salary) );
}
else
cout << "Wrong type." << endl;
aluhnev
0
Junior Poster in Training
Recommended Answers
Jump to Postis it used to substitute constructor?
Yes. An exception-safe and (usually) more efficient substitute.
This function is typically used to replace the construction
std::shared_ptr<T>(new T(args...))
of a shared pointer from the raw pointer returned by a call to new.In contrast to that expression,
std::make_shared<T>
typically allocates …
All 2 Replies
Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.