You don't need to allocate the instance of singleton dynamically. It could look the following way (this is sometimes called "lazy loading singleton" ~ the instance is created late & kinda "automatically"):
#include <iostream>
#include <string>
class Questionnary
{
private:
// constructor taking string:
Questionnary(const std::string& name) : name_(name) { }
public:
static Questionnary& getInstance(const std::string& name)
{
static Questionnary q(name);
std::cout << "My name is: " << q.name_ << std::endl;
return q;
}
private:
std::string name_;
};
int main() {
Questionnary::getInstance("Josh");
Questionnary::getInstance("Harry");
}
output:
My name is: Josh
My name is: Josh
Note that constructor will be called only once right when the getInstance
is called for the first time.