473,405 Members | 2,354 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,405 software developers and data experts.

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 1517
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.comwrote 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*******@rocketmail.com
Jun 27 '08 #5
"Jim Langston" <ta*******@rocketmail.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*******@rocketmail.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*******@rocketmail.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 (Ihavereasontobelievethat_p_pointstoa_jarray) {
// dynamic_cast required so that the type of
// the _jobject can be verified at run time
q = dynamic_cast<_jarray *>(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 (Ihavereasontobelievethat_p_pointstoa_jarray) {
    * * * * // dynamic_cast required so that the type of
    * * * * // the _jobject can be verified at run time
    * * * * q * * * = dynamic_cast<_jarray *>(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<jobject*(&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**...you just can't do
    it.

    You could use a reinterpret_cast, but you're asking for a lot of trouble
    if you do. By using reinterpret_cast 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
    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
    by: steve | last post by:
    I am writing for game application. Performance is an issue. Any advise would be appreiciated.
    13
    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...
    1
    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...
    19
    by: tthunder | last post by:
    Hi @all, I've got an interesting problem. These are my classes: ---------------------- class fooBase {
    5
    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)....
    22
    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...
    8
    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()...
    18
    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
    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...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    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...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    1
    by: Sonnysonu | last post by:
    This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
    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
    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...
    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.