Solved Compiler does not produce .o file , just a moc_.o and moc_.cpp files.
-
interface class -> a class with all virtual functions assigned a zero.
in my case IBase is a pure virtual Abstract class , no MACROS , no inheritance from QObject. But, a derivative of the IBase also derives from the QGraphicsObject , so it is possible to compile and link the code. I know that because it does in another project.
Hopefully, it clarifies the situation a little.
-
@ddze
Then you can't do this:qobject_cast<IBase*>(event->source())
If you need to cast to that base pointer, then use ordinary
dynamic_cast
. -
I have resolved it , that line of the code served only as check that an instance of the IBase exists prior to proceeding further.
By removing that line and subsequent lines that check for the NULL pointer compiles and I will have to sort "eventual" point of failure by different mean.
Still, not sure why that line compiles in another program.
Kind Regards
-
Still, not sure why that line compiles in another program.
This seems doubtful, but it doesn't matter. I'm glad it works and for this particular case you could use:
dynamic_cast<IBase*>(event->source())
instead of
qobject_cast
and everything should compile and run fine.Cheers!
-
normally I do , I do not know why I did not try that one in this case. probably I had thought it was not the type casting causing the trouble. Though , when I think about it , it is actually very relevant.
Good about this is to learn that the Abstract classes may be part of the static libs.
-
dynamic_cast
did not work, but thereinterpret_cast
compiled ok. -
dynamic_cast did not work, but the reinterpret_cast was ok.
You shouldn't use
reinterpret_cast
, it's not for that purpose. What's the problem with thedynamic_cast
? -
here is the ...
error: cannot dynamic_cast 'event->QDragEnterEvent::<anonymous>.QDragMoveEvent::<anonymous>.QDropEvent::source()' (of type 'class QObject*') to type 'class IBase*' (target is not pointer or reference to complete type)
IBase source = dynamic_cast<IBase>(event->source());
^seems like that dynamic_cast cannot cast if the object is not instantiated completely ...
-
IBase source = dynamic_cast<IBase>(event->source());
I don't see the stars ...!
dynamic_cast
works only on pointers (or references), so this line is quite simply invalid. You can't have polymorphic cast on an object, it just doesn't make sense. You can however do it on a pointer to that object.IBase * source = dynamic_cast<IBase *>(event->source());
Should work just fine.
-
here is the line
IBase *source = dynamic_cast<IBase*>(event->source());
, not the pointer -
@ddze
Okay then, your previous post didn't reflect that. Anyway, have you included the header where theIBase
class resides?PS:
Oh, I see, because you pasted the code as text, and the forum parser recognised the the asterisks and made the text italic ... heh. -
you are 100% correct , it was the header file missing .... . The other project has the header IBase , that is why it compiles .... probably I deleted it accidentally or by trying the things out. (Should use a diff from now on)
-
once on this topic , what would happen if one has a cross-reference include headers ?
I noticed few times that only if I declare a class forward declaration skipping an include of the header to be able to compile. Would in that case be ok to use
reinterpret_cast
as thedynamic_cast
may needed the full header?probably this applies
An expression of integral, enumeration, pointer, or pointer-to-member type can be converted to its own type. The resulting value is the same as the value of expression. (since C++11)
Ref: reinterpret_cast
-
@ddze
No, not at all.dynamic_cast
is polymorphic cast, it goes looks up the virtual table and makes safe casting for object pointers.reinterpret_cast
is a different kettle of fish entirely, it just switches from one "interpretation" of data to another (whence the name). So it's completely legal to cast pretty much anything withreinterpret_cast
but it's a bad idea, because it can be quite error-prone.You can do this:
struct SomeStruct { int a; double b; QString x; }; SomeStruct * variable = new SomeStruct; char * bytes = reinterpret_cast<char *>(variable); //< This is valid, compiles and runs just fine. Only problem is, you may accidentally modify memory that's not yours ...
And you have the whole structure as an array of bytes ... but it's ill advised to do such things in general.
reinterpret_cast
is similar to the functionality theunion
provides.
So as rule of thumb, do not usereinterpret_cast
. -
Thanks a lot.