473,734 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12151
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...@com Acast.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
5179
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
2979
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
2
1916
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
9723
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 initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
7
1729
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, short Trank, bool Tcont> TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont& tten );
12
7205
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? class Trial { public: Trial() {
12
2305
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 switch statements. My first approach was to rely on partial template specialization. Therefore, I have: // .h file template <int vSomeClass;
2
2289
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 <stack> template <class T> class A { public:
4
5549
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> class A
0
8777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.