473,569 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How would I do this dynamic_cast?

Rob
Expand|Select|Wrap|Line Numbers
  1. (I am not the one who defined these classes)
  2.  
  3. class _jobject {};
  4. class _jarray : public _jobject {};
  5.  
  6. typedef _jobject* jobject;
  7. typedef _jarray jarray;
  8.  
  9. int main()
  10. {
  11. jobject * a;
  12. jarray b;
  13. a = dynamic_cast<jobject*(&b);
  14. .....
  15. }
  16.  
This doesn't work and the problem is the only type names I'm
guaranteed to have are jobject and jarray. The original _jobject is
how it's defined here but I'm not guaranteed it''ll always be that way
so I have to work with jobject/jarray.

How would I cast between jobject and jarray?
Jun 27 '08 #1
9 1525
Rob
Sorry, I missed a "*" in there.

Expand|Select|Wrap|Line Numbers
  1. (I am not the one who defined these classes)
  2.  
  3. class _jobject {};
  4. class _jarray : public _jobject {};
  5.  
  6. typedef _jobject* jobject;
  7. typedef _jarray* jarray;
  8.  
  9. int main()
  10. {
  11. jobject * a;
  12. jarray b;
  13. a = dynamic_cast<jobject*(&b);
  14. .....
  15. }
  16.  
  17.  
Jun 27 '08 #2
Rob
But it still doesn't work... :)
Jun 27 '08 #3
"Rob" <so************ ***@yahoo.comwr ote in message
Expand|Select|Wrap|Line Numbers
  1. (I am not the one who defined these classes)
  2. class _jobject {};
  3. class _jarray : public _jobject {};
  4. typedef _jobject* jobject;
  5. typedef _jarray jarray;
  6. int main()
  7. {
  8.     jobject * a;
  9.     jarray b;
  10.     a = dynamic_cast<jobject*(&b);
  11. ....
  12. }
  13.  

This doesn't work and the problem is the only type names I'm
Does this even compile?
guaranteed to have are jobject and jarray. The original _jobject is
how it's defined here but I'm not guaranteed it''ll always be that way
so I have to work with jobject/jarray.

How would I cast between jobject and jarray?
I think you are not understanding what dynamic_cast does. You can always
assign a derived class pointer to a base class pointer with public
inheritance. The inverse may or may not hold true. This is where a run time
cast named dynamic_cast comes into picture. If the downcasting is
successful, then the address returned by dynamic_cast is non NULL.

Why do you require dynamic_cast? Can't virtual functions help you?

--
http://techytalk.googlepages.com
Jun 27 '08 #4
Rob wrote:
Expand|Select|Wrap|Line Numbers
  1. (I am not the one who defined these classes)
  2. class _jobject {};
  3. class _jarray : public _jobject {};
  4. typedef _jobject* jobject;
  5. typedef _jarray jarray;
  6. int main()
  7. {
  8.     jobject * a;
  9.     jarray b;
  10.     a = dynamic_cast<jobject*(&b);
  11. ....
  12. }
  13.  

This doesn't work and the problem is the only type names I'm
guaranteed to have are jobject and jarray. The original _jobject is
how it's defined here but I'm not guaranteed it''ll always be that way
so I have to work with jobject/jarray.

How would I cast between jobject and jarray?
jobject* a becomes _jobject** because jobject is _jobject*
jarray b becomes _jarray.

Now you're trying to cast &b which is address of _jarray which is _jarray*
to a jobject* which is _jobject***. You see the problem?
--
Jim Langston
ta*******@rocke tmail.com
Jun 27 '08 #5
"Jim Langston" <ta*******@rock etmail.comwrote in message
Rob wrote:
[snip]
>This doesn't work and the problem is the only type names I'm
guaranteed to have are jobject and jarray. The original _jobject is
how it's defined here but I'm not guaranteed it''ll always be that way
so I have to work with jobject/jarray.

How would I cast between jobject and jarray?

jobject* a becomes _jobject** because jobject is _jobject*
jarray b becomes _jarray.

Now you're trying to cast &b which is address of _jarray which is _jarray*
to a jobject* which is _jobject***. You see the problem?
Even if he corrects that the intention to use dynamic_cast is not justified.

--
http://techytalk.googlepages.com
Jun 27 '08 #6
sk_usenet wrote:
"Jim Langston" <ta*******@rock etmail.comwrote in message
>Rob wrote:
[snip]
>>This doesn't work and the problem is the only type names I'm
guaranteed to have are jobject and jarray. The original _jobject is
how it's defined here but I'm not guaranteed it''ll always be that
way so I have to work with jobject/jarray.

How would I cast between jobject and jarray?

jobject* a becomes _jobject** because jobject is _jobject*
jarray b becomes _jarray.

