Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

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

* 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<U> const& 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<U> const& other ): x_( other.x_ )
     { say( "Template constructor 2." ); }
     MyClass2( MyClass2 const& other ): x_( other.x_ )
     { say( "Copy constructor 2." ); }

};

typedef MyClass1<int> Class1;
typedef MyClass2<int> Class2;

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?
On Jun 6, 5:17 pm, Deep <mail2sukhd@gmail.com> wrote:

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!

* 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?

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<U> t) : x_(t.x_) { }
        T get() { return x_; }

};

int main()
{
        Test<int> t1(1);
        Test<double> t2(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 Wikstrm

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc