By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Pointer convension, dot vs. indirection notation

P: n/a
In a recent posting, my notation was corrected:
tree* prootNode = new tree;
tree* psubNode = new tree;

prootNode->child = psubNode;
psubNode->parent = prootNode;


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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> In a recent posting, my notation was corrected:
> tree* prootNode = new tree;
> tree* psubNode = new tree;
>
> prootNode->child = psubNode;
> psubNode->parent = prootNode;
struct tree rootNode;
struct tree subNode;

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


I would event say

tree rootNode;
tree subNode;

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


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
Jul 22 '05 #2

P: n/a
On Sat, 14 Aug 2004 15:43:04 -0700, Casper <ca****@jbr.dk> wrote:
In a recent posting, my notation was corrected:
> tree* prootNode = new tree;
> tree* psubNode = new tree;
>
> prootNode->child = psubNode;
> psubNode->parent = prootNode;


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.


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
Jul 22 '05 #3

P: n/a
John Harrison wrote:
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


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
Jul 22 '05 #4

P: n/a

"Casper" <ca****@jbr.dk> wrote in message
news:5V********************@weber.videotron.net...
In a recent posting, my notation was corrected:
> tree* prootNode = new tree;
> tree* psubNode = new tree;
>
> prootNode->child = psubNode;
> psubNode->parent = prootNode;


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.


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.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.