473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

check derivations of pointer types (without dynamic_cast!)

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)

Jul 23 '05 #1
5 2018
tt******@gmx.de wrote:
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).
I bet you 1000 Euros that it doesn't. I left it in just to show you
that you lied when you said that it was "clean" and "compiled well".
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.
See Boost, ::boost::is_base_and_derived<T,U> template.

And you should try to use correct terminology. In C++ there is no
way to "derive a pointer type from another pointer type". There is
a way, however, to derive a type from another type, and to declare
a pointer to it. Yes, I knew what you meant but only because I've
been reading this newsgroup for so long. Next time you explain it
to somebody else, you better do it right.
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)


V
Jul 23 '05 #2
On 2005-06-25 09:51:40 +0100, tt******@gmx.de said:
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;
----
}


Your wording is a bit confusing as to where the problem lies.
Is it that dynamic_cast<T *>(x) fails when x is 0 ?

If yes, there's a simple fix:

bool check(Base & b) {
return b.ptr == 0 ? false : dynamic_cast<T *> (b.ptr) ;
}

otherwise, I don't understand the problem :(
--
JFB

Jul 23 '05 #3
::boost::is_base_and_derived<T*,U> does not work in my case!
For this function I need two variable types. I only have one at the
time I want to check them.

If I am in my class "Checker", I have got T, ok! But how to get U?
I only have a reference to Base (which is "Checker", too). I do not
know, if this Base is Checker<c1>, Checker<c2>, Checker<c3>,... I can
try, ok, but this is not what I want to do.

Jul 23 '05 #4
Well, your check is not necessary, because dynamic_cast does return
NULL (FALSE), if ptr is NULL!

But I don't want to return NULL! If the types are derived (or the same)
I want to return TRUE even if ptr is NULL.

It does not matter for me which value is in my ptr, I am only
interested in its type.

And yes again, I know, that dynamic_cast cannot do this! I thought
about things like RTTI, but even there I cannot see functions which can
solve my problem.

Jul 23 '05 #5

<tt******@gmx.de> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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;

You have declared ptr as the type c*. You want to know what type it is?
It's type is c*, because that's what it's declared as.
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);
}
};


What exactly are you trying to accomplish?

If ptr has been instantiated, then you can use dynamic_cast to verify its
type, right? (Although, I fail to see a need to do so.) And if it's NULL,
then it hasn't been instantiated, so the ONLY thing that can be said about
it is that it's a c* pointer. If that's ok, then return TRUE when ptr is
NULL. If it's not ok, then what exactly do you want?

You code does not show how ptr gets set. There's a setPtr function, but
nothing ever calls it. And there's no constructor shown, so as far as I can
tell, ptr is NEVER set to anything!

Without knowing what it is you're trying to accomplish, we can't give you a
reasonable solution to the problem.

But if all you want to do is find out the type of object pointed to by ptr,
then obviously it's impossible unless it actually DOES point to something,
right? Otherwise, it's simply a NULL pointer of type c*. Given the code
you've shown, that tells you nothing about the type passed as a parameter to
Checker<>. But that's the fault of your code sample which isn't using that
parameter in anything but the check and setPtr functions, as far as I can
tell.

Either fix the example and explain what you're actually trying to
accomplish, or else accept that an uninitialized base class pointer is
simply that, an uninitialized base class pointer, and has no relation
whatsoever to the parameter passed in to the constructor (unless and until
you set up that relationship somehow, such as by instantiating an object for
ptr to point to).

One final thing: if you have a member pointer, chances are you need to
initialize it in the constructor (at least to NULL, if nothing else), plus
you'll likely need to delete it in your base class destructor, and also add
a copy constructor and assignment operator. (See the "rule of three".)

-Howard


Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: laredotornado | last post by:
Hello, Are all pointer types the same length? My instinct tells me yes, but I just wanted to confirm with the experts. So if I have typedef struct { char* field1; int field2; }...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
11
by: jois.de.vivre | last post by:
I am interfacing with a third party API (written in C, if that matters) that has an "event handler" function with the following definition: void event_handler(int event_code, unsigned long...
2
by: NotGuru | last post by:
I was writing some C extensions for Python and use PyTupleType_Check extensively. I found that all the PySomeType_Check macros directly delegate the job to PyObject_TypeCheck(op, &PyType_Type). The...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
12
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
1
by: ladesidude | last post by:
Hi, I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.