Pointer convension, dot vs. indirection notation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Casper

    Pointer convension, dot vs. indirection notation

    In a recent posting, my notation was corrected:
    [color=blue]
    > tree* prootNode = new tree;
    > tree* psubNode = new tree;
    >
    > prootNode->child = psubNode;
    > psubNode->parent = prootNode;[/color]

    struct tree rootNode;
    struct tree subNode;

    rootNode.child = &subNode;
    subNode.parent = &rootNode;

    Is this due to some general C++ convension/practice or because its
    compiler specific to use indirection? I've seen many compilers
    understanding -> however.

    Thanks,
    Casper
  • Jonathan Mcdougall

    #2
    Re: Pointer convension, dot vs. indirection notation

    > In a recent posting, my notation was corrected:[color=blue]
    >[color=green]
    > > tree* prootNode = new tree;
    > > tree* psubNode = new tree;
    > >
    > > prootNode->child = psubNode;
    > > psubNode->parent = prootNode;[/color]
    >
    > struct tree rootNode;
    > struct tree subNode;
    >
    > rootNode.child = &subNode;
    > subNode.parent = &rootNode;[/color]

    I would event say

    tree rootNode;
    tree subNode;

    The struct qualifier is redundant.
    [color=blue]
    > Is this due to some general C++ convension/practice or because its
    > compiler specific to use indirection? I've seen many compilers
    > understanding -> however.[/color]

    There is a difference between these two : prootNode and psubNode are
    pointers to heap-allocated objects and rootNode and subNode are objects
    allocated on the stack. If you don't know the difference between them, you
    should by a better book. The general practice is : if you don't need to
    allocate objects on the heap, put them on the stack. It is faster, more
    efficient (and all the good words like that) and easier to work with. No
    news is good news.


    Jonathan


    Comment

    • John Harrison

      #3
      Re: Pointer convension, dot vs. indirection notation

      On Sat, 14 Aug 2004 15:43:04 -0700, Casper <casper@jbr.d k> wrote:
      [color=blue]
      > In a recent posting, my notation was corrected:
      >[color=green]
      > > tree* prootNode = new tree;
      > > tree* psubNode = new tree;
      > >
      > > prootNode->child = psubNode;
      > > psubNode->parent = prootNode;[/color]
      >
      > struct tree rootNode;
      > struct tree subNode;
      >
      > rootNode.child = &subNode;
      > subNode.parent = &rootNode;
      >
      > Is this due to some general C++ convension/practice or because its
      > compiler specific to use indirection? I've seen many compilers
      > understanding -> however.
      >[/color]

      I think you misunderstand. I didn't see the original post, but your code
      and the correction are just different code. Your code uses pointers to
      structs (hence ->) and the correction uses structs (hence .). These are
      just different, it's not a case of one being correct notation and the
      other not. I can't say which is right for you, but at a guess your
      original code looks more likely, maybe you misunderstood the point that
      was being made.

      In any case you better learn the difference between something which is a
      pointer and something which is not, and when you would use one and when
      the other, if you are going to be programming dynamically allocated trees.

      john

      Comment

      • Casper

        #4
        Re: Pointer convension, dot vs. indirection notation

        John Harrison wrote:[color=blue]
        > I think you misunderstand. I didn't see the original post, but your
        > code and the correction are just different code. Your code uses
        > pointers to structs (hence ->) and the correction uses structs (hence
        > .). These are just different, it's not a case of one being correct
        > notation and the other not. I can't say which is right for you, but at
        > a guess your original code looks more likely, maybe you misunderstood
        > the point that was being made.
        >
        > In any case you better learn the difference between something which is
        > a pointer and something which is not, and when you would use one and
        > when the other, if you are going to be programming dynamically
        > allocated trees.
        >
        > john[/color]

        My original post was about the lifespan of structs as tree nodes. It
        would appear I did not understand the context of the point that was
        being made, which was in regard to a root node only. It did not make
        much sence to me as you would have a hard time building a dynamic tree
        using structs on the stack only.

        Because if I am allocating (sub)nodes dynamically and adding to my tree,
        I might as well have the root be on the heap/free store as well. Thanks
        for clarifying!

        Casper

        Comment

        • jeffc

          #5
          Re: Pointer convension, dot vs. indirection notation


          "Casper" <casper@jbr.d k> wrote in message
          news:5VtTc.3662 7$Kx.1617940@we ber.videotron.n et...[color=blue]
          > In a recent posting, my notation was corrected:
          >[color=green]
          > > tree* prootNode = new tree;
          > > tree* psubNode = new tree;
          > >
          > > prootNode->child = psubNode;
          > > psubNode->parent = prootNode;[/color]
          >
          > struct tree rootNode;
          > struct tree subNode;
          >
          > rootNode.child = &subNode;
          > subNode.parent = &rootNode;
          >
          > Is this due to some general C++ convension/practice or because its
          > compiler specific to use indirection? I've seen many compilers
          > understanding -> however.[/color]

          There is nothing wrong with ->. I don't know the context of your original
          code, but it might have been "corrected" because the general rule is don't
          use dynamic memory allocation unless you really need to.


          Comment

          Working...