473,224 Members | 1,682 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,224 software developers and data experts.

How to call templated constructor explicitly?

How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.

#include <iostream>

class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};

int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?

std::cin.get();

return 0;
}

If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.

Thanks in advance,

Lawrence

Jul 16 '07 #1
3 11896
Lawrence Spector wrote:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.

#include <iostream>

class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};

int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?

std::cin.get();

return 0;
}

If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.
There is no way to do that. Any templated constructor has to have the
argument[s] from which the compiler will deduce the template arguments.
In your case you need to supply (at least a bogus) argument, like

template<class TTestClass(T* bogus) {
...
}

which you will supply as

TestClass testClass((int*)0); // means 'T' is 'int'.

You could invent some other (nicer, perhaps) way of providing the bogus
argument.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 16 '07 #2
On Jul 16, 3:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Lawrence Spector wrote:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.
#include <iostream>
class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};
int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?
std::cin.get();
return 0;
}
If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.

There is no way to do that. Any templated constructor has to have the
argument[s] from which the compiler will deduce the template arguments.
In your case you need to supply (at least a bogus) argument, like

template<class TTestClass(T* bogus) {
...
}

which you will supply as

TestClass testClass((int*)0); // means 'T' is 'int'.

You could invent some other (nicer, perhaps) way of providing the bogus
argument.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I see. Thanks for the prompt reply. I guess to make it look a little
nicer, I could use a factory when I create it.

Thanks for the assistance,

Lawrence

Jul 16 '07 #3
Lawrence Spector wrote:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.

#include <iostream>

class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};

int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?

std::cin.get();

return 0;
}
I think, that has no chance of working.
If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.
As a work-around, you could introduce a dummy parameter:

template <class T >
TestClass ( T* )
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}

and construct the object via:

TestClass test_object ( (some_type*)0 );
Best

Kai-Uwe Bux
Jul 16 '07 #4

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

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
2
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
7
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum,...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
4
by: l.s.rockfan | last post by:
Hello, how do i have to call an inherited, templated class constructor from the initializer list of the inheriting, non-templated class constructor? example code: template<typename T>...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.