casting pointers

Discussion in 'C++' started by Ying Yang, Sep 8, 2003.

  1. Ying Yang

    Ying Yang Guest

    Hi,

    How to I make pointers of different object types point to each other?

    <snippet>

    Node2* link2;
    Node1* link1;

    link1 = link2 //error


    Regards
    wewewewe




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    Ying Yang, Sep 8, 2003
    #1
    1. Advertising

  2. Ying Yang

    Ron Natalie Guest

    "Ying Yang" <> wrote in message news:...
    > Hi,
    >
    > How to I make pointers of different object types point to each other?
    >
    > <snippet>
    >
    > Node2* link2;
    > Node1* link1;
    >
    > link1 = link2 //error


    Certainly is an error. What are you really trying to do? How are Node1 and Node2 related?
     
    Ron Natalie, Sep 8, 2003
    #2
    1. Advertising

  3. Ying Yang wrote:

    > Hi,
    >
    > How to I make pointers of different object types point to each other?


    You don't. The language prevents this. It can be overcome by using
    casts, but this is dangerous and the result may not work at all.

    You are attempting to do things that far exceed your knowledge of the
    language. I suggest you go back and learn the language first.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 8, 2003
    #3
  4. Ying Yang

    Kevin Doyle Guest

    A new C++ type REtard->Kevin Goodsell
    "Kevin Goodsell" <> wrote in message
    news:mc67b.3766$...
    > Ying Yang wrote:
    >
    > > Hi,
    > >
    > > How to I make pointers of different object types point to each other?

    >
    > You don't. The language prevents this. It can be overcome by using
    > casts, but this is dangerous and the result may not work at all.
    >
    > You are attempting to do things that far exceed your knowledge of the
    > language. I suggest you go back and learn the language first.
    >
    > -Kevin
    > --
    > My email address is valid, but changes periodically.
    > To contact me please use the address from a recent posting.
    >
     
    Kevin Doyle, Sep 8, 2003
    #4
  5. Kevin Doyle wrote:

    > A new C++ type REtard->Kevin Goodsell


    Your inability to grasp a simple concept such as NOT TOP-POSTING
    strongly suggests that the mental deficiency is yours, not mine.

    If you can find any error in my post you are more than welcome to point
    it out. If not, then your abusive post would seem to be completely
    unwarranted.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 8, 2003
    #5
  6. Ying Yang

    Howard Guest

    "Ron Natalie" <> wrote in message
    news:3f5ca2ae$0$46507$...
    >
    > "Ying Yang" <> wrote in message

    news:...
    > > Hi,
    > >
    > > How to I make pointers of different object types point to each other?
    > >
    > > <snippet>
    > >
    > > Node2* link2;
    > > Node1* link1;
    > >
    > > link1 = link2 //error

    >
    > Certainly is an error. What are you really trying to do?


    How are Node1 and Node2 related?
    >

    Yeah, that's certainly what needs answering here. My *guess* is the OP is
    using a derived class (Node2) and trying to pass around base-class (Node1)
    pointers. If not, then it's definitely a "bad thing". But, even that *can*
    be accomplished , by using void* pointers. Not a good idea though, unless
    you *really* know what you're doing and why.

    -Howard
     
    Howard, Sep 8, 2003
    #6
  7. Ying Yang

    Default User Guest

    Kevin Doyle wrote:
    >
    > A new C++ type REtard->Kevin Goodsell



    *pa-lonk*



    Brian Rodenborn
     
    Default User, Sep 8, 2003
    #7
  8. Ying Yang

    Ying Yang Guest


    > Kevin Doyle wrote:
    >
    > > A new C++ type REtard->Kevin Goodsell

    >
    > Your inability to grasp a simple concept such as NOT TOP-POSTING
    > strongly suggests that the mental deficiency is yours, not mine.


    I hear you - how strange.

    > If you can find any error in my post you are more than welcome to point
    > it out. If not, then your abusive post would seem to be completely
    > unwarranted.


    wewewe


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    Ying Yang, Sep 9, 2003
    #8
  9. Ying Yang

    Ying Yang Guest


    > Ying Yang wrote:
    >
    > > Hi,
    > >
    > > How to I make pointers of different object types point to each other?

    >
    > You don't. The language prevents this. It can be overcome by using
    > casts, but this is dangerous and the result may not work at all.


    How would I go about it using casts?

    link1 = (*link1)link2; //right?


    > You are attempting to do things that far exceed your knowledge of the
    > language. I suggest you go back and learn the language first.



    wewewe


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    Ying Yang, Sep 9, 2003
    #9
  10. Ying Yang wrote:
    >
    > > Ying Yang wrote:
    > >
    > > > Hi,
    > > >
    > > > How to I make pointers of different object types point to each other?

    > >
    > > You don't. The language prevents this. It can be overcome by using
    > > casts, but this is dangerous and the result may not work at all.

    >
    > How would I go about it using casts?
    >
    > link1 = (*link1)link2; //right?


    You need to be aware what the above effectively does:
    It tells the compiler:

    'Dear Compiler. Shut up!
    I am the programmer and you better do what I tell you to do.
    I don't care that you think that those types don't fit, I don't even
    want to know. I am the programmer and I have the power to do this,
    for the simple reason that I know what I do. So once again:
    Shut up before I shut the computer down!'

    Needless to say that the programmer in 95% of all cases does *not*
    know what he/she is doing.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 9, 2003
    #10
  11. Ying Yang

    Gavin Deane Guest

    "Ying Yang" <> wrote in message news:<>...
    > > Ying Yang wrote:
    > >
    > > > Hi,
    > > >
    > > > How to I make pointers of different object types point to each other?

    > >
    > > You don't. The language prevents this. It can be overcome by using
    > > casts, but this is dangerous and the result may not work at all.

    >
    > How would I go about it using casts?
    >
    > link1 = (*link1)link2; //right?


    Wrong. That doesn't even compile.

    Your compiler is complaining because it really doesn't want to assign
    link2 to link1. It thinks that's a dangerous or incorrect thing to do.
    You could "shut the compiler up" with a correct cast. But the compiler
    is your friend, you should only overrule it if you know exactly what
    you are doing and why. Since you are not even sure of the syntax for
    casting, it seems unlikely that you can carefully judge that casting
    really is appropriate in this case.

    That is why people have asked you for more information about the
    relationship between the Node1 and Node2 types, and an explanation of
    what you are trying to do. They want to help you avoid further
    problems a short way down the line. Nobody wants to show you how to do
    the cast and leave it at that when it seems very likely that casting
    will just cause more problems.

    So, how are the Node1 and Node2 types related (if they are related at
    all), and why are you trying to assign a Node2 to a Node1?

    GJD
     
    Gavin Deane, Sep 9, 2003
    #11
  12. Ying Yang

    Ron Natalie Guest

    "Ying Yang" <> wrote in message news:...

    >
    > How would I go about it using casts?
    >
    > link1 = (*link1)link2; //right?
    >

    Wrong.

    We can't tell you how to "go about it" until you tell us what
    it is that you are trying to do. Why do you want to assign
    these pointers? What are link1 and link2? We can't
    answer this on such little information.
     
    Ron Natalie, Sep 9, 2003
    #12
  13. Ying Yang

    Ying Yang Guest

    > Ying Yang wrote:
    > >
    > > > Ying Yang wrote:
    > > >
    > > > > Hi,
    > > > >
    > > > > How to I make pointers of different object types point to each

    other?
    > > >
    > > > You don't. The language prevents this. It can be overcome by using
    > > > casts, but this is dangerous and the result may not work at all.

    > >
    > > How would I go about it using casts?
    > >
    > > link1 = (*link1)link2; //right?

    >
    > You need to be aware what the above effectively does:
    > It tells the compiler:
    >
    > 'Dear Compiler. Shut up!
    > I am the programmer and you better do what I tell you to do.
    > I don't care that you think that those types don't fit, I don't even
    > want to know. I am the programmer and I have the power to do this,
    > for the simple reason that I know what I do. So once again:
    > Shut up before I shut the computer down!'
    >
    > Needless to say that the programmer in 95% of all cases does *not*
    > know what he/she is doing.



    I'm the other 5%, so does the above cast syntacally correct? or is the wrong
    cast method used?


    wewewewe



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
     
    Ying Yang, Sep 9, 2003
    #13
  14. Ying Yang

    Ron Natalie Guest

    "Ying Yang" <> wrote in message news:...

    >
    > I'm the other 5%, so does the above cast syntacally correct? or is the wrong
    > cast method used?


    What you wrote is never correct. We can't tell you what is correct if you don't
    tell us what it is you want to happen. We and your compiler are not clairvoyant.
    You must specify exactly what you want done.
     
    Ron Natalie, Sep 9, 2003
    #14
  15. Ying Yang wrote:
    >
    > > Ying Yang wrote:
    > > >
    > > > > Ying Yang wrote:
    > > > >
    > > > > > Hi,
    > > > > >
    > > > > > How to I make pointers of different object types point to each

    > other?
    > > > >
    > > > > You don't. The language prevents this. It can be overcome by using
    > > > > casts, but this is dangerous and the result may not work at all.
    > > >
    > > > How would I go about it using casts?
    > > >
    > > > link1 = (*link1)link2; //right?

    > >
    > > You need to be aware what the above effectively does:
    > > It tells the compiler:
    > >
    > > 'Dear Compiler. Shut up!
    > > I am the programmer and you better do what I tell you to do.
    > > I don't care that you think that those types don't fit, I don't even
    > > want to know. I am the programmer and I have the power to do this,
    > > for the simple reason that I know what I do. So once again:
    > > Shut up before I shut the computer down!'
    > >
    > > Needless to say that the programmer in 95% of all cases does *not*
    > > know what he/she is doing.

    >
    > I'm the other 5%,


    Honestly
    What I have seen so far in this newsgroup from you: No, you are not.

    > so does the above cast syntacally correct?


    No.

    > or is the wrong
    > cast method used?


    It is a C-style cast and there is only one syntax for it:

    link1 = (Node1*)link2;

    But as others have said already: Don't do it!

    You behave like somebody wanting to know how to walk on a steel rope
    high above a river. When asked why, it turns out that you want to cross
    that river but don't know about the bridge 100 meter downstream. If
    you tell us what you want to do, some of us will show you the bridge.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 9, 2003
    #15
  16. Ying Yang

    Sumit Rajan Guest

    Karl Heinz Buchegger wrote:


    > You behave like somebody wanting to know how to walk on a steel rope
    > high above a river. When asked why, it turns out that you want to cross
    > that river but don't know about the bridge 100 meter downstream. If
    > you tell us what you want to do, some of us will show you the bridge.
    >



    Nice analogy, Karl!
     
    Sumit Rajan, Sep 9, 2003
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  2. Ernesto

    Casting smart pointers

    Ernesto, Sep 20, 2004, in forum: C++
    Replies:
    8
    Views:
    501
    Gianni Mariani
    Sep 23, 2004
  3. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    577
    James Kanze
    Dec 20, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    695
Loading...

Share This Page