Now you're trying to cast &b which is address of _jarray which is
_jarray* to a jobject* which is _jobject***. You see the problem?

Even if he corrects that the intention to use dynamic_cast is not
justified.
I agree it's not justified. I also think though that he doesn't understand
a few things about this code.

Consider that this compiles:

class _jobject {};
class _jarray : public _jobject {};

typedef _jobject* jobject;
typedef _jarray jarray;

int main()
{
_jobject* a;
_jarray b;
a = &b;
}

My feeling is that he had the types so hidden by typedefs and tried to do
something like this, got errors and tried to use dynamic cast to fix it.
Fixing the too many indirections allows an assignment without any type of
cast necessary.
--
Jim Langston
ta*******@rocke tmail.com
Jun 27 '08 #7
Rob wrote:
Expand|Select|Wrap|Line Numbers
  1. (I am not the one who defined these classes)
  2. class _jobject {};
  3. class _jarray : public _jobject {};
  4. typedef _jobject* jobject;
  5. typedef _jarray jarray;
  6. int main()
  7. {
  8.      jobject * a;
  9.      jarray b;
  10.      a = dynamic_cast<jobject*(&b);
  11. ....
  12. }
  13.  

This doesn't work and the problem is the only type names I'm
guaranteed to have are jobject and jarray. The original _jobject is
how it's defined here but I'm not guaranteed it''ll always be that way
so I have to work with jobject/jarray.

How would I cast between jobject and jarray?
According to the declarations you have given jarray is a synonym for
_jarray which is derived from _jobject, but jobject is synonym for a
POINTER to a _jobject. &b returns a pointer to the instance of _jarray,
which can be converted without a cast, because it is just working up the
class hierarchy, to a pointer to _jobject, which is synonymous with
jobject, not with pointer to jobject. I do not know your objective, but
the following should compile:

int main()
{
jobject a;
jarray b;
a = &b;
}

dynamic_cast is used for moving DOWN the class hierarchy, not up. So if
you have a pointer to _jobject that you have reason to believe is
actually a pointer to _jarray you can:

_jobject * p;
_jarray * q;
// no cast required because an instance of _jarray
// is guaranteed to be an instance of _jobject
p = q;
if (Ihavereasontob elievethat_p_po intstoa_jarray) {
// dynamic_cast required so that the type of
// the _jobject can be verified at run time
q = dynamic_cast<_j array *>(p);
}

As I see it the problem you are facing is that the name of the typedef
jobject is confusing because it does not warn the programmer that it is
a pointer to a class, not a class itself. If the architect had really
intended to use the jobject and jarray definitions to hide the internal
implementation then they should have been defined explicitly as classes,
not as typedefs.

In any event C++ pointers are extremely dangerous and should be avoided
as much as possible. They are a carryover artifact from C. Their use
exposes you to risks because the compiler cannot catch most misuses.
For example the compiler will permit you to use an instance of jobject
as an array! That is because C permits you to use any pointer as an
array. In your fragment of code what would happen if you coded:

a[3] = b; // !?

Jun 27 '08 #8
Rob
On Apr 13, 3:49*pm, Jim Cobban <jcob...@magma. cawrote:
Rob wrote:
Expand|Select|Wrap|Line Numbers
  1.  (I am not the one who defined these classes)
