473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

forwarding templates

Hello,

I need a template class with a type T and a function INIT. For the default
case the INIT-function-pointer should point to the internal member function
init. But since the template parameters depend on the class member function
( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init ) I
don't know how to overcome this problem. Can onybody give me a hint.

Thank You, Thomas

template<class T, void (*INIT)(void)class C_C;

template<class T, void (*INIT)(void)=C_C<T, xx>::init >

class C_C

{

protected:

static void init(void){ cout << endl << "C_C::init()"; return; }

protected:

T val;

public:

C_C( void ){ INIT(); cout << endl << "+C_C"; return; }

virtual ~C_C( void ) { cout << endl << "-C_C"; return; }

};


Jan 6 '07 #1
3 1280

Thomas wrote:
Hello,

I need a template class with a type T and a function INIT. For the default
case the INIT-function-pointer should point to the internal member function
init. But since the template parameters depend on the class member function
( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init ) I
don't know how to overcome this problem. Can onybody give me a hint.
There are probably various ways. This one uses a dummy default argument
and a traits class.

regards
Andy Little
//------------------

#include <iostream>

// dummy function
void default_init(){}

// use as default
template<class T, void (*INIT)(void) = default_init class C_C;

//traits class
template <typename C>
struct init_traits;

// unspecialised use for no default
template <
class T,
void (*INIT)(void)
>
struct init_traits< C_C<T,INIT
{
static void (*init_function)();
};

//specialsie use for default
template < class T>
struct init_traits< C_C<T,default_init
{
static void (*init_function)();
};

// need to declare...
template<
class T,
void (*INIT)(void)
>
void (*init_traits<C_C<T,INIT::init_function)()= INIT;

template<
class T
>
void (*init_traits<C_C<T,default_init::init_function)() =
C_C<T,default_init>::init;
template<
class T,
void (*INIT)(void)
>
class C_C {
public:
static void init(void){ std::cout << "C_C::init()\n"; return; }

T val;

C_C( void ){ init_traits<C_C>::init_function(); }
virtual ~C_C( void ) { std::cout << "-C_C\n"; return; }
};

void other_init(){std::cout << "other init\n";}

int main()
{
C_C<intx;
C_C<int,other_initxx;
}

Jan 6 '07 #2
Thomas napsal:
Hello,

I need a template class with a type T and a function INIT. For the default
case the INIT-function-pointer should point to the internal member function
init. But since the template parameters depend on the class member function
( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init ) I
don't know how to overcome this problem. Can onybody give me a hint.

Thank You, Thomas

template<class T, void (*INIT)(void)class C_C;

template<class T, void (*INIT)(void)=C_C<T, xx>::init >

class C_C

{

protected:

static void init(void){ cout << endl << "C_C::init()"; return; }

protected:

T val;

public:

C_C( void ){ INIT(); cout << endl << "+C_C"; return; }

virtual ~C_C( void ) { cout << endl << "-C_C"; return; }

};
Hi. You need to know init function already before class C_C definition.
Try it this way:

#include <iostream>

template<typename T>
struct C_C_initializer
{
static void init(T& t)
{
std::cout << "C_C_initializer::init(T& t)\n";
}
};

template<class T, void (*INIT)(T&) = C_C_initializer<T>::init>
class C_C
{
protected:
T val;

public:
C_C()
{
INIT(val);
std::cout << "+C_C\n";
}

virtual ~C_C()
{
std::cout << "-C_C\n";
}

private:
};

// Custom initializer
void MyInit(int& i)
{
std::cout << "MyInit\n";
}

int main()
{
C_C<intinstance;
C_C<int, MyInitinstance2;
}

In initializer is added some reference to value to be initialized.
Place there whatever you want.

Jan 6 '07 #3
Hello,

many thanks to Ondra and Andy for their directly answer.

My primary goal was to "include" the helper init functions resp. init
classes (C_C_initializer, default_init ) as a member function in C_C. With
respect to the comments of Ondra and Andy it seems, that there is no way to
do that.

Best regards, Thomas


"Thomas" <Th****@Schmid-Zettler.deschrieb im Newsbeitrag
news:en**********@online.de...
Hello,

I need a template class with a type T and a function INIT. For the
default
case the INIT-function-pointer should point to the internal member
function
init. But since the template parameters depend on the class member
function
( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init ) I
don't know how to overcome this problem. Can onybody give me a hint.

Thank You, Thomas

template<class T, void (*INIT)(void)class C_C;

template<class T, void (*INIT)(void)=C_C<T, xx>::init >

class C_C

{

protected:

static void init(void){ cout << endl << "C_C::init()"; return; }

protected:

T val;

public:

C_C( void ){ INIT(); cout << endl << "+C_C"; return; }

virtual ~C_C( void ) { cout << endl << "-C_C"; return; }

};


Jan 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anu | last post by:
Hi everyone, I have just joined the group. I need your expert comments on my problem urgently. I am trying to establish SSH forwarding for establishing connections from my web server (obsgeosn) to...
1
by: Binod Nair | last post by:
Hi All, I have an ASP.NET appication running on http://xx.xxx.xxxx.xx/aspApplication and I have a domain http://www.mydomain.com registered at godaddy.com for this application.I have setup...
1
by: Jesse Rosenthal | last post by:
Hello all, I'm writing a script which will backup data from my machine to a server using rsync. It checks to see if I am on the local network. If I am, it runs rsync over ssh to 192.168.2.6...
0
by: nicholas | last post by:
I use session variables that stores data for a shopping basket. The site runs at www.mysite.com/france and works fine. But I also have the domainname: www.mysiteforfrance.com So, on a Plesk...
0
by: Daniel P. | last post by:
Can anyone advise me for a staring point where I can learn about how to do forwarding? What I need is to learn how to have IIS machine that is exposed to the outside world that does only...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
1
by: thomas | last post by:
Hello all, It seems like subdomain forwarding prevents ASP.Net session state from working correctly. Example: two websites http://www.jgphotographers.com/test and...
5
by: Dave | last post by:
Hi All, I'm experiencing problems with sending mail using mail() to forwarded email accounts. The problem seems to revolve around the optional 4th argument of mail(), namely 'additional headers'....
6
by: mcl | last post by:
I have a domain name which is set up for web forwarding with a frame. I have a link on one of the site's pages to an external site. When I select the link the external site is displayed correctly...
1
by: sj7272 | last post by:
Hi, I am building email marketing framework, my email templates go out to clients and I wish to include a "send to a friend" or "forward to a friend" link in the outbound email. I want to track...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.