Template Class Issues in C++.
-
N a v a n e e t h wrote:
You can return pointer to this abstract class instantiated with proper derived one.
Can you demonstrate how to do it? :confused:
class DummyBase {};
class B : public DummyBase{
// Class Definitions
};
class C : public DummyBase{
// Class Definitions
};class A{
public:
B obj1;
C obj2;
DummyBase* search(int condition) //What do I specify return type???
{
if(condition==1)
return &obj1;
if(condition==2)
return &obj2;
if(condition==3)
return this;
}
};Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
N a v a n e e t h wrote:
You can return pointer to this abstract class instantiated with proper derived one.
Can you demonstrate how to do it? :confused:
Here is a trivial example.
class Car{
public:
virtual string SayName() = 0;
};class Mercedez : public Car{
public:
string SayName(){
return "Mercedez";
}
};class Ferrari : public Car{
public:
string SayName(){
return "Ferrari";
}
};Car* CreateACar(string carType){
if(carType == "Mercedez")
return new Mercedez;
else if(carType == "Ferrari")
return new Ferrari;
}Use it like
Car* car = CreateACar("Mercedez");
std::cout << car->SayName();Read about factory design pattern. :)
Navaneeth How to use google | Ask smart questions
-
N a v a n e e t h wrote:
You can return pointer to this abstract class instantiated with proper derived one.
Can you demonstrate how to do it? :confused:
I had implemented
DummyBase
this way only. :) But there was problem accessing functions ofclass B & C
throughmain()
. After figuring out why, I have another serious problem now. Actual Implementations ofclass B and C are: class DummyBase {}; template <'class abc'> class Base{ public: abc func1(); }; class B : public Base`<datatype1`>, DummyBase{ // Class Definitions }; class C : public Base`<datatype2`>, DummyBase{ // Class Definitions }; class A: public Base`<datatype3`>, DummyBase{ public: B obj1; C obj2; DummyBase* search(int condition) //What do I specify return type??? { if(condition==1) return &obj1; if(condition==2) return &obj2; if(condition==3) return this; } }; void main() { DummyBase *ptr; A objA; int n; cin>>n; ptr = objA.search(n); ptr->func1(); //Error : Since DummyBase does not contain func1() }
Now the problem is thatfunc1()
cannot be included inDummyBase
since return type of func1() is a template parameter. How to solve this..... :confused: -
I had implemented
DummyBase
this way only. :) But there was problem accessing functions ofclass B & C
throughmain()
. After figuring out why, I have another serious problem now. Actual Implementations ofclass B and C are: class DummyBase {}; template <'class abc'> class Base{ public: abc func1(); }; class B : public Base`<datatype1`>, DummyBase{ // Class Definitions }; class C : public Base`<datatype2`>, DummyBase{ // Class Definitions }; class A: public Base`<datatype3`>, DummyBase{ public: B obj1; C obj2; DummyBase* search(int condition) //What do I specify return type??? { if(condition==1) return &obj1; if(condition==2) return &obj2; if(condition==3) return this; } }; void main() { DummyBase *ptr; A objA; int n; cin>>n; ptr = objA.search(n); ptr->func1(); //Error : Since DummyBase does not contain func1() }
Now the problem is thatfunc1()
cannot be included inDummyBase
since return type of func1() is a template parameter. How to solve this..... :confused: -
I had implemented
DummyBase
this way only. :) But there was problem accessing functions ofclass B & C
throughmain()
. After figuring out why, I have another serious problem now. Actual Implementations ofclass B and C are: class DummyBase {}; template <'class abc'> class Base{ public: abc func1(); }; class B : public Base`<datatype1`>, DummyBase{ // Class Definitions }; class C : public Base`<datatype2`>, DummyBase{ // Class Definitions }; class A: public Base`<datatype3`>, DummyBase{ public: B obj1; C obj2; DummyBase* search(int condition) //What do I specify return type??? { if(condition==1) return &obj1; if(condition==2) return &obj2; if(condition==3) return this; } }; void main() { DummyBase *ptr; A objA; int n; cin>>n; ptr = objA.search(n); ptr->func1(); //Error : Since DummyBase does not contain func1() }
Now the problem is thatfunc1()
cannot be included inDummyBase
since return type of func1() is a template parameter. How to solve this..... :confused:That's a tricky one. I'd probably try it like this: Add virtual destructors to DummyBase, B and C, to ensure a v-table. Then you can use dynamic_cast to work out which typ has been returned:
class DummyBase { virtual ~DummyBase() {} };
template <'class abc'>
class Base{
public:
abc func1();
};class B : public Base`<datatype1`>, DummyBase{
virtual ~B() {}
// Class Definitions
};
class C : public Base`<datatype2`>, DummyBase{
virtual ~C() {}
// Class Definitions
};class A: public Base`<datatype3`>, DummyBase{
public:
B obj1;
C obj2;
DummyBase* search(int condition) //What do I specify return type???
{
if(condition==1)
return &obj1;
if(condition==2)
return &obj2;
if(condition==3)
return this;
}
};void main()
{
DummyBase *ptr;
A objA;
int n;
cin>>n;
ptr = objA.search(n);
if (B* bPtr = dynamic_cast<B*>(ptr))
{ bPtr->func1(); }
if (C* bPtr = dynamic_cast<C*>(ptr))
{ bPtr->func1(); }
}Yeah, it sucks because you're taking so many decisions. There are probably better ways, but I've not really put sufficient thought into it to think of a better one. Possibly something like Boost.Variant or Boost.Any might make things nicer.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
That's a tricky one. I'd probably try it like this: Add virtual destructors to DummyBase, B and C, to ensure a v-table. Then you can use dynamic_cast to work out which typ has been returned:
class DummyBase { virtual ~DummyBase() {} };
template <'class abc'>
class Base{
public:
abc func1();
};class B : public Base`<datatype1`>, DummyBase{
virtual ~B() {}
// Class Definitions
};
class C : public Base`<datatype2`>, DummyBase{
virtual ~C() {}
// Class Definitions
};class A: public Base`<datatype3`>, DummyBase{
public:
B obj1;
C obj2;
DummyBase* search(int condition) //What do I specify return type???
{
if(condition==1)
return &obj1;
if(condition==2)
return &obj2;
if(condition==3)
return this;
}
};void main()
{
DummyBase *ptr;
A objA;
int n;
cin>>n;
ptr = objA.search(n);
if (B* bPtr = dynamic_cast<B*>(ptr))
{ bPtr->func1(); }
if (C* bPtr = dynamic_cast<C*>(ptr))
{ bPtr->func1(); }
}Yeah, it sucks because you're taking so many decisions. There are probably better ways, but I've not really put sufficient thought into it to think of a better one. Possibly something like Boost.Variant or Boost.Any might make things nicer.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
Yeah, it sucks because you're taking so many decisions.
Yes, that's a quite heck of a job. Actually
search() function
is like a framework function which links 2 separate applications. Now if this approach is to be implemented then I have to do a lot of coding at caller side. Also there are about 40-50 functions(may increase with further development) which would callsearch
. And this approach is quite rigid and for instance if newclass M
object is introduced inclass A
a lot work would be required. So a more efficient flexible solution is required. :confused:Stuart Dootson wrote:
But I've not really put sufficient thought into it to think of a better one.
If possible could you please spare some time to think of a better one??? ;P I have nearly wasted a day trying to find workarounds for it and now i cannot think more since here in India it's midnight now and i'm feeling sleepy. :zzz:
Stuart Dootson wrote:
Possibly something like Boost.Variant or Boost.Any might make things nicer.
I am not familiar with Boost libraries. So how could they be implemented??
-
Stuart Dootson wrote:
Yeah, it sucks because you're taking so many decisions.
Yes, that's a quite heck of a job. Actually
search() function
is like a framework function which links 2 separate applications. Now if this approach is to be implemented then I have to do a lot of coding at caller side. Also there are about 40-50 functions(may increase with further development) which would callsearch
. And this approach is quite rigid and for instance if newclass M
object is introduced inclass A
a lot work would be required. So a more efficient flexible solution is required. :confused:Stuart Dootson wrote:
But I've not really put sufficient thought into it to think of a better one.
If possible could you please spare some time to think of a better one??? ;P I have nearly wasted a day trying to find workarounds for it and now i cannot think more since here in India it's midnight now and i'm feeling sleepy. :zzz:
Stuart Dootson wrote:
Possibly something like Boost.Variant or Boost.Any might make things nicer.
I am not familiar with Boost libraries. So how could they be implemented??
With Boost, this is the best I can think of (note that I've substituted int, cahr, float for the template parameters):
#include <iostream>
#include <boost/variant.hpp>template <class abc>
class Base{
public:
abc func1();
};class B : public Base<int>{
public:
virtual ~B() {}
// Class Definitions
};
class C : public Base<float>{
public:
virtual ~C() {}
// Class Definitions
};class A: public Base<char>{
public:
B obj1;
C obj2;
boost::variant<A*, B*, C*> search(int condition) //What do I specify return type???
{
if(condition==1)
return &obj1;
if(condition==2)
return &obj2;
if(condition==3)
return this;
}
};int main(int, char**)
{
A objA;
int n;
std::cin>>n;
boost::variant<A*, B*, C*> v = objA.search(n);
if (B* b = boost::get<B*>(v))
{
b->func1();
}
else if (C* c = boost::get<C*>(v))
{
c->func1();
}
return 0;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
With Boost, this is the best I can think of (note that I've substituted int, cahr, float for the template parameters):
#include <iostream>
#include <boost/variant.hpp>template <class abc>
class Base{
public:
abc func1();
};class B : public Base<int>{
public:
virtual ~B() {}
// Class Definitions
};
class C : public Base<float>{
public:
virtual ~C() {}
// Class Definitions
};class A: public Base<char>{
public:
B obj1;
C obj2;
boost::variant<A*, B*, C*> search(int condition) //What do I specify return type???
{
if(condition==1)
return &obj1;
if(condition==2)
return &obj2;
if(condition==3)
return this;
}
};int main(int, char**)
{
A objA;
int n;
std::cin>>n;
boost::variant<A*, B*, C*> v = objA.search(n);
if (B* b = boost::get<B*>(v))
{
b->func1();
}
else if (C* c = boost::get<C*>(v))
{
c->func1();
}
return 0;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Thanks for sparing some time.... :) But isn't this approach similar to previous approach??? Still I have to code a lot at caller side as in last solution... Is there no way to reduce coding at caller side??
NeoAks007 wrote:
Is there no way to reduce coding at caller side??
Not really - you have different return types for the different implementations of func1 - you have to handle those differently. If you want to handle them n the same way, then why not make them the same?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
NeoAks007 wrote:
Is there no way to reduce coding at caller side??
Not really - you have different return types for the different implementations of func1 - you have to handle those differently. If you want to handle them n the same way, then why not make them the same?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
If you want to handle them n the same way, then why not make them the same?
It cannot be made same because their functionalities are different in different classes... I understand that this is holiday but still if anyone reading this thread comes up with an idea, do post a solution please... I need it seriously..... I am still listening.....
modified on Sunday, March 15, 2009 1:36 PM
-
Stuart Dootson wrote:
If you want to handle them n the same way, then why not make them the same?
It cannot be made same because their functionalities are different in different classes... I understand that this is holiday but still if anyone reading this thread comes up with an idea, do post a solution please... I need it seriously..... I am still listening.....
modified on Sunday, March 15, 2009 1:36 PM
I think you're making this way too complicated. If you change the return type, the code handling that will have to change as well.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
I think you're making this way too complicated. If you change the return type, the code handling that will have to change as well.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke