I see the use of a Singleton pointer with online examples, so when I created my own Singleton without a pointer I became very confused as to why every example uses a pointer.
Why is a pointer better than a reference?
Here's my code:
class SimpleSingleton{
private:
static int integerDataMember;
static SimpleSingleton singletonInstance;
SimpleSingleton(){}
public:
static SimpleSingleton GetInstance(){
return singletonInstance;
}
void IncrementDataMember() const { integerDataMember++; }
int GetDataMember() const { return integerDataMember; }
};
SimpleSingleton SimpleSingleton::singletonInstance;
int SimpleSingleton::integerDataMember = 0;
int main()
{
SimpleSingleton::GetInstance().IncrementDataMember();
cout << SimpleSingleton::GetInstance().GetDataMember() << endl;
{
SimpleSingleton::GetInstance().IncrementDataMember();
cout << SimpleSingleton::GetInstance().GetDataMember() << endl;
}
return 0;
}
Related
I'm just getting started with Object Oriented Programming. I'm trying to access member function of two different classes within a template function. I have restricted access to member functions based on boolean flag isAggregateElement. For some reason, Compiler throws error stating that there is no such member function.
class descriptor{
public:
int getName(){
return -5;
}
};
class aggregate{
public:
int getDescription() {
return 234;
}
int getUnit(){
return 1;
}
};
template <typename T>
void buildObjectInfo(const T& classMemberType, const bool& isDataInterface){
T baseTypeElement = classMemberType;
bool isAggregateElement = !isDataInterface;
if(isAggregateElement){
cout<<baseTypeElement.getUnit()<<endl;
} else {
cout<<baseTypeElement.getName()<<endl; // Error gets resolved if I remove the else construct
}
}
int main()
{
aggregate a;
descriptor d;
buildObjectInfo<aggregate>(a,false);
return 0;
}
What should I do to access getUnit() without deleting boolean condition (or) removing else construct in the template function ?
Both branches must be valid. Suppose you call buildObjectInfo(d,false), what should happen then?
You can use constexpr if to discard the false branch.
Note that the getters should be const methods. The template argument can be deduced from the function parameter and you do not need the bool:
#include <iostream>
#include <type_traits>
struct descriptor{
int getName() const { return -5; }
};
struct aggregate{
int getDescription() const { return 234; }
int getUnit() const { return 1; }
};
template <typename T>
void buildObjectInfo(const T& t){
if constexpr(std::is_same_v<aggregate,T>) {
std::cout << t.getUnit() << '\n';
} else {
std::cout << t.getName() << '\n';
}
}
int main() {
aggregate a;
descriptor d;
buildObjectInfo(a);
buildObjectInfo(d);
}
However, for only 2 different types an overloaded function is much simpler:
#include <iostream>
struct descriptor{
int getName() const { return -5; }
};
struct aggregate{
int getDescription() const { return 234; }
int getUnit() const { return 1; }
};
void buildObjectInfo(const aggregate& t) {
std::cout << t.getUnit() << '\n';
}
void buildObjectInfo(const descriptor& t) {
std::cout << t.getName() << '\n';
}
int main() {
aggregate a;
descriptor d;
buildObjectInfo(a);
buildObjectInfo(d);
}
My goal is to separate data from various implementations. I don't want my things to know what actual subclass it is they are working with, either way around. To make things perform only a single task with minimal information.
I'll throw some code in your eyes first.
// Example program
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <functional>
class Model
{
public:
virtual bool set(int p_attrId, int p_value) {return false;};
virtual bool get(int p_attrId, int & p_value) const {return false;};
};
class Derived: public Model
{
static constexpr int c_classId = 1;
int value = 1;
public:
enum EAttrs
{
eAttr1 = c_classId * 1000
};
virtual bool set(int p_attrId, int p_value) override
{
switch(p_attrId)
{
case eAttr1:
value = p_value;
return true;
default:
return Model::set(p_attrId, p_value);
}
}
virtual bool get(int p_attrId, int & p_value) const override
{
switch(p_attrId)
{
case eAttr1:
p_value = value;
return true;
default:
return Model::get(p_attrId, p_value);
}
}
};
// GuiTextBoxComponent.h
// no includes to any class derived from model
class GuiTextBoxComponent
{
std::weak_ptr<Model> m_model;
int m_attrId;
public:
void draw()
{
auto locked = m_model.lock();
if(locked)
{
int value;
bool result = locked->get(m_attrId, value);
if(!result)
{
std::cout << "Failed to get attribute " << m_attrId << "\n";
return;
}
std::cout << "AttrID: " << m_attrId << " Value: " << value << "\n";
}
else
{
std::cout << "Model is dead\n";
}
}
void setSource(std::weak_ptr<Model> p_model, int p_attrId)
{
m_model = p_model;
m_attrId = p_attrId;
}
};
int main()
{
std::shared_ptr<Model> model (new Derived);
GuiTextBoxComponent textbox;
textbox.setSource(model, Derived::eAttr1);
textbox.draw();
}
The motivation behind this is acquisition of all data from a single interface.
I need to be able to add functionality like the GuiTextBoxComponent, without #include "Derived1.h" in its header.
The challenge with this design is that the Model interface needs to implement all types required from anywhere in the program.
How would you extend the types provided?
Is there some other design that could be used to achieve similar results?
Generally, I think this is an XY problem but here is how you can beautify your code a bit. First, I implemented two interfaces: Getter and Setter like:
enum class EAttrs {
eAttr1
};
template <typename GetterImpl>
struct Getter {
bool get(EAttrs const attrId, int& value) {
switch (attrId) {
case EAttrs::eAttr1:
return static_cast<GetterImpl*>(this)->get(value);
default:
return false;
}
}
};
template <typename SetterImpl>
struct Setter {
bool set(EAttrs const attrId, int value) {
switch (attrId) {
case EAttrs::eAttr1:
return static_cast<SetterImpl*>(this)->set(value);
default:
return false;
}
}
};
Here I used CRTP, i.e. static polymorphism. Then implementation of your derived classes is a bit simpler:
class Derived1 : public Getter<Derived1>, Setter<Derived1> {
int value = 1;
public:
bool set(int p_value) {
value = p_value;
return true;
}
bool get(int & p_value) {
p_value = value;
return true;
}
};
class Derived2 : public Getter<Derived1>, Setter<Derived1> {
int value = 2;
public:
bool set(int p_value) {
value = p_value;
return true;
}
bool get(int & p_value) {
p_value = value;
return true;
}
};
Finally, since we were using CRTP, there is no need for creating std::unique_ptr. Code that's using above classes could look like:
template <typename T>
void printInt(Getter<T>& model, EAttrs p_attrId) {
int value;
bool result = model.get(p_attrId, value);
if (!result)
{
std::cout << "Failed to get attribute " << static_cast<int>(p_attrId) << "\n";
return;
}
std::cout << "AttrID: " << static_cast<int>(p_attrId) << " Value: " << value << "\n";
}
int main()
{
Derived1 derived1;
Derived2 derived2;
printInt(derived1, EAttrs::eAttr1);
printInt(derived2, EAttrs::eAttr1);
}
Check out the DEMO.
P.S. Note the usage of enum class instead of plain enum.
Take a look at this CppCon's talk about Solid principles. Your code might be a good example to apply those principles to.
Today I'm working on a singleton test case in c++.
The singleton is working fine but I would like to instantiate the static object when the user try to access a member of it, so if the variable isn't created when we try to access a member of it, it will not crash instead it will simply generate my singleton.
Here's my class.h:
class PDG : public EmployeRH
{
public:
static void Instantiate(std::string nom, std::string prenom);
// Current manual instantiation version of the singleton
PDG* operator->();
// This is the line I just added to overload "->" operator ... But it seems it's never called.
void SePresenter();
static PDG* _instance;
private:
PDG();
~PDG();
PDG(std::string nom, std::string prenom);
int _budget;
};
Methods.cpp
PDG* PDG::_instance=NULL;
PDG::PDG()
{
}
PDG::~PDG()
{
}
PDG::PDG(std::string a_nom, std::string a_prenom):EmployeRH(a_nom,a_prenom)
{
_budget = 100000;
}
void PDG::Instantiate(std::string a_nom, std::string a_prenom)
{
cout << "instantiation pdg" << endl;
if (_instance == NULL)
{
_instance = new PDG(a_nom,a_prenom);
}
}
PDG* PDG::operator->()
{
PDG::Instantiate("Unknown", "Unknown");
return _instance;
}
void PDG::SePresenter()
{
cout << _nom << " " << _prenom << endl;
}
main.cpp
void main()
{
PDG::_instance->SePresenter();
system("pause");
}
The thing is, it goes directly into "SePresenter()" and not into my overloaded operator "->".
If anyone could help it would be greatfull.
Thanks,
Impact
PDG::_instance is a pointer to PDG so -> simply dereferences the pointer and you can't override the behaviour. To override the -> operator you must call it on the class directly not on a pointer: (*PDG::_instance)->SePresenter(). To preserve your desired syntax and to remove the undefined behaviour from dereferencing the null pointer you can change PDG::_instance into a structure which holds your instance pointer.
#include <string>
#include <iostream>
using namespace std;
struct EmployeRH {
EmployeRH() {}
EmployeRH(std::string nom, std::string prenom) {}
std::string _nom;
std::string _prenom;
};
class PDG : public EmployeRH {
public:
static PDG* Instantiate(std::string nom, std::string prenom);
// Current manual instantiation version of the singleton
void SePresenter();
static struct Instance {
PDG* operator->()
{
return PDG::Instantiate("Unknown", "Unknown");
}
} _instance;
private:
PDG();
~PDG();
PDG(std::string nom, std::string prenom);
int _budget;
};
PDG::Instance PDG::_instance;
PDG::PDG()
{
}
PDG::~PDG()
{
}
PDG::PDG(std::string a_nom, std::string a_prenom)
: EmployeRH(a_nom, a_prenom)
{
_budget = 100000;
}
PDG* PDG::Instantiate(std::string a_nom, std::string a_prenom)
{
static PDG instance(a_nom, a_prenom);
cout << "instantiation pdg" << endl;
return &instance;
}
void PDG::SePresenter()
{
cout << _nom << " " << _prenom << endl;
}
int main()
{
PDG::_instance->SePresenter();
return 0;
}
I've also changed your singleton to use a function static which makes your code thread safe.
Working on a small role-playing game battle system to practice object-oriented code. I have a Party class, which has a vector to store a variable amount of party members.
Initializing my party and adding a member seems to work great. I'm even able to call the member's take_damage() function to change the member's hp and that seems to work too.
But when I check on the same member's hp on the next line using the hp getter, it's right back where it started.
I made a destructor for the member class to see what was going on and according to the output the object is being destroyed many times. Why is this?
class Member
{
private:
int hp;
public:
Member() { hp = 1; }
~Member() { std::cout << "DESTROYED!!" << std::endl; }
int get_hp() { return hp; }
void take_damage(int amt) { hp += amt }
};
class Party {
private:
std::vector<Member> members;
public:
void add_member(Member memb) { members.push_back(memb); }
Member get_member(int num) { return members[num]; }
};
int main() {
Party p;
Member m;
p.add_member(m);
std::cout << p.get_member(0).get_hp() << std::endl;
p.get_member(0).take_damage(4);
std::cout << p.get_member(0).get_hp() << std::endl;
}
Your get_member method returns a copy of the array element, rather than a reference to it. Return a reference, and the member will be modified.
Member& get_member(int num) { return members[num]; }
see small issue in code -=amt
also changed function of get_member
other than that, the code seems fine.
class Member
{
private:
int hp;
public:
Member() { hp = 1; }
~Member() { std::cout << "DESTROYED!!" << std::endl; }
int get_hp() { return hp; }
void take_damage(int amt) { hp -= amt } // This should be -=amt, not +=amt
};
class Party {
private:
std::vector<Member> members;
public:
void add_member(Member memb) { members.push_back(memb); }
Member& get_member(int num) { return members[num]; }
};
int main() {
Party p;
Member m;
p.add_member(m);
std::cout << p.get_member(0).get_hp() << std::endl;
p.get_member(0).take_damage(4);
std::cout << p.get_member(0).get_hp() << std::endl;
}
I'm new to singletons and generally to OOP, and i have a little problem with my code. I searched here for "Cannot call member function without object", but the engine returned some topics that aren't related to singletons.
This is my code and the compiler tells me "cannot call member function 'void mySingleton::set_value(int)' without object". Do you have any ideas on how could i solve this?
#include <iostream>
class mySingleton{
private:
int value;
static mySingleton *_pInstance;
mySingleton(int Value=0) {value=Value;}
public:
~mySingleton() {value=0;}
static mySingleton *getInstance()
{
if(!_pInstance) _pInstance=new mySingleton;
return _pInstance;
}
int get_value() {return value;}
void set_value(int x=0) {value=x;}
};
mySingleton *mySingleton::_pInstance=NULL;
int main()
{
std::cout << "Initial value:" << mySingleton::getInstance()->get_value() << std::endl;
mySingleton::set_value(5);
std::cout << "Modified value: " << mySingleton::getInstance()->get_value() << std::endl;
return 0;
}