E
Eric
I am implementing a variation on the Singleton design pattern, that
allows up to 8 objects of a class to be instantiated, and returns a
null pointer for anything more than 8.
I am running into a compile problem with GNU g++.
Here is the code:
/******************** FILE sample.h ********************/
#ifndef SAMPLE_H
#define SAMPLE_H
class Allow_8_Objects
{
public:
// Destructor is public so that it can be executed by
// "delete"ing the pointer to the object. Note that the
// Contstructor is protected so it can't be executed
// from the outside with "new" (one of the rules for
// the Singleton design pattern).
//
~Allow_8_Objects();
static Allow_8_Objects* MakeInstance( void );
// This method creates an object of this class, and
// returns its pointer. Returns NULL if no more
// objects can be made or some other error
// happened.
protected:
Allow_8_Objects();
// Protected constructor as part of the implementation
// of a modified Singleton design pattern (modified to
// allow 8 instances).
private:
static Allow_8_Objects* theInstance;
// an instance of this class. Static
// because it is used by MakeInstance(), which is
// static. This pointer is what is returned by
// MakeInstance().
static int numInstances;
// number of instances in existence. Cannot
// exceed maxObjects. Note: static.
};
#endif // SAMPLE_H
/**************** END OF FILE sample.h *****************/
/*******************************************************/
/******************* FILE sample.cpp *******************/
#include "sample.h"
static const int maxObjects = 8;
// Maximum of 8 objects allowed per node
int Allow_8_Objects::numInstances = 0;
Allow_8_Objects::Allow_8_Objects()
{
}
Allow_8_Objects::~Allow_8_Objects()
{
}
/********** HERE IS THE METHOD WITH THE PROBLEM **********/
// Make another object if not already at max.
//
Allow_8_Objects* Allow_8_Objects::MakeInstance( void )
{
theInstance = (Allow_8_Objects*)NULL;
// If we already have the max number of objects, leave
// the return pointer set to NULL, else make a new object
// and get ready to return its pointer.
//
if ( numInstances < maxObjects )
{
theInstance = new Allow_8_Objects;
}
return theInstance;
}
/*************** END OF FILE sample.cpp ****************/
Note that MakeInstance( ) is static, and theInstances and numInstances
are both static. So, MakeInstance shouldn't have any trouble
accessing theInstances and numInstances, right?
What happens when I compile this is that the cimpiler complains about
"undefined reference to Allow_8_Objects::theInstance" at lines 1, 7,
and 11 of MakeInstance(). It can find and compile numInstances just
fine (line 9 of MakeInstance()), just not theInstance.
Any ideas?
allows up to 8 objects of a class to be instantiated, and returns a
null pointer for anything more than 8.
I am running into a compile problem with GNU g++.
Here is the code:
/******************** FILE sample.h ********************/
#ifndef SAMPLE_H
#define SAMPLE_H
class Allow_8_Objects
{
public:
// Destructor is public so that it can be executed by
// "delete"ing the pointer to the object. Note that the
// Contstructor is protected so it can't be executed
// from the outside with "new" (one of the rules for
// the Singleton design pattern).
//
~Allow_8_Objects();
static Allow_8_Objects* MakeInstance( void );
// This method creates an object of this class, and
// returns its pointer. Returns NULL if no more
// objects can be made or some other error
// happened.
protected:
Allow_8_Objects();
// Protected constructor as part of the implementation
// of a modified Singleton design pattern (modified to
// allow 8 instances).
private:
static Allow_8_Objects* theInstance;
// an instance of this class. Static
// because it is used by MakeInstance(), which is
// static. This pointer is what is returned by
// MakeInstance().
static int numInstances;
// number of instances in existence. Cannot
// exceed maxObjects. Note: static.
};
#endif // SAMPLE_H
/**************** END OF FILE sample.h *****************/
/*******************************************************/
/******************* FILE sample.cpp *******************/
#include "sample.h"
static const int maxObjects = 8;
// Maximum of 8 objects allowed per node
int Allow_8_Objects::numInstances = 0;
Allow_8_Objects::Allow_8_Objects()
{
}
Allow_8_Objects::~Allow_8_Objects()
{
}
/********** HERE IS THE METHOD WITH THE PROBLEM **********/
// Make another object if not already at max.
//
Allow_8_Objects* Allow_8_Objects::MakeInstance( void )
{
theInstance = (Allow_8_Objects*)NULL;
// If we already have the max number of objects, leave
// the return pointer set to NULL, else make a new object
// and get ready to return its pointer.
//
if ( numInstances < maxObjects )
{
theInstance = new Allow_8_Objects;
}
return theInstance;
}
/*************** END OF FILE sample.cpp ****************/
Note that MakeInstance( ) is static, and theInstances and numInstances
are both static. So, MakeInstance shouldn't have any trouble
accessing theInstances and numInstances, right?
What happens when I compile this is that the cimpiler complains about
"undefined reference to Allow_8_Objects::theInstance" at lines 1, 7,
and 11 of MakeInstance(). It can find and compile numInstances just
fine (line 9 of MakeInstance()), just not theInstance.
Any ideas?