D
Dan Huantes
I was presented a problem today where a class had member
variable that was an object of a templated class. The
class wanted to instantiate the object as a private member
variable and call a constructor other than the default
constructor. I couldn't figure out why it wasn't working
so I changed the member variable to a pointer to an object
instead and intialized it in the constructor and destroyed
in the destructor. It bothered me that I had to do this
work around so I generated the small app shown below to
see if I could duplicate the problem and I did. It seems
like you can't have an object that is based on a templated
class call anything other than the default constructor
when created on the stack. I looked in the Stroustrup
book see if there was a reference to this language feature
and couldn't find it. Am I missing something or is my
work around the only way to do this? Thanks!
Dan
P.S.
I compiled this using Visual C++ .Net. I haven't tried it
on 6.0 but it should work(I think...templates sometimes
give 6.0 problems but this is a very simple example)
************Example Follows**********************
// TestApp.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
//Templated Class
template <class T>
class DanTest
{
public:
//Default Constructor
DanTest():first(0),second(0){};
//Another Constructor
DanTest(int one, int two):first(one),second(two){};
private:
int first;
int second;
};
//Cleanup the Syntax with typedef
typedef DanTest<double> DanTestD;
//Class with a DanTestD member
class DanTestUser
{
public:
double value;
double value2;
static const int n_const_int = 5;
DanTestUser():value(0.0),value2(0.0){};
private:
//This works fine with the default constructor but
//the compiler throws a C2061 error when I try
//to user the alternate constructor.
//
DanTestD myDT(n_const_int,n_const_int);
};
int _tmain(int argc, _TCHAR* argv[])
{
DanTestUser x;
return 0;
}
variable that was an object of a templated class. The
class wanted to instantiate the object as a private member
variable and call a constructor other than the default
constructor. I couldn't figure out why it wasn't working
so I changed the member variable to a pointer to an object
instead and intialized it in the constructor and destroyed
in the destructor. It bothered me that I had to do this
work around so I generated the small app shown below to
see if I could duplicate the problem and I did. It seems
like you can't have an object that is based on a templated
class call anything other than the default constructor
when created on the stack. I looked in the Stroustrup
book see if there was a reference to this language feature
and couldn't find it. Am I missing something or is my
work around the only way to do this? Thanks!
Dan
P.S.
I compiled this using Visual C++ .Net. I haven't tried it
on 6.0 but it should work(I think...templates sometimes
give 6.0 problems but this is a very simple example)
************Example Follows**********************
// TestApp.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
//Templated Class
template <class T>
class DanTest
{
public:
//Default Constructor
DanTest():first(0),second(0){};
//Another Constructor
DanTest(int one, int two):first(one),second(two){};
private:
int first;
int second;
};
//Cleanup the Syntax with typedef
typedef DanTest<double> DanTestD;
//Class with a DanTestD member
class DanTestUser
{
public:
double value;
double value2;
static const int n_const_int = 5;
DanTestUser():value(0.0),value2(0.0){};
private:
//This works fine with the default constructor but
//the compiler throws a C2061 error when I try
//to user the alternate constructor.
//
DanTestD myDT(n_const_int,n_const_int);
};
int _tmain(int argc, _TCHAR* argv[])
{
DanTestUser x;
return 0;
}