Implementing the singleton pattern [closed]
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};
int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}
My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?
c++
closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};
int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}
My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?
c++
closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485
If this question can be reworded to fit the rules in the help center, please edit the question.
1
createInstance is not a static function so you cant call it without an object.
– user1438832
Jan 1 at 5:22
ohhhhhhh, thank you
– LuckyBlue
Jan 1 at 5:24
I am abit drunk, but thank you
– LuckyBlue
Jan 1 at 5:24
How come is this a singleton pattern?
– Kunal Puri
Jan 1 at 5:27
It is beginning to look like a singleton pattern
– LuckyBlue
Jan 1 at 5:30
|
show 1 more comment
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};
int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}
My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?
c++
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};
int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}
My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?
c++
c++
asked Jan 1 at 5:20


LuckyBlueLuckyBlue
24
24
closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485
If this question can be reworded to fit the rules in the help center, please edit the question.
1
createInstance is not a static function so you cant call it without an object.
– user1438832
Jan 1 at 5:22
ohhhhhhh, thank you
– LuckyBlue
Jan 1 at 5:24
I am abit drunk, but thank you
– LuckyBlue
Jan 1 at 5:24
How come is this a singleton pattern?
– Kunal Puri
Jan 1 at 5:27
It is beginning to look like a singleton pattern
– LuckyBlue
Jan 1 at 5:30
|
show 1 more comment
1
createInstance is not a static function so you cant call it without an object.
– user1438832
Jan 1 at 5:22
ohhhhhhh, thank you
– LuckyBlue
Jan 1 at 5:24
I am abit drunk, but thank you
– LuckyBlue
Jan 1 at 5:24
How come is this a singleton pattern?
– Kunal Puri
Jan 1 at 5:27
It is beginning to look like a singleton pattern
– LuckyBlue
Jan 1 at 5:30
1
1
createInstance is not a static function so you cant call it without an object.
– user1438832
Jan 1 at 5:22
createInstance is not a static function so you cant call it without an object.
– user1438832
Jan 1 at 5:22
ohhhhhhh, thank you
– LuckyBlue
Jan 1 at 5:24
ohhhhhhh, thank you
– LuckyBlue
Jan 1 at 5:24
I am abit drunk, but thank you
– LuckyBlue
Jan 1 at 5:24
I am abit drunk, but thank you
– LuckyBlue
Jan 1 at 5:24
How come is this a singleton pattern?
– Kunal Puri
Jan 1 at 5:27
How come is this a singleton pattern?
– Kunal Puri
Jan 1 at 5:27
It is beginning to look like a singleton pattern
– LuckyBlue
Jan 1 at 5:30
It is beginning to look like a singleton pattern
– LuckyBlue
Jan 1 at 5:30
|
show 1 more comment
2 Answers
2
active
oldest
votes
static
is missing.
but you don't implement singleton pattern currently, if you want to, use Meyers' one:
class PersonA
{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
PersonA(const PersonA&) = delete;
PersonA& operator =(const PersonA&) = delete;
static PersonA& getInstance(){
static PersonA instance{};
return instance;
}
};
But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:
class PersonA{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
std::unique_ptr<PersonA> create()
{
std::cout << "Instance created" << std::endl;
return std::make_unique<PersonA>();
}
};
add a comment |
you can do it like -
#include <iostream>
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
static PersonA* p;
public:
static PersonA* createInstance(){
if(p == nullptr)
{
cout << "Instance created" << endl;
p= new PersonA();
}
return p;
};
};
PersonA* PersonA::p=nullptr;
int main()
{
PersonA* A = PersonA::createInstance();
PersonA* B = PersonA::createInstance();
return 0;
}
Thank you thank you, god bless. Happy new year
– LuckyBlue
Jan 1 at 5:31
Attempting to code when drunk is not good
– LuckyBlue
Jan 1 at 5:31
But happy new year anyway!!
– LuckyBlue
Jan 1 at 5:31
BIG UP MAN!!!!!!!
– LuckyBlue
Jan 1 at 5:42
You will also want to delete the copy constructor and assignment operator.
– user4581301
Jan 1 at 5:44
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
static
is missing.
but you don't implement singleton pattern currently, if you want to, use Meyers' one:
class PersonA
{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
PersonA(const PersonA&) = delete;
PersonA& operator =(const PersonA&) = delete;
static PersonA& getInstance(){
static PersonA instance{};
return instance;
}
};
But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:
class PersonA{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
std::unique_ptr<PersonA> create()
{
std::cout << "Instance created" << std::endl;
return std::make_unique<PersonA>();
}
};
add a comment |
static
is missing.
but you don't implement singleton pattern currently, if you want to, use Meyers' one:
class PersonA
{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
PersonA(const PersonA&) = delete;
PersonA& operator =(const PersonA&) = delete;
static PersonA& getInstance(){
static PersonA instance{};
return instance;
}
};
But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:
class PersonA{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
std::unique_ptr<PersonA> create()
{
std::cout << "Instance created" << std::endl;
return std::make_unique<PersonA>();
}
};
add a comment |
static
is missing.
but you don't implement singleton pattern currently, if you want to, use Meyers' one:
class PersonA
{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
PersonA(const PersonA&) = delete;
PersonA& operator =(const PersonA&) = delete;
static PersonA& getInstance(){
static PersonA instance{};
return instance;
}
};
But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:
class PersonA{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
std::unique_ptr<PersonA> create()
{
std::cout << "Instance created" << std::endl;
return std::make_unique<PersonA>();
}
};
static
is missing.
but you don't implement singleton pattern currently, if you want to, use Meyers' one:
class PersonA
{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
PersonA(const PersonA&) = delete;
PersonA& operator =(const PersonA&) = delete;
static PersonA& getInstance(){
static PersonA instance{};
return instance;
}
};
But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:
class PersonA{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
std::unique_ptr<PersonA> create()
{
std::cout << "Instance created" << std::endl;
return std::make_unique<PersonA>();
}
};
edited Jan 1 at 17:15
answered Jan 1 at 9:12
Jarod42Jarod42
118k12103188
118k12103188
add a comment |
add a comment |
you can do it like -
#include <iostream>
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
static PersonA* p;
public:
static PersonA* createInstance(){
if(p == nullptr)
{
cout << "Instance created" << endl;
p= new PersonA();
}
return p;
};
};
PersonA* PersonA::p=nullptr;
int main()
{
PersonA* A = PersonA::createInstance();
PersonA* B = PersonA::createInstance();
return 0;
}
Thank you thank you, god bless. Happy new year
– LuckyBlue
Jan 1 at 5:31
Attempting to code when drunk is not good
– LuckyBlue
Jan 1 at 5:31
But happy new year anyway!!
– LuckyBlue
Jan 1 at 5:31
BIG UP MAN!!!!!!!
– LuckyBlue
Jan 1 at 5:42
You will also want to delete the copy constructor and assignment operator.
– user4581301
Jan 1 at 5:44
|
show 1 more comment
you can do it like -
#include <iostream>
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
static PersonA* p;
public:
static PersonA* createInstance(){
if(p == nullptr)
{
cout << "Instance created" << endl;
p= new PersonA();
}
return p;
};
};
PersonA* PersonA::p=nullptr;
int main()
{
PersonA* A = PersonA::createInstance();
PersonA* B = PersonA::createInstance();
return 0;
}
Thank you thank you, god bless. Happy new year
– LuckyBlue
Jan 1 at 5:31
Attempting to code when drunk is not good
– LuckyBlue
Jan 1 at 5:31
But happy new year anyway!!
– LuckyBlue
Jan 1 at 5:31
BIG UP MAN!!!!!!!
– LuckyBlue
Jan 1 at 5:42
You will also want to delete the copy constructor and assignment operator.
– user4581301
Jan 1 at 5:44
|
show 1 more comment
you can do it like -
#include <iostream>
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
static PersonA* p;
public:
static PersonA* createInstance(){
if(p == nullptr)
{
cout << "Instance created" << endl;
p= new PersonA();
}
return p;
};
};
PersonA* PersonA::p=nullptr;
int main()
{
PersonA* A = PersonA::createInstance();
PersonA* B = PersonA::createInstance();
return 0;
}
you can do it like -
#include <iostream>
using namespace std;
class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};
static PersonA* p;
public:
static PersonA* createInstance(){
if(p == nullptr)
{
cout << "Instance created" << endl;
p= new PersonA();
}
return p;
};
};
PersonA* PersonA::p=nullptr;
int main()
{
PersonA* A = PersonA::createInstance();
PersonA* B = PersonA::createInstance();
return 0;
}
answered Jan 1 at 5:29
user1438832user1438832
39318
39318
Thank you thank you, god bless. Happy new year
– LuckyBlue
Jan 1 at 5:31
Attempting to code when drunk is not good
– LuckyBlue
Jan 1 at 5:31
But happy new year anyway!!
– LuckyBlue
Jan 1 at 5:31
BIG UP MAN!!!!!!!
– LuckyBlue
Jan 1 at 5:42
You will also want to delete the copy constructor and assignment operator.
– user4581301
Jan 1 at 5:44
|
show 1 more comment
Thank you thank you, god bless. Happy new year
– LuckyBlue
Jan 1 at 5:31
Attempting to code when drunk is not good
– LuckyBlue
Jan 1 at 5:31
But happy new year anyway!!
– LuckyBlue
Jan 1 at 5:31
BIG UP MAN!!!!!!!
– LuckyBlue
Jan 1 at 5:42
You will also want to delete the copy constructor and assignment operator.
– user4581301
Jan 1 at 5:44
Thank you thank you, god bless. Happy new year
– LuckyBlue
Jan 1 at 5:31
Thank you thank you, god bless. Happy new year
– LuckyBlue
Jan 1 at 5:31
Attempting to code when drunk is not good
– LuckyBlue
Jan 1 at 5:31
Attempting to code when drunk is not good
– LuckyBlue
Jan 1 at 5:31
But happy new year anyway!!
– LuckyBlue
Jan 1 at 5:31
But happy new year anyway!!
– LuckyBlue
Jan 1 at 5:31
BIG UP MAN!!!!!!!
– LuckyBlue
Jan 1 at 5:42
BIG UP MAN!!!!!!!
– LuckyBlue
Jan 1 at 5:42
You will also want to delete the copy constructor and assignment operator.
– user4581301
Jan 1 at 5:44
You will also want to delete the copy constructor and assignment operator.
– user4581301
Jan 1 at 5:44
|
show 1 more comment
1
createInstance is not a static function so you cant call it without an object.
– user1438832
Jan 1 at 5:22
ohhhhhhh, thank you
– LuckyBlue
Jan 1 at 5:24
I am abit drunk, but thank you
– LuckyBlue
Jan 1 at 5:24
How come is this a singleton pattern?
– Kunal Puri
Jan 1 at 5:27
It is beginning to look like a singleton pattern
– LuckyBlue
Jan 1 at 5:30