Different types of objects created when using various template constructors
Oct 30, 2011 at 8:45pm UTC
Hi,
I need help with following problem:
Different types of objects created when using various template constructors, with same template types.
The code that is describes problem:
file: template.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef TEMPLATE_H_INCLUDED
#define TEMPLATE_H_INCLUDED
using namespace std;
template <typename T, typename U>
class cTemplate_t
{
public :
cTemplate_t (); //declaration of Defaults Constructor
cTemplate_t (const T&, const U&); //declaration of Constructor
private :
void * _var1;
U* _var2;
};
template <typename T, typename U> //Definition of Defaults Constructor
cTemplate_t<T,U>::cTemplate_t () : _var1(0), _var2(0)
{
}
template <typename T, typename U> //Definition of Constructor
cTemplate_t<T,U>::cTemplate_t (const T& tRef, const U& uRef) : _var1(0), _var2(0)
{
}
#endif // TEMPLATE_H_INCLUDED
file: main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <typeinfo>
#include "template.h"
using namespace std;
int main()
{
cTemplate_t<int ,int > template1;
cTemplate_t<int ,int > template2 (1, 2);
cout << typeid (template1).name() << endl;
cout << typeid (template2).name() << endl;
return 0;
}
I expected that
console output in both
cout lines will be same. But
template1 object type differ from type of object
template2 .
Why do I get different types of objects if their template parameters are same?
Last edited on Oct 30, 2011 at 8:51pm UTC
Oct 30, 2011 at 8:56pm UTC
They both give the same output for me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <typeinfo>
template <typename T, typename U>
class A{
public :
A(): a(0), b(0){}
A(const T&, const U&): a(0), b(0){}
private :
void *a;
U *b;
};
int main(){
A<int ,int > a;
A<int ,int > b(1, 2);
std::cout << typeid (a).name() << std::endl;
std::cout << typeid (b).name() << std::endl;
return 0;
}
Oct 30, 2011 at 9:15pm UTC
Hi helios,
Thanks for help.
I will check my compiler installation.
Topic archived. No new replies allowed.