Archived
This forum has been archived. Please start a new discussion on GitHub.
ICE C++ question
Please forgive my C++ ignorance, but I am really a Java programmer attempting to write a C++ test and am hoping that someone can point me in the right direction. I have a slice file which essentially defines a bunch of interfaces and classes. The basic structure is I have a base class which is extended by a bunch of subclasses and I have a base service interface which is extended by subinterfaces:
ClassA
ClassB : ClassA
ServiceX {
virtual ClassA getA();
}
ServiceY : ServiceX {
getA() {
return ClassB();
}
}
In C++ the interface generates something like:
ClassAPtr getA();
where ClassAPtr is the rerference counted pointer (IceInternal::Handle) which is typedefed in the generated header file. My question is, how does one properly downcast this to the actual implementation class. In Java one would do something like:
ClassB val = (ClassB)ServiceY.getA();
How do I accomplish this same functionality in C++?
Thanks again,
Mayer
ClassA
ClassB : ClassA
ServiceX {
virtual ClassA getA();
}
ServiceY : ServiceX {
getA() {
return ClassB();
}
}
In C++ the interface generates something like:
ClassAPtr getA();
where ClassAPtr is the rerference counted pointer (IceInternal::Handle) which is typedefed in the generated header file. My question is, how does one properly downcast this to the actual implementation class. In Java one would do something like:
ClassB val = (ClassB)ServiceY.getA();
How do I accomplish this same functionality in C++?
Thanks again,
Mayer
0
Comments
-
Hi,
In C++, we usually recommend that you create a smart pointer type for implementation classes. For example:// C++ class ClassAI : public ClassA { ... }; typedef IceUtil::Handle<ClassAI> ClassAIPtr;
Having done that, you can downcast quite easily:ClassAPtr a = ... ClassAIPtr ai = ClassAIPtr::dynamicCast(a); if(ai) { // dynamicCast may return nil! }
In the absence of a typedef for your implementation class, you can use the normal C++ mechanism:ClassAPtr a = ... ClassAI* ai = dynamic_cast<ClassAI*>(a.get()); if(ai) { ... }
Hope that helps,
- Mark0 -
After re-reading your post, it sounds like you simply want to downcast to another Slice-defined type. In this case you would use the same technique I described above:
ClassAPtr a = ... ClassBPtr b = ClassBPtr::dynamicCast(a); if(b) { ... }
- Mark0