Skip to main content
added 517 characters in body
Source Link
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have a base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
   
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };

This seems to be the possible solution :

class SingletonInterface
{
public:
   template <class T>
   static T& getInstance() 
   {
      return T::getInstance();
   }

private:
   SingletonInterface() { };
};

class TrafficGenerator 
{
public:
  
   static TrafficGenerator& getInstance()
   {
      static TrafficGenerator obj;
      return obj;
   }

   void whatAmI()
   {
      std::cout << " I am Traffic Generator" << std::endl;
   }

private:

   TrafficGenerator () {};
};

int main() 
{

   TrafficGenerator& hTG = SingletonInterface::getInstance<TrafficGenerator>() ;
   hTG.whatAmI();
};
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have a base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
   
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
  1. I have a base and derived class since I need to have different kinds of concrete classes.

  2. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
   
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };

This seems to be the possible solution :

class SingletonInterface
{
public:
   template <class T>
   static T& getInstance() 
   {
      return T::getInstance();
   }

private:
   SingletonInterface() { };
};

class TrafficGenerator 
{
public:
  
   static TrafficGenerator& getInstance()
   {
      static TrafficGenerator obj;
      return obj;
   }

   void whatAmI()
   {
      std::cout << " I am Traffic Generator" << std::endl;
   }

private:

   TrafficGenerator () {};
};

int main() 
{

   TrafficGenerator& hTG = SingletonInterface::getInstance<TrafficGenerator>() ;
   hTG.whatAmI();
};
deleted 89 characters in body
Source Link
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have a base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
    
    static ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have a base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
    
    static ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have a base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
   
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
added 20 characters in body
Source Link
user34073
user34073
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have ana base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
    
    static ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
    
    static ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have an base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
    
    static ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
  1. The reason I am not creating a static object is because I need to have the ability to delete the created concrete object.

  2. I have a base and derived class since I need to have different kinds of concrete classes.

  3. I cannot use the getInstance() function in the base class since I cannot make it both static and virtual.

class ITrafficEventsReturner {
    
    public:    virtual ~ITrafficEventsReturner() {} ;
    
    protected:
    
       ITrafficEventsReturner() { };
    
       // no definition for copy constructor and assignment operator  
       ITrafficEventsReturner(const ITrafficEventsReturner&);      
       ITrafficEventsReturner& operator=(const ITrafficEventsReturner&); };
    
    
    class ConcreteTrafficReturner : public ITrafficEventsReturner {
    
    public:
        static ConcreteTrafficReturner* getInstance() {
          if (myself == 0)
          {
             myself = new ConcreteTrafficReturner();
          }
    
          return myself;    }
    
       ~ConcreteTrafficReturner()
        {
          if (myself)
          {
             delete myself;
             myself = 0;
          }
        }
    
    private :    ConcreteTrafficReturner() : ITrafficEventsReturner()    {
    
       };
    
       static ConcreteTrafficReturner* myself; };
    
    static ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    ConcreteTrafficReturner* ConcreteTrafficReturner::myself = 0;
    
    int main() 
    {
       ITrafficEventsReturner* ConcreteTrafficReturner =     
            ConcreteTrafficReturner::getInstance();
    
       delete ConcreteTrafficReturner;
    };
added 71 characters in body
Source Link
Loading
added 233 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
added 31 characters in body
Source Link
Loading
Source Link
Loading