Originally Posted by
execute
you were saying that some argue Singleton is an "anti-pattern", and that just didn't make much sense
hmm... as in you have never heard of the term "anti-pattern"?
Originally Posted by
execute
But laserlight was nice enough with extremely wise experience & expertise, that she recommended using "extern".
You have taken my statement out of context since I made no such recommendation. I merely pointed out that a global variable can exist across translation units by means of extern.
Originally Posted by
execute
So instead I could include "CCommon.h" and do:
extern void getDLL(string dll);
That would merely be a declaration of a free function. What I was talking about is declaring the object as extern.
Originally Posted by
execute
Well that sort of made the whole Singleton approach unnecessary. I don't really need a CCommon class, since my common functions can be accessed in multiple files now.
I think that you are confusing the concept of a singleton with that of a class. This probably also explains why you suggested that manzoor replace a forward declaration with a singleton. The concepts are orthogonal.
Originally Posted by
execute
Having it as a singleton, doesn't do any damage, I am using one instance of it.
That is a misconception. You should only choose to implement the singleton pattern for a class when it is certain that there can be only one instance of that class in any given program that uses the class. If not, you are doing "damage" because you are limiting the number of instances of the class when there is no good reason to do so.
Originally Posted by
execute
So I was saying in a way, I could eliminate the singleton and do the same thing, and I can also do it with singleton, it's the same.
Talking of eliminating the singleton does not make sense. What you are talking about is the removal of the implementation of the singleton pattern.
EDIT:
To put things in perspective consider:
Code:
#ifndef SKETCHERVIEW_H_
#define SKETCHERVIEW_H_
// sketcherview.h
class CElement; // Forward declaration.
class CSketcherView
{
public:
// ...
void view() const;
private:
CElement* element;
};
#endif
Code:
//sketcherview.cpp
#include "sketcherview.h"
#include "element.h"
// ...
void CSketcherView::view() const
{
element->draw();
}
execute, what you suggested is that this be turned into:
Code:
#ifndef SKETCHERVIEW_H_
#define SKETCHERVIEW_H_
// sketcherview.h
class CSketcherView
{
public:
// ...
void view() const;
};
#endif
Code:
//sketcherview.cpp
#include "sketcherview.h"
#include "element.h"
// ...
void CSketcherView::view() const
{
CElement::getInstance().draw();
}
I do not see how the latter can possibly be a replacement for the former when the former allows multiple CSketcherView objects to own their own CElement objects, and allows for polymorphism where CElement is an abstract base class. Clearly, forward declarations and the singleton pattern are not substitutable.