Click here to Skip to main content
14,767,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to create an object of template type in C++?

What I have tried:

I have tried to create templatized object in C++.
Posted
Updated 4 days ago

   
Comments
CPallini 4 days ago
   
5. :-D
a.) Let's assume you have a template class something like
template <class T>
class TNullable
{
  // This class does implement TNullable in a generic way
}

b.) Then it is a good practice to define a type to use the template class
typedef TNullable<int> TNullableInt;

c.) Use the template class
TNullableInt  myNullableIntVariable;

Note: Of course you can make it shorter and use directly
TNullable<int> myDirectNullableIntVariable;


I hope it is helpfull and not too abstract ;)


To give you an idea, a possible implementation of TNullable

template <class T>
class TNullable
{
   private:
      bool  FIsNull;
      T     FValue;

      const T& GetValue() const
      {
         if (FIsNull)
         {
            throw(*new Exception("TNullable Null Value Access"));
         }
         return(FValue);
      }

      void SetValue(const T& Value)
      {
         FValue= Value;
         FIsNull= false;
      }
   public:
      TNullable()
         :  FIsNull  (true),
            FValue   ()
      {
      }

      TNullable(const T& rhs)
         :  FIsNull  (false),
            FValue   (rhs)
      {
      }

      virtual ~TNullable(){}

   	// Copy
      TNullable (const TNullable& rhs)
      {
         operator=(rhs);
      }

      // Assignement
	   const TNullable& operator=(const TNullable& rhs)
      {
         if (this != &rhs)
         {
            FIsNull  = rhs.FIsNull;
            FValue   = rhs.FValue;
         }
         return(*this);
      }
      // Operator T
	   const T& operator=(const T& rhs)
      {
         FIsNull  = false;
         FValue   = rhs;
      }

      bool operator== (const TNullable& rhs) const
      {
         if (FIsNull != rhs.FIsNull)
         {
            return(false);
         }
         else if (FIsNull)
         {
            return(true);
         }
         else
         {
            return(FValue == rhs.FValue);
         }
      }

      bool operator!= (const TNullable& rhs) const
      {
         return(!operator==(rhs));
      }

      __property bool IsNull     = {read= FIsNull, write= FIsNull};
      __property T    Value      = {read= GetValue, write= SetValue};
};
   
v5
Comments
Rick York 4 days ago
   
Item b) is called specialization and the word is "practice." There is also the using statement which is very handy.
embar.k.adero 4 days ago
   
Thank you Sir (using statement in c++?)
Rick York 4 days ago
   
Yes.

using TNullableInt = TNullable<int>;

is an example.
embar.k.adero 4 days ago
   
I'm old, definitively, thanks a lot :)
... and btw corrected 'practice'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900