Pointer convension, dot vs. indirection notation

J

Jonathan Mcdougall

In a recent posting, my notation was corrected:
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
 
J

John Harrison

In a recent posting, my notation was corrected:


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
 
C

Casper

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
 
C

Casper

John said:
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
 
J

jeffc

Casper said:
In a recent posting, my notation was corrected:


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top