Hunk wrote:
Quote:
Ondra Holub wrote:
Quote:
Hunk napsal:
Well, you do not need it, if you want to declare pointer or reference
to my_class. But if you need to access any method (or attribute) of
this class, you need the definition of this class as well as in case
you need to declare instance of this class.
It is the same as in following example without templates:
class X;
X& rx; // OK
X* px; // OK
X x; // Error
void X::method() // Error - nothing is known about methods of class X
{
}
If you need generic class, you have to give to user whole
implementation. You can split it into 2 headers, one contains only
declaration of templated class and second contains full implementation,
so you can speed up compilation simillary as with #include <iosfwdand
#include <iostream>
>
So there's no way other than to include the header file which contains
the declaration?
That means I would have to necessarily include the declaration header
file , and using forward declaration has no effect except for use as
pointers. The methods cannot be accessed for a template class? Is there
any method people have to hide the template declarations?
Template class forward declarations are normally in a source file, and
not placed in a header.
If done properly, you can put your template source code in the *.cpp
source file.
Template class forward declaration method limits the available types
that can be used with the template. From looking at your code, this
seems to be acceptable for your requirements.
Here's an example usage:
//c_reg.h
/////////////////////////////////////////////////////////////////////////////////////
#include <string>
template<typename T,typename R>
class c_reg
{
public:
virtual R get_data(T p_data);
virtual void set_data(T , R);
};
typedef c_reg<int,std::stringc_my_reg; //This is NOT a forward
template declaration
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.h
/c_reg.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "c_reg.h"
//*****************************
template c_reg<int,std::string>; //This IS a template class forward
declaration!!!
//*****************************
template<typename T,typename RR c_reg<T,R>::get_data(T p_data)
{
return R();
}
template <typename T,typename Rvoid c_reg<T,R>::set_data(T p_key, R
p_data)
{
std::cout << "c_reg<T,R>::set_data " << p_key << ":" << p_data <<
std::endl;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg.cpp
//c_reg_init.h
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg.h"
#include <iostream>
template<typename T>
class c_reg_init : public c_my_reg
{
public:
void set_data (int, std::string);
};
template<typename Tvoid c_reg_init<T>::set_data(int i, std::string s)
{
std::cout << "c_reg_init<T>::set_data " << i << ":" << s <<
std::endl;
c_my_reg::set_data(i, s);
}
/////////////////////////////////////////////////////////////////////////////////////
//end of c_reg_init.h
//main.cpp
/////////////////////////////////////////////////////////////////////////////////////
#include "c_reg_init.h"
int main(int, char**)
{
c_reg_init<longmy_c_reg_init;
my_c_reg_init.set_data(3, "4");
c_my_reg * pc_my_reg = &my_c_reg_init;
pc_my_reg->set_data(5, "6");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
//end of main.cpp