Template constructor Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
User avatar
kolo
Earned some good credits
Earned some good credits
Posts: 120
Joined: Tue Jun 21, 2005 1:19 pm
Location: Russia, Cheboksary
Contact:

Template constructor

Post by kolo »

I have one c++ question: how to call template constructor of some class.
For example:

Code: Select all

#include <iostream>

class Speaker
{
public:
 void SayHello(){ std::cout << "Hello world" << std::endl;
};

class Some
{
public:
 template<class A>
 Some()
 {
   A*a = new A();
   a->SayHello();
 }
};

int main()
{
 Some *s = new ???// what code I must put here, if I want initialize some with Speaker class
}
May be C++ standart forbid constructions like this?
only MSW & MSVS ))
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
The simple case

Code: Select all

class Speaker
{
public:
	void SayHello(){ std::cout << "Hello world" << std::endl;}
};

template<class A>
class Some
{
public:
 Some()
 {
   A*a = new A();
   a->SayHello();
 }
};
Call it like

Code: Select all

Some<Speaker> *s = new Some<Speaker>;
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
User avatar
kolo
Earned some good credits
Earned some good credits
Posts: 120
Joined: Tue Jun 21, 2005 1:19 pm
Location: Russia, Cheboksary
Contact:

RE:

Post by kolo »

I know, that this is a simple solution, but I don`t need "solution", i need answer to my question :-). I know another few solutions how to avoid this problem. I got just theoretical interest :), not practical.
only MSW & MSVS ))
User avatar
kolo
Earned some good credits
Earned some good credits
Posts: 120
Joined: Tue Jun 21, 2005 1:19 pm
Location: Russia, Cheboksary
Contact:

Post by kolo »

I find answer in standart: 14.5.2,5(note)
Note: because the explicit template argument list follows the function template name, and
because conversion member function templates and constructor member function templates are called without
using a function name, there is no way to provide an explicit template argument list for these function templates
only MSW & MSVS ))
Post Reply