473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

can constructor be a template?

Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

please guide me...
regards,
john

Jun 6 '07 #1
4 1815
* Deep:
Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

please guide me...
You can use templated constructors, but a templated constructor is never
formally a copy constructor. Therefore, the compiler may generate a
copy constructor, and use that, if copying of same type is invoked.
This is difficult to test because the compiler is generally allowed to
remove copy constructor calls even if the copy constructor has some side
effect.

Also, you need some other constructor in order to create any object in
the first place.

You can try (although output from the copy constructor is not guaranteed)

#include <iostream>
#include <ostream>

void say( char const s[] ) { std::cout << s << std::endl; }

template<typename T>
class MyClass1
{
public:
int x_;
public:
MyClass1(): x_( 0 ) {}
template<typename U>
MyClass1( MyClass1<Uconst& other ): x_( other.x_ )
{ say( "Template constructor 1." ); }
};

template<typename T>
class MyClass2
{
public:
int x_;
public:
MyClass2(): x_( 0 ) {}
template<typename U>
MyClass2( MyClass2<Uconst& other ): x_( other.x_ )
{ say( "Template constructor 2." ); }
MyClass2( MyClass2 const& other ): x_( other.x_ )
{ say( "Copy constructor 2." ); }
};

typedef MyClass1<intClass1;
typedef MyClass2<intClass2;

void foo( Class1 ) { say( "foo 1" ); }
void foo( Class2 ) { say( "foo 2" ); }

int main()
{
Class1 o1;
foo( o1 );
Class2 o2;
foo( o2 );
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 6 '07 #2
On Jun 6, 5:17 pm, Deep <mail2sukhd...@gmail.comwrote:
Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}

};

please guide me...

regards,
john
Not sure if this will work, but it requires prior knowledge of what
"T" actually is, so you specify a constructor for each possible type
of "T";

for example;

public:
my_class(const my_class<int>&other) : x_(other.x_) {}
my_class(const my_class<char*>&other) : x_(other.x_) { }

I hope I've understood the question correctly!

Jun 6 '07 #3
* Alf P. Steinbach:
>
You can try (although output from the copy constructor is not guaranteed)
Oh dang. In this specific case (function argument by value) as far as I
recall the copy constructor call can't be elided by the compiler.
Sorry, the rules are special-cased, so I just rely on the general rule
"Don't ever rely on side effects in copy constructors", simpler.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 6 '07 #4
On 2007-06-06 18:17, Deep wrote:
Can I use a class in this manner, where a constructor is of templated:

template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

please guide me...
Sure you can:

#include <iostream>

template<class T>
class Test
{
public:
T x_;
Test(T t) :x_(t) {}

template<class U>
Test(Test<Ut) : x_(t.x_) { }
T get() { return x_; }
};

int main()
{
Test<intt1(1);
Test<doublet2(t1);
std::cout << t2.get();
}

Notice however that this requires that x_ is public, or else the
constructor will not have access to t.x_ above unless T == U (since the
two instantiations of Test would then have different types and no access
to the other types private data.

--
Erik Wikström
Jun 6 '07 #5

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
3
by: jack | last post by:
Hi there, I have a function F(x, y, z) and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0) z0 is fixed. Suppose somebody wrote a routine called "Compute" which simply computes f(a)...
2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
3
by: JKop | last post by:
You know the whole "Linked List" concept, where each object knows where the the next one is. Well, in one of its simplest forms I have: template<class T> struct LinkedObject : virtual public T...
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
15
by: Alexander Stippler | last post by:
hi, the following does not work. I do not understand why, since it works if I replace line marked by (1) with the line below ((2)). Compiling with gcc4.0 results in "error: conversion from...
13
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
6
by: Adam Badura | last post by:
Hi! I have a class like: template<int size_integer, int size_fraction> class number { template<typename charT, typename traitsT> explicit number(const charT* textual, int radix = 10);
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>...
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
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...
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
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.