473,729 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2036
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_bas e_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_bas e_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.d e> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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
1461
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; } myStruct; myStruct *a;
5
5347
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: const char * : "pointer to const-qualified char". char *: "pointer to char". Are these pointed-to types compatibles?
0
2033
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 this topic getting lost before people interested in the problem notice it. The important tricks to the solution are two: 1) make the custom classes take a TEMPLATE argument which defines their BASE class 2) EMBED the custom classes in a "Traits"...
11
7293
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 user_data); The function takes an event code along with the user data but does not act on or change the user data. In my application, I want to pass a pointer as the user data. This pointer is to an array allocated with new, say, as follows:
2
2120
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 PyObject_TypeCheck(op, &PyType_Type) is again a macro and defined as ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp))) in object.h. My questions is: is it necessary to check the null pointer in the macro or it's a job for the...
5
3911
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 static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; // <--- this
12
3934
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
1
7331
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. #include <stdio.h> #define Size1 (4) #define Size2 (3)
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.