Connecting Tech Pros Worldwide Forums | Help | Site Map

Pointer convension, dot vs. indirection notation

Casper
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Guest
 
Posts: n/a
#2: Jul 22 '05

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


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Pointer convension, dot vs. indirection notation


On Sat, 14 Aug 2004 15:43:04 -0700, Casper <casper@jbr.dk> 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
Casper
Guest
 
Posts: n/a
#4: Jul 22 '05

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
jeffc
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Pointer convension, dot vs. indirection notation



"Casper" <casper@jbr.dk> wrote in message
news:5VtTc.36627$Kx.1617940@weber.videotron.net...[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.


Closed Thread