This question already has answers here:
Where are static variables stored in C and C++?
(16 answers)
Closed 9 years ago.
For below singleton class where would be the memory taken from (Stack or Global memroy)
class Singleton
{
public:
static Singleton* get()
{
static Singleton instance;
return &instance;
}
};
instance will be located in static storage (or global as you call it).
Related
This question already has answers here:
What is the purpose of the Most Vexing Parse?
(6 answers)
Closed 2 years ago.
I have a Request_manager.h file declaring an object Tickets_Queue as static in Request_manager, which is a nested class from itself.
class Request_manager {
public:
class Tickets_Queue{
private:
pthread_mutex_t m_mutex;
public:
Tickets_Queue(){};
~Tickets_Queue(){};
};
static Tickets_Queue ticket_queue;
private:
static int m_connected;
};
To initialize it, in Request_manager.cpp I write:
int Request_manager::m_connected(0);
Request_manager::Tickets_Queue Request_manager::ticket_queue();
The initialization of m_connected works but for ticket_queue it says:
gcc.archive core/bin/gcc-5.4.0/debug/link-static/threading-multi/libcore.a
gcc.compile.c++ data_interfaces/bin/gcc-5.4.0/debug/link-static/threading-multi/Request_manager.o
data_interfaces/Request_manager.cpp:17:62: error: no ‘dataserver::Request_manager::Tickets_Queue dataserver::Request_manager::ticket_queue()’ member function declared in class ‘dataserver::Request_manager’
Request_manager::Tickets_Queue Request_manager::ticket_queue();
You are calling a function.
Yeah. My mistake. As #john says it is prototyping a function.
You have to declare the type and the variable:
Request_manager::Tickets_Queue Request_manager::ticket_queue;
This question already has answers here:
C++ singleton vs. global static object
(8 answers)
Closed 5 years ago.
I know that singleton allow only one instance of an object. Each method declared in the singleton will only operate on this object.
I was wondering why to not simply declare a global object which will achieve the same goal?
I am certainly forgetting something. If singletons exist there must be specific uses or help to realize specific mechanisms.
For instance:
class Singleton
{
public:
static Singleton& Instance()
{
static Singleton sg;
return sg;
}
void function();
};
would be the same as:
class NotSingleton
{
public:
NotSingleon();
~NotSingleton()
void function();
};
NotSingleton nsg;
However, nothing prevent me to use more than one instance of NotSingleton
Singleton is used when we do not want to create more than one object. Singleton class ensures that not more than one object is created. However, having a global object doesn't ensure this.
class Singleton {
public static Singleton object = null;
public void singleton() {
if (object == null)
object = new Singleton();
return object;
}
}
This class will not create more than one object. This is the purpose of the Singleton class.
This question already has answers here:
C++11 "auto" semantics
(3 answers)
Closed 6 years ago.
I am writing a simple garbage collector in C++. I need a singleton class GarbageCollector to deal with different types of memory.
I used a Meyer's singleton pattern. But when I try to call instance, an error appears:
error: ‘GarbageCollector::GarbageCollector(const GarbageCollector&)’ is private
GarbageCollector(const GarbageCollector&);
^
Here is the class definition.
class GarbageCollector //Meyers singleton (http://cpp-reference.ru/patterns/creational-patterns/singleton/)
{
public:
static GarbageCollector& instance(){
static GarbageCollector gc;
return gc;
}
size_t allocated_heap_memory;
size_t max_heap_memory;
private:
//Copying, = and new are not available to be used by user.
GarbageCollector(){};
GarbageCollector(const GarbageCollector&);
GarbageCollector& operator=(GarbageCollector&);
};
I call the instance with the following line:
auto gc = GarbageCollector::instance();
Change
auto gc = GarbageCollector::instance();
to
auto& gc = GarbageCollector::instance();
Otherwise gc is not a reference, then returned GarbageCollector need to be copied, but the copy ctor is private, that's why compiler complains.
This question already has answers here:
Is Meyers' implementation of the Singleton pattern thread safe?
(6 answers)
Closed 6 years ago.
I've seen posts about this issue but I'm still trying to figure it out. Is this way alright for implementing a safe singelton? I'm using mutex, static member and return its reference.
#include <mutex>
using namespace std;
mutex mtx;
class MySingleton {
private:
MySingleton();
public:
MySingleton& getInstance() {
mtx.lock();
static MySingleton instance;
mtx.unlock();
return instance;
}
};
Is this way alright for implementing a safe singelton?
It's overshoot. Just get rid of the mutex and write:
static MySingleton& getInstance() {
static MySingleton instance;
return instance;
}
The thread safe creation of instance is guaranteed when the function is called the 1st time.
This question already has answers here:
Why class size depend only on data members and not on member functions?
(7 answers)
Closed 7 years ago.
Below are two structures defined in C/C++:
struct a
{
static int i;
void fun() {int i;}
};
struct b
{
static int i;
};
a obj1;
b obj2;
why sizeof of both obj1 and obj2 are same?
Non-virtual member functions and static members don't affect the size of an object, as they aren't stored inside the object.
Adding one or more virtual member functions would increase the size by an implementation-specific amount, usually the size of a pointer.