Hi
Thanks for the response. I searched the forum and found a good post explaining the operation of qobject_cast and it explains why it fails.
I also read on the proposed solutions there:
1) Using inherits() : This will not work for me since I up from a base class to a class that inherits the base class. As I understand inherits() works the other way around?
2) Creating a library with the classes in it. : I would use this as a last resort really.
For now, I think I found a different solution using reinterpret_cast. It works for now, but I'm not sure if it will work if there are more child classes present. Let me first show my code:
if (variable) {
if (reinterpret_cast<MatrixReal*> (variable))
QMessageBox::warning(0,
"Cast successful",
"Cast successful");
else
}
if (variable) {
if (reinterpret_cast<MatrixReal*> (variable))
QMessageBox::warning(0,"Cast successful","Cast successful");
else
QMessageBox::warning(0,"Cast failed","Cast failed");
}
To copy to clipboard, switch view to plain text mode
Now the question is, will this work if there is for example a MatrixComplex class as well? I would then have something like:
if (variable) {
if (reinterpret_cast<MatrixReal*> (variable))
QMessageBox::warning(0,
"Cast successful",
"Cast successful to real matrix");
else if (reinterpret_cast<MatrixComplex*> (variable))
QMessageBox::warning(0,
"Cast successful",
"Cast successful to complex matrix");
else
}
if (variable) {
if (reinterpret_cast<MatrixReal*> (variable))
QMessageBox::warning(0,"Cast successful","Cast successful to real matrix");
else if (reinterpret_cast<MatrixComplex*> (variable))
QMessageBox::warning(0,"Cast successful","Cast successful to complex matrix");
else
QMessageBox::warning(0,"Cast failed","Cast failed");
}
To copy to clipboard, switch view to plain text mode
Will this still work? I guess the answer will be that this is a bad implementation since I read everywhere that reinterpret_cast is unsafe.
If needed I can post more code and look at an alternative way of doing this. I can also give more details of why I need to do these casts in my application.
Thanks,
Jaco
Bookmarks