F
Floogle
I cannot figure out why the following program instantiates two type
"2" instances instead of one "1" and one "2".
Could someone plz enlighten me?
Many thanks
Here's the code
-------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Base
{
public:
virtual int GetType()const=0;
};
class Dev1 : public Base
{
public:
int GetType()const{return 1;}
};
class Dev2 : public Base
{
public:
int GetType()const{return 2;}
};
struct BaseHolder
{
Base* pBase;
BaseHolder(Base* pB)Base(pB)
{}
};
template <class T>
BaseHolder CreateABaseHolder()
{
return BaseHolder(new T());
}
int main()
{
BaseHolder h1 = CreateABaseHolder<Dev1>();
BaseHolder h2 = CreateABaseHolder<Dev2>();
cout << "\nID of base in h1 (should be == 1) = " <<
h1.pBase->GetType();
cout << "\nID of base in h2 (should be == 2)= " <<
h2.pBase->GetType();
return 0;
}
-------------------------------------------------------------------------
"2" instances instead of one "1" and one "2".
Could someone plz enlighten me?
Many thanks
Here's the code
-------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Base
{
public:
virtual int GetType()const=0;
};
class Dev1 : public Base
{
public:
int GetType()const{return 1;}
};
class Dev2 : public Base
{
public:
int GetType()const{return 2;}
};
struct BaseHolder
{
Base* pBase;
BaseHolder(Base* pB)Base(pB)
{}
};
template <class T>
BaseHolder CreateABaseHolder()
{
return BaseHolder(new T());
}
int main()
{
BaseHolder h1 = CreateABaseHolder<Dev1>();
BaseHolder h2 = CreateABaseHolder<Dev2>();
cout << "\nID of base in h1 (should be == 1) = " <<
h1.pBase->GetType();
cout << "\nID of base in h2 (should be == 2)= " <<
h2.pBase->GetType();
return 0;
}
-------------------------------------------------------------------------