Skip to main content
gamedev.net
🔒 Locked

Nested C++ Template Issue

Started by PaleRaider 0 replies 300+ views
Original Post
PaleRaider
PaleRaider
I am new to using templates and was wondering how I can go about doing the following. I have a base template class called Field:
          
template <class Item>
class Field{
private:
    Item data
    std::string strName

    explicit Field(std::string name, Item value): strName(name),m_iData(value){
   }

   ~Field(){}

   const Item& getData()const{
	return data
   }
   
   const std::string& getName()const{
       return strName
   }

   void setData(Item data){
       m_iData = data;
   }
   
   void setName(std::string name){
       m_strName = name;
   }
};
  
I would like to have class called Record that contains a set of Field instances in an stl container. The problem is I want the Record to contain different types of Fields, i.e.
  
Field<int>, Field<string>, and Field<float>
      
How do I go about declaring a vector like that? Also I need some methods of the Record class to return the data contained in a specified Field, and this of course is dependent upon the Field itself. This little has caused me no end of frustration. [edited by - paleraider on May 8, 2003 9:11:08 PM] [edited by - paleraider on May 8, 2003 9:12:10 PM] [edited by - paleraider on May 8, 2003 9:13:09 PM] [edited by - paleraider on May 8, 2003 9:14:12 PM]
risingdragon3
risingdragon3
You use boost::any. (www.boost.org).
Either that or you use polymorphism and a base class.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.