By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

can constructor be a template?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
* 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

P: n/a
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

P: n/a
* 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.