Calling an inherited template member function

Hi.
I've come across some template code, and just to clarify myself, I've written the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

template <class T> class Context 
{
public:
    void open(T& t) throw() {

	    cout << "Context::open() | content: " << t << endl; 
	};
};

class ContextString: public Context<string> 
{
public:
    void init(string& str) throw() {

	open(str); // (1)

	Context<string>::open(str); // (2)
    }
};

class ContextInt: public Context<int> 
{
public:
    void init(int i) throw() {

	open(i);
    }
};

int main()
{
    string context_s("Rosebud\n");
    int context_i(1941);

    ContextString cs;
    cs.init(context_s);

    ContextInt ci;
    ci.init(context_i);

    return 0;
}



At first, when I was going to use the template code, I called open() from ContextString::init() in the way (2).
Later I realised, I could use way (1), which is clearer and better in case things change in the future.

Could anyone give any direction/link/book to understand better what is happening behind the scenes?
(e.g.: An article about this matter in templates or a section in a chapter of a book would be great)

Thanks in advance,
Tomas.
This maybe:
http://www.cplusplus.com/doc/tutorial/templates.html

I *think* it covers what you want, but I'm not totally sure...you might also want to do a search for "C++ templates" or something.
closed account (z05DSL3A)
A good book on templates:

C++ Templates: The Complete Guide
by David Vandevoorde, Nicolai M. Josuttis

Hardcover: 552 pages
Publisher: Addison Wesley (20 Nov 2002)
Language English
ISBN-10: 0201734842
ISBN-13: 978-0201734843
Thanks firedraco, but that case is not covered there.The information there is pretty basic.
Moreover, it is not complete. In section Templates and multiple-file projects, it says:

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.


That is not completely true. There are two compilation models: Inclusion Compilation Model vs. Separate Compilation Model, and that is true only for the first one.

Of course, to be able to use the second one, your compiler has to give you that support. It is normally done with the export keyword.

Thanks Grey, I'll have a look at that book. I've already heard about it, but never had time to skim on it.
What is the inheritance for?

This might fit your needs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

template <class T>
class Context 
{
    void open( const T & thing )
    {
        cout << "Inside Context::open() with thing [" << thing << "]" << endl;
        // open the thing
    }
public:
    void init( const T & thing )
    {
        open( thing );
        //...
    }
    ~Context()
    {
        // close the thing
    }
};

typedef Context<string> ContextString
typedef Context<int> ContextInt

int main()
{
    string s( "Rosebud\n" );
    ContextString cs;
    cs.init( s );

    int i( 1941 );
    ContextInt ci;
    ci.init( i );

    return 0;
}
This is just a simplyfied example of code I came across, seymore15074.
In the original one the inheritance has its sense.

The aim of the example is to explain the situation, and try to understand that you can use both ways of calling open(). I think the first one can used because when ContextString inherits from Context<string> the template class is already instanciated.

So, even if no ContextString object is created, I think, the template class is instanciated.
Topic archived. No new replies allowed.