Why do I get a compilation error when I call a base-class function with a pointer to a pointer to an inherited class?
Example:
class cFoo {};
class cBar : public cFoo {};
void func1(cFoo *) {} // base class
void func2(cFoo **) {} // base class
void main(void)
{ cBar bar, *pbar; // inherited class
func1(&bar); // compiles ...
Hi all!
I'm currently working on integrating a third-party package that uses lots of RTTI stuff on a non-RTTI platform (Android). Basically, I did my own RTTI implementation but I'm stuck on a problem.
The issue is that a lot of classes are having the diamond inheritance problem since all the classes derive from the same base class (ob...
Is there a way in C++ to construct your class such that given a pointer to your class you can instruct dynamic_cast<>() how to cast to another class for which you are wrapping the implementation? Would operator cast do the trick? Imagine I have an Abstract interface base class and derive a concreteA from this as well as concreteB, but ...
The dynamic_cast operator is returning zero (0) when I apply to a pointer that points to an instance of a multiply inherited object. I don't understand why.
The hierarchy:
class Field_Interface
{
public:
virtual const std::string get_field_name(void) const = 0; // Just to make the class abstract.
};
class Record_ID_Interface
{...
Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
what is difference between static and dynamic cast in c++?
...
Environment: Linux C++ / Qt 4x
I do not understand why the following downcast returns NULL? I pasted base and derived class below.
Thanks in advance for any tips or suggestions.
-Ed
void MainWindow::onRtledaEventHandler(fes::EventArgs eventArgs)
{
// This cast returns a NULL ?
fes::AtsCommandEventArgs* atsCommandEventArgs = d...
I want to cast this:
class Base
{
public:
virtual ~Base(){};
};
class Der : public Base {};
int main()
{
const Base* base = new Der;
Der* der = dynamic_cast<Der*>(base); // Error
return 0;
}
What should I do?
I tried to put: const Der* der = dynamic_cast<Der*>(base); to mantain the const but this doesn't work.
...
(I applogise in advance for the length of this post, however the problem is fairly complex - hopefully everything is clear however I have simplified the scenario a lot and so there is a good chance that I've missed a vital fact, or there is something that I've not explained.)
Setting the scene
I'm using code generation to generate C# w...
For instance I have code like that
class Base1
{
virtual void wonderFULL() = 0;
};
class Base2
{
// all this weird members
};
class Derived : public Base1, public Base2
{
// not so weird members
};
int main()
{
Derived Wonder;
magicFunction(&Wonder);
return 0;
}
void magicFunction(Base2 *ptr)
{
if (Base1 *b1 = dynamic_...
The below code explains the problem. Fill in same_sub_class to detect if
the two pointers to virtual base class A are in fact the same concrete
class.
struct A {
...
}:
struct B : public A {
...
}:
struct C : public A {
...
}
bool same_sub_class(A * a1, A * a2){
// Fill this in to return true if a1 and a2 are
//...
(Please no advise that I should abstract X more and add another method to it.)
In C++, when I have a variable x of type X* and I want to do something specific if it is also of type Y* (Y being a subclass of X), I am writing this:
if(Y* y = dynamic_cast<Y*>(x)) {
// now do sth with y
}
The same thing seems not possible in Java (or...
Possible Duplicate:
How is dynamic_cast typically implemented?
Hi!
how does dynamic_cast actually work? where does the runtime know from whether some piece of memory actually fits the datatype or not?
Thanks!
...
Before reading the question:
This question is not about how useful it is to use dynamic_cast. Its just about its performance.
I've recently developed a design where dynamic_cast is used a lot.
When discussing it with co-workers almost everyone says that dynamic_cast shouldn't be used because of its bad performance (these are co-workers ...