Let's assume I have a singleton class:
class Singleton {
public:
static Singleton* getInstance();
void doit();
std::vector<Object*>& getVector();
private:
std::vector<Object*> _vector;
static Singleton *instance;
Singleton();
~Singleton();
Singleton(const Singleton&);
};
class Delegator {
public:
void main();
}
- In the
doit
method I populate the_vector
with pointers to objects. - In the
main()
from theDelegator
class, I callgetVector()
and display the results.
Given this abstraction, I have the following questions:
- Can I delete all the pointers to instances of Object in the
main()
fromDelegator
(after displaying the results). If yes, is this recommended? - Does the singleton ever get destroyed? In other words, will the reference returned in
getVector()
always be valid? - I return a reference to a vector instead of a copy of the vector in
getVector()
. Given that the vector only contains pointers to objects and will not modify the vector content outside theSingleton
class, is there any efficiency gained from returning a reference?
Thank you in advance