Click here to Skip to main content
15,881,248 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 22-Feb-21 5:47am

 
Share this answer
 
Comments
CPallini 22-Feb-21 11:45am    
5. :-D
Member 15025528 1-Mar-21 7:03am    
The given solution about creating templatized class. I am looking for creating an object template type. For example an object at one time may have different members and at other time will have different members.
Richard MacCutchan 1-Mar-21 7:08am    
Then they will be different objects.
Member 15025528 1-Mar-21 7:14am    
Yes, correct Richard.
That will be different objects of same class.
I am able to create template class, but I am not able to create templatized object of same class.
I am looking a help what the way to implement.
Richard MacCutchan 1-Mar-21 7:23am    
If the objects are different, then they must be objects of different classes. You need to understand what a class is used for.
a.) Let's assume you have a template class something like
C++
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
C++
typedef TNullable<int> TNullableInt;

c.) Use the template class
C++
TNullableInt  myNullableIntVariable;

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


I hope it is helpfull and not too abstract ;)


To give you an idea, a possible implementation of TNullable

C++
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};
};
 
Share this answer
 
v5
Comments
Rick York 22-Feb-21 11:59am    
Item b) is called specialization and the word is "practice." There is also the using statement which is very handy.
[no name] 22-Feb-21 12:02pm    
Thank you Sir (using statement in c++?)
Rick York 22-Feb-21 12:03pm    
Yes.

using TNullableInt = TNullable<int>;

is an example.
[no name] 22-Feb-21 12:05pm    
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