Expand|Select|Wrap|Line Numbers
  1.         
  2.                  class _jobject {};
  3.  class _jarray : public _jobject {};
  •  
  •         
  •                  typedef _jobject* jobject;
  •  typedef _jarray jarray;
  •  
  •         
  •                  int main()
  •  {
  •  * * *jobject * a;
  •  * * *jarray b;
  •  * * *a = dynamic_cast<jobject*(&b);
  •  ....
  •  }
  •  
  •  
  • This doesn't work and the problem is the only type names I'm
    guaranteed to have are jobject and jarray. The original _jobject is
    how it's defined here but I'm not guaranteed it''ll always be that way
    so I have to work with jobject/jarray.
    How would I cast between jobject and jarray?

    According to the declarations you have given jarray is a synonym for
    _jarray which is derived from _jobject, but jobject is synonym for a
    POINTER to a _jobject. *&b returns a pointer to the instance of _jarray,
    which can be converted without a cast, because it is just working up the
    class hierarchy, to a pointer to _jobject, which is synonymous with
    jobject, not with pointer to jobject. *I do not know your objective, but
    the following should compile:

    int main()
    {
    * * * jobject a;
    * * * jarray b;
    * * * a = &b;

    }

    dynamic_cast is used for moving DOWN the class hierarchy, not up. *So if
    you have a pointer to _jobject that you have reason to believe is
    actually a pointer to _jarray you can:

    * * * * _jobject * * * *p;
    * * * * _jarray * * * * q;
    * * * * // no cast required because an instance of _jarray
    * * * * // is guaranteed to be an instance of _jobject
    * * * * p * * * = q;
    * * * * if (Ihavereasontob elievethat_p_po intstoa_jarray) {
    * * * * // dynamic_cast required so that the type of
    * * * * // the _jobject can be verified at run time
    * * * * q * * * = dynamic_cast<_j array *>(p);
    * * * * }

    As I see it the problem you are facing is that the name of the typedef
    jobject is confusing because it does not warn the programmer that it is
    a pointer to a class, not a class itself. *If the architect had really
    intended to use the jobject and jarray definitions to hide the internal
    implementation then they should have been defined explicitly as classes,
    not as typedefs.

    In any event C++ pointers are extremely dangerous and should be avoided
    as much as possible. *They are a carryover artifact from C. *Their use
    exposes you to risks because the compiler cannot catch most misuses.
    For example the compiler will permit you to use an instance of jobject
    as an array! *That is because C permits you to use any pointer as an
    array. *In your fragment of code what would happen if you coded:

    * * * * a[3] * *= b; * *// !? Thanks!
    Jun 27 '08 #9
    Rob wrote:
    Sorry, I missed a "*" in there.

    [code]
    (I am not the one who defined these classes)

    class _jobject {};
    class _jarray : public _jobject {};

    typedef _jobject* jobject;
    typedef _jarray* jarray;

    int main()
    {
    jobject * a;
    jarray b;
    a = dynamic_cast<jo bject*(&b);
    ....
    }
    The reason this doesn't work is that such casts are simply not possible.
    Once you start using multiple levels of dereference the type system
    fails. You can't assign a _jarray** to a _jobject**...yo u just can't do
    it.

    You could use a reinterpret_cas t, but you're asking for a lot of trouble
    if you do. By using reinterpret_cas t you're saying to the compiler, "I
    know what I'm doing is insane and will inevitably cause me endless
    suffering, but I'm going to do it anyway so just shut the hell up and do
    the cast!" All type information is then lost.

    Whatever decisions guided you to attempt this are flawed. Reevaluate
    your situation.

    BTW, the names of your classes violate the standard. Any name starting
    with '_' in the global namespace is reserved for the implementation to use.
    Jun 27 '08 #10

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

    Similar topics

    2
    1860
    by: exits funnel | last post by:
    Hello, I have the following simple code //BEGIN CODE #include <iostream> class foo { public: virtual void fun( ) { }}; class bar : public foo {public:void fun( ) { }}; int main( )
    17
    3341
    by: steve | last post by:
    I am writing for game application. Performance is an issue. Any advise would be appreiciated.
    13
    4946
    by: GianGuz | last post by:
    Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy. The question I would to rise is when dynamic_cast is really necessary? A wise usage of templates and/or non dynamic_cast operators can grant the compiler with any information it needs to...
    1
    1516
    by: Steven T. Hatton | last post by:
    The result shown below doesn't surprise me now. But it did several months ago when I followed some bad advice and tried to check if I had a live object at the address referenced by a pointer. Can I assume the result is consistente with the Standard? Tue May 10 21:24:24:> cat foo.cc #include <iostream> struct Foo{virtual ~Foo(){} };...
    19
    4044
    by: tthunder | last post by:
    Hi @all, I've got an interesting problem. These are my classes: ---------------------- class fooBase {
    5
    2028
    by: tthunder | last post by:
    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...
    22
    4789
    by: Boris | last post by:
    I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look now at this code: Base *b = new Level2(); Level1 *l1 = dynamic_cast<Level1*>(b); Should dynamic_cast return a valid pointer or 0? I wonder as...
    8
    5424
    by: pietromas | last post by:
    In the example below, why does the dynamic_cast fail (return NULL)? It should be able to cast between sibling classes ... #include <iostream> class A { public: virtual const int get() const = 0;
    18
    3097
    by: mark | last post by:
    class SORef {...}; class SelectedFORef : public SORef {...}; SOFORef SOCastToSOF(SORef sor) { SelectedFORef sof=nil; sof=dynamic_cast<SelectedFORef>(sor); return sof; };
    25
    3113
    by: lovecreatesbea... | last post by:
    Suppose I have the following three classes, GrandBase <-- Base <-- Child <-- GrandChild The following cast expression holds true only if pBase points object of type of ``Child'' or ``GrandChild'', i.e. types not upper than Child in the above class hierarchy, dynamic_cast<Child*>pBase
    0
    7700
    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...
    0
    7924
    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. ...
    0
    8125
    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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
    1
    7676
    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...
    0
    7974
    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...
    0
    5219
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
    1
    2114
    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
    1
    1221
    muto222
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.
    0
    938
    bsmnconsultancy
    by: bsmnconsultancy | last post by:
    In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

    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.