Solved

c++ class template (not inline)

Posted on 2014-01-22
4
419 Views
Last Modified: 2014-01-22
I have coded a C++ class template that is a descendent of the STL map template. The code compiles and runs as expected. I have coded it inline in a header file. How do I code the member functions in a separate cpp file, using the header file for the prototype declaration only ?
The code in the header file is a follows :

#include <map.h>

template<class T1,class T2>
class MyMap : public map<T1,T2>
{
    private:
    map<T1,T2> &mx;
    T2 *x;
    public:
    MyMap()  : mx(*this){};
    ~MyMap(void){};
    map<T1,T2> operator = (map<T1,T2>  m)
    {
        mx = m;
        return m;
    };
    T2& operator[](T1 k)
    {
        if(mx.count(k)==0)
        {
            x = &new T1();
            mx[k]= *x;
        }
        return  mx[k];
    }
};
0
Comment
Question by:alcindor
  • 2
4 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 39800098
Normally, you do not code the template member functions in a separate cpp file since the templates are not actual code that is run until the compiler statically instantiates them. If another module uses your header and tries to instantiate them, it will not know how since it doesn't have direct access to the template .cpp file.

Is there a reason why you want to separate them?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 400 total points
ID: 39800139
Here is a  more detailed elaboration of why you don't separate the template body code from the template declarations.
   Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?

But, if you really must, there is a way that requires extra maintenance when you need to add a new static instantiation of your template class.
Separate Template Class Definition from Declaration
The message from this last link is that in your .cpp file, you will need to add as many specific static instantiations as you  program requires - something like:
  template class MyMap <int, int>;
  template class MyMap <string, int>;
  etc.

When you compile this .cpp file, you get the body of all the different signatures your program needs. If you later need a <string, string>, then you have to go back and add
    template class MyMap <string, string>;
to the template's .cpp file.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 39800148
>>How do I code the member functions in a separate cpp file, using the header file for the
>>prototype declaration only ?

Just like with any other class that would not be templated. The caveat just is: While the C++ Standard technically allows doing that, most compilers don't support it. So, if that already works for you, the question basically is: Why putting in the effort if chances are that you will have to add a '#include "MyMap.cpp"' to get it to work anyway?
0
 
LVL 2

Author Closing Comment

by:alcindor
ID: 39800257
Thanks for your comments. I have a better understanding now and will not be coding the definition from the declaration.

Roger
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question