T
tthunder
Hi @all,
Perhaps some of you know my problem, but I had to start a new thread.
The old one started to become very very confusing.
Here clean code (which compiles well with my BCB 6.0 compiler). You can
find a problem there, which cannot be solved by dynamic_cast, because
the pointers can be NULL, and I only want to know, if the pointer type
is derived from another pointer type.
BTW: I cannot create a temporary instance, because used classes can be
abstract (pure virtual functions)...
Here the code:
--------------------
class c
{
virtual ~c() {}
};
class c1 : public c
{
virtual void foo() = 0;
....
};
class c2 : public c1
{
....
};
class c3 : public c2
{
....
};
class bad1 : public c
{
.....
};
--------------------
class Base
{
public:
c *ptr;
virtual ~Base() {}
};
template <class T>
class Checker : public Base
{
public:
bool check(Base &base)
{
---- PSEUDO (with dynamic_cast, which does not work, if
ptr is NULL - a temporary instance cannot be created, because T can
always be a abstract class):
if (dynamic_cast<T *> (base.ptr))
return true;
return false;
----
}
void setPtr(c *p_ptr)
{
ptr = dynamic_cast<T *> (p_ptr);
}
};
main()
{
Checker<c1> checker1;
Checker<c2> checker2;
Checker<c3> checker3;
Checker<bad1> checker4;
checker1.check(checker1); // return true
checker1.check(checker2); // return true
checker1.check(checker3); // return true
checker1.check(checker4); // return false, because bad1 cannot
be casted to c1
}
--------------
Do *****NOT***** use dynamic_cast!!!!!!
Do *****NOT***** use dynamic_cast!!!!!!
Do *****NOT***** use dynamic_cast!!!!!!
Please read the following line in my code:
---- PSEUDO (with dynamic_cast, which does not work, if ptr is NULL)
Perhaps some of you know my problem, but I had to start a new thread.
The old one started to become very very confusing.
Here clean code (which compiles well with my BCB 6.0 compiler). You can
find a problem there, which cannot be solved by dynamic_cast, because
the pointers can be NULL, and I only want to know, if the pointer type
is derived from another pointer type.
BTW: I cannot create a temporary instance, because used classes can be
abstract (pure virtual functions)...
Here the code:
--------------------
class c
{
virtual ~c() {}
};
class c1 : public c
{
virtual void foo() = 0;
....
};
class c2 : public c1
{
....
};
class c3 : public c2
{
....
};
class bad1 : public c
{
.....
};
--------------------
class Base
{
public:
c *ptr;
virtual ~Base() {}
};
template <class T>
class Checker : public Base
{
public:
bool check(Base &base)
{
---- PSEUDO (with dynamic_cast, which does not work, if
ptr is NULL - a temporary instance cannot be created, because T can
always be a abstract class):
if (dynamic_cast<T *> (base.ptr))
return true;
return false;
----
}
void setPtr(c *p_ptr)
{
ptr = dynamic_cast<T *> (p_ptr);
}
};
main()
{
Checker<c1> checker1;
Checker<c2> checker2;
Checker<c3> checker3;
Checker<bad1> checker4;
checker1.check(checker1); // return true
checker1.check(checker2); // return true
checker1.check(checker3); // return true
checker1.check(checker4); // return false, because bad1 cannot
be casted to c1
}
--------------
Do *****NOT***** use dynamic_cast!!!!!!
Do *****NOT***** use dynamic_cast!!!!!!
Do *****NOT***** use dynamic_cast!!!!!!
Please read the following line in my code:
---- PSEUDO (with dynamic_cast, which does not work, if ptr is NULL)