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.
Related
This question already has answers here:
C++: Difference Between Non-Member Function and Static Member Function?
(3 answers)
Closed 1 year ago.
So using static keyword below we can call MyMethod without creating an object of the class MyClass, my question is how is it even possible as a class is not allocated memory when the code is compiled?
static int MyMethod( int * a, int * b );
int one = 1;
int two = 2;
MyClass::MyMethod( &two, &one );
It seems you have a bit of a misunderstanding. There is no memory allocated for a class's methods every time it is made. Class methods are generally separate from their classes. I guess you could call the "Memory allocated to the member functions" the space that the opcodes take up. But yes, this is separate from the objects. It is just a language requirement that a member function that isn't static must be called by an instance of the class. Static functions intentionally don't care about this, and can be called even when it is not called by an object.
This question already has an answer here:
How to initialize a shared_ptr that is a member of a class?
(1 answer)
Closed 8 years ago.
I have a class as:
class LargeObject
{
public:
LargeObject();
void DoSomething();
private:
std::unique_ptr<Thing> pThing;
};
Then when I want to create the pointer in the constructor
LargeObject()
{
pThing(new Thing()); //This does not work.
}
I want to use the member variable throughout the code. How to do that?
I think initialization should be in constructor's initialization list, that's the place where constructors should be invoked from another constructor:
LargeObject()
:pThing(new Thing){}
This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 9 years ago.
So I have the following c++ class
class MyClass:
public:
static void InitObject();
private:
static MyObject *myObject;
};
And then in the .cpp file I do
void MyClass::InitObject
{
myObject = new MyObject();
}
However, I get a compiler error saying that "myObject" was referenced from InitObject() and then it says Linker command failed with exit code 1.
Why doesn't this work? How do I fix it?
Since static data members of classes in C++ need memory space that is separate from the instance memory, they need to be defined, in addition to being declared in the class.
You do that in a separate translation unit (in your CPP file) like this:
MyObject *MyClass::myObject;
This definition tells the compiler to allocate space for myObject in the static memory area. Without this definition, the code is going to compile, but the linker will report an error, because it is responsible for ensuring that all referenced static objects have memory allocated to them.
Extend your .cpp file with the following definition for myObject:
MyObject* MyObject::myObject = NULL;
NOTE:
For your particular case you might be better off saying:
class MyClass:
{
public:
static MyClass& instance();
private:
MyClass() {}
};
and in .cpp file:
MyClass& MyClass::instance()
{
static MyClass myInstance;
return myInstance;
}
I'd prefer this over using new MyClass(). Any access to the instance() method will guarantee your instance is initialized exactly once, and you'll get a valid reference to it.
'Space' is completely allocated on the stack then, as well for the reference as for the instance itself.
You never allocated space for MyObject::myObject. Put this in the CPP file:
MyObject* MyObject::myObject;
This question already has answers here:
Reference as class member initialization
(4 answers)
Closed 9 years ago.
I am sorry if this meight be a really simple question but i am new to c++ and working on a simple vocable trainer for understanding c++. (come from java..)
I'd like to pass a const FileManager as reference to my Logic. But i don't get it work. I don't want to have a copy or such.
So i tried it like this: (the main)
FileManager& file = FileManager();
Logic logic = Logic(file);
Inside of the Logic i'd like to store the reference:
class Logic
{
public:
Logic(const FileManager& manager);
~Logic();
private:
const FileManager& m_fileManager;
};
Logic::Logic(const FileManager& manager) :
{
m_fileManager = manager;
}
Thanks
Once the body of a constructor is entered, all member variables have already been initialized. Thereafter, you can only assign to them. This can't work with references - as we know, they need to be initialized when their lifetime begins.
You need to use member initializer list:
Logic::Logic(const FileManager& manager)
: m_fileManager(manager) // m_fileManager is initialized here
{
}
Consider if you really want a reference member. For one, they make your class non-assignable. A smart pointer might be a better choice.
Your example has three flaws. The first, is that you try to initialize a none const reference with a temporary object:
FileManager& file = FileManager();
Most likely you just want to use a FileManager instance here:
FileManager file;
Second, references must be initialized. You achieve this by using the initializer list syntax:
Logic::Logic(const FileManager& manager)
: m_fileManager( manager )
{
}
In addition, the initializing, you use for Logic requires that Logic is assignable. Simply use:
Logic logic(file);
If you have reference members in a class, objects of that class are not assignable by default.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I call ::std::make_shared on a class with only protected or private constructors?
I want to create a shared pointer to a class, and have a factory method that returns it while keeping the constructor\destructor protected. since the shared pointer can't access the the constructor or the destructor, I get compiler errors.
I am using llvm 4.1, but I am looking for a solution that can be compiler independent (besides making the constructor\destructor public).
this is a code sample:
class Foo
{
public:
static std::shared_ptr<Foo> getSharedPointer()
{
return std::make_shared<Foo>();
}
protected:
Foo(int x){}
~Foo(){}
};
any Ideas?
Just allocate the pointer yourself instead of calling make_shared:
static std::shared_ptr<Foo> getSharedPointer()
{
return std::shared_ptr<Foo>(new Foo);
}
Note, however, that this would require making the destructor public.