开发者

What does template<class key, class type> mean before a method in C++?

开发者 https://www.devze.com 2022-12-24 04:33 出处:网络
I have got this code and I am trying to understand the convention followed, all the method defined in the .cpp file have template<class KeyType, class DataType> written before them. What does th

I have got this code and I am trying to understand the convention followed, all the method defined in the .cpp file have template<class KeyType, class DataType> written before them. What does that mean?

Example:

开发者_如何学编程//Constructor
template<class key, class type>
MyOperation<key, type>::MyOperation()
{
  //method implementation
}

//A method
template<class key, class type>
MyOperation<key, type>::otherOperation()
{
  //method implementation
}

Thanks


There has to be a good answer for this already but I'll throw mine into the pool as well.

C++ allows for declarations and implementations of program structures to be done separately. It stems from how C/C++ programmers publish new functionality to each other: header files are included in dependent compilation units rather than those units relying on metadata present in compilation (like you would expect if you work with C# or Java).

Each time you give the compiler an instruction whether it is a declaration ("there will be this thing with this interface") or an implementation ("here is this thing with this interface and these behaviors"), you have an opportunity to templatize that directive.

The fact that you have an option to do so, rather than a requirement to do so, gives you a lot more flexibility than you are afforded by more modern languages such as Java and C#.

Consider the following template (I'm rusty so be kind with minor syntax issues, please):

template<typename Junk>
class IGotJunk {
private:
  Junk myJunk_;
public:
  void SetJunk(Junk const& source);
  Junk const& GetJunk() const;
}

Your "typical" implementation of said template might include these default behaviors:

template<typename Junk>
void IGotJunk<Junk>::SetJunk(Junk const& source)
{
  myJunk_ = source;
}

However, for strings, there is a risk that the string will be modified after the pointer is copied, in which case, you could provide a specialized behavior that ensures the string itself is copied, rather than the pointer (again, it's been a long, long time)...

void IGotJunk<char*>::SetJunk(char* const& source)
{
  free(myJunk_);
  myJunk_ = malloc(strlen(source) + 1);
  strcpy(myJunk_, source);
}

You could then do something similar for GetJunk(). That is probably why you have to declare the template parameters for each artifact you create: because you might not want them to be the same in every case.


This class is a template class -- it can be parameterized by types. To create an instance of this class, you would write, for example,

MyOperation<int, int> myop;

There's a decent article about C++ templates here. Templates are an important part of C++ but they take a very long time to become proficient in them. A large part of the C++ standard library uses templates (this is often known by its unofficial name, the STL). For example, vector<T> is a template in type T.


It tells the C++ compiler that the thing following that declaration is templated.

More specifically, it means that in whatever follows, the words KeyType and DataType are placeholder names for some type, which will be evident or specified upon calling the method. At that point, the compiler takes the template, substitutes KeyType and DataType in the method based on whatever you use, and compiles that copy.

In the code example you show, the templated part is the class, so it is possible that the methods don't actually use them - but C++ needs to be able to tie a generic implementation of the method to the class whenever it's used.


It just means that in header file template class is defined as:

template<class key, class type>
class MyOperation
{
   ...
};

So when you are implementing methods in .cpp file you need to include template parameters:

template<class key, class type> MyOperation<key, type>::

As in:

template<class key, class type> MyOperation<key, type>::otherOperation() { ... }
0

精彩评论

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

关注公众号