Hello guys,
here I have a snippet of code and it would be very helpful, if you can explain why the second dynamic_cast fails and if the static_cast is the way to do it.
There are three classes Base, MyString and MyList. MyString and MyList are derived from Base. MyList is a template-class
#include <QCoreApplication>
#include <QDebug>
class Base
{
public:
Base(){
}
virtual ~Base(){
qDebug() << "~Base()";
}
};
class MyString : public Base
{
public:
public:
MyString(const QString& str) : Base(), str(str){
}
virtual ~MyString(){
qDebug() << "~MyString()";
}
};
template <class T>
class MyList : public QList<T>, public Base
{
public:
MyList<T>():QList<T>(){
}
MyList<T>(QList<T> h):QList<T>(h){
}
virtual ~MyList<T>(){
qDebug() << "~MyList()";
}
};
Base* createMyList()
{
MyList<Base*>* myList = new MyList<Base*>();
myList->append(new MyString("joey potter"));
myList->append(new MyString("allen harper"));
myList->append(new MyString("charlie harper"));
myList->append(new MyString("john mcclane"));
return myList;
}
int main(int argc, char *argv[])
{
Q_UNUSED(a);
MyList<Base*>* myList = new MyList<Base*>();
myList->append(new MyString("string1"));
//I know that first element is from class MyString so static_cast is sufficient
Q_ASSERT(dynamic_cast<MyString*>(myList->first()) != NULL);// ok
Q_ASSERT(static_cast<MyString*>(myList->first()) != NULL);// ok
MyList<MyString*>* anotherList = static_cast<MyList<MyString*>*>(createMyList());
Q_ASSERT(anotherList != NULL);// ok
MyList<MyString*>* differentList = dynamic_cast<MyList<MyString*>*>(createMyList());
Q_ASSERT(differentList != NULL);// ASSERT: "differentList != NULL" in file ../dynamic_cast_test/main.cpp, line 74
//free ram
QListIterator<Base*> lit1(*myList);
while(lit1.hasNext()){
delete lit1.next();
}
delete myList;
QListIterator<MyString*> lit2(*anotherList);
while(lit2.hasNext()){
delete lit2.next();
}
delete anotherList;
QListIterator<MyString*> lit3(*differentList);
while(lit3.hasNext()){
delete lit3.next();
}
delete differentList;
return 0;
}
#include <QCoreApplication>
#include <QDebug>
class Base
{
public:
Base(){
}
virtual ~Base(){
qDebug() << "~Base()";
}
};
class MyString : public Base
{
public:
QString str;
public:
MyString(const QString& str) : Base(), str(str){
}
virtual ~MyString(){
qDebug() << "~MyString()";
}
};
template <class T>
class MyList : public QList<T>, public Base
{
public:
MyList<T>():QList<T>(){
}
MyList<T>(QList<T> h):QList<T>(h){
}
virtual ~MyList<T>(){
qDebug() << "~MyList()";
}
};
Base* createMyList()
{
MyList<Base*>* myList = new MyList<Base*>();
myList->append(new MyString("joey potter"));
myList->append(new MyString("allen harper"));
myList->append(new MyString("charlie harper"));
myList->append(new MyString("john mcclane"));
return myList;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Q_UNUSED(a);
MyList<Base*>* myList = new MyList<Base*>();
myList->append(new MyString("string1"));
//I know that first element is from class MyString so static_cast is sufficient
Q_ASSERT(dynamic_cast<MyString*>(myList->first()) != NULL);// ok
Q_ASSERT(static_cast<MyString*>(myList->first()) != NULL);// ok
MyList<MyString*>* anotherList = static_cast<MyList<MyString*>*>(createMyList());
Q_ASSERT(anotherList != NULL);// ok
MyList<MyString*>* differentList = dynamic_cast<MyList<MyString*>*>(createMyList());
Q_ASSERT(differentList != NULL);// ASSERT: "differentList != NULL" in file ../dynamic_cast_test/main.cpp, line 74
//free ram
QListIterator<Base*> lit1(*myList);
while(lit1.hasNext()){
delete lit1.next();
}
delete myList;
QListIterator<MyString*> lit2(*anotherList);
while(lit2.hasNext()){
delete lit2.next();
}
delete anotherList;
QListIterator<MyString*> lit3(*differentList);
while(lit3.hasNext()){
delete lit3.next();
}
delete differentList;
return 0;
}
To copy to clipboard, switch view to plain text mode
Why does following cast fail?
MyList<MyString*>* differentList = dynamic_cast<MyList<MyString*>*>(createMyList())
MyList<MyString*>* differentList = dynamic_cast<MyList<MyString*>*>(createMyList())
To copy to clipboard, switch view to plain text mode
Cause MyList<Base*>* is completely different from MyList<MyString*>* and the dynamic check is technically impossible ?
But can I do a static_cast(no dynamic checks), cause I know it's a MyList<MyString*>*? Is this cast is safe?
thank you
Bookmarks