开发者

in C++, how do you code a class ADT--like std::vector or std::stack--that takes a data type in <> as argument and constructs the object accordingly?

开发者 https://www.devze.com 2023-02-14 13:03 出处:网络
Ie. I want to code a class ADT that can be used like this: myADT <type>开发者_JAVA技巧 objectA;

Ie. I want to code a class ADT that can be used like this:

myADT <type>开发者_JAVA技巧 objectA;

in the same way that someone would use a vector like:

vector <type> aVector;

I'm guessing maybe it has something to do with templates and overloading the <> operator, but how do I get it to take a data type as an argument?


Let's say you have a class that implements your ADT using a hard-coded typedef T:

class ADT
{
  public:
    typedef int T;

    ADT();
    T& operator[](size_t index);
    const T& operator[](size_t index) const;
    size_t size() const;
    ...
  private:
    T* p_;
    size_t size_;
};

(You have to work out the internal storage and member functions that are appropriate for your actual ADT).

To change this so that you can specify T as done for std::vector etc.:

template <typename T>    <-- specification of T moved to here
class ADT
{
  public:
                         <-- typedef for T removed from here
    ADT();
    T& operator[](size_t index);
    const T& operator[](size_t index) const;
    size_t size() const;
    ...
  private:
    T* p_;
    size_t size_;
};

Usage is then:

ADT<int> an_adt_int;
ADT<double> an_adt_double;


If ADT is abstract data type then you should use templates

template<class T>
struct myADT
{
    // implementation, for example, two variables of type T.
    T A;
    T B;
};

Using

myADT<int> objectA;

objectA.A = 3;
objectB.B = 4;

myADT<char> C;

C.A = 'A';

I would recommend you to read a book or articles about C++ templates. Google "C++ template".


Templates help to adapt functionality for a type with out actually repeating code for each type, which is generic programming.

template < class T >
class foo
{
    T number ;
    public:
    foo( T a )
    {
        number = a; // This can be achieved through initializer lists too.
    }
};

In the above snippet, keyword template says that the following is a template. And the type is class, i.e., enclosed in <>. So, foo is a class template whose template parameter is T.

foo<int> obj(10);

The template parameter is of type int. So, the corresponding code is generated by the compiler on template intantiation. i.e.,

class foo
{
    int number ;
    public:
    foo( int a )
    {
        number = a; // This can be achieved through initializer lists too.
    }
};

Had if a different template parameter other than int is supplied, corresponding code would be generated by the compiler on template instantiation. For more info, MSDN Templates Tutorial. Hope it helps.


I hope it's what you are looking for

template <typename NodeType> class List;
template <typename NodeType>
class Node_List
{
    ...
    private:
      NodeType date;
      Node_List< NodeType > *next_Ptr;    
}

class List
{
   ...
   private:
     Node_List< NodeType > *first_Ptr;
     Node_List< NodeType > *last_Ptr;
 }

Using:

List < int > int_list; List < char > char_list;


@Matt Munson, there is an answer to the question for Tony... from cplusplus:

The format for declaring function templates with type parameters is:

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号