Question:
How to access "things" in C++ functions?
shadow4light
2011-10-06 13:12:13 UTC
I'm not sure what to call them, but it's better if I give an example.

Say I want to write a function to insert nodes into a Binary Tree or a 2-4 Tree. How do I access the tree, in order to add the node to it?

I have a feeling that it's not like C, where you can just pass the tree in through arguments/parameters.

I saw a function prototype for it, and it was Tree.Insert(int x), where x is the integer to insert.

I'm fairly new to C++, so I don't really know much about this.

Constructive help will be greatly appreciated.
Thank you.
Five answers:
samofcalifornia
2011-10-06 15:01:00 UTC
It depends on where the code is to add the node. If you create a tree class that has the tree in it and make a member function (a function in the class) that adds a node, then the member function magically knows where its data is at. In other words, if the class has a member called nodes, then the add function can reference (use) the member nodes. The class knows where its stuff is at.



I say it is magic, but what actually happens is that member functions have a hidden parameter. All member functions have it except for static member functions. For every class there is a "this" which is a pointer to the instance of the class. Every non-static member function has at least one parameter, even if you see none. The first parameter is always the "this" pointer so the member function can access its members. It really helps to understand about the this pointer. It will help you to understand other things in the future.
cja
2011-10-06 13:26:13 UTC
A tree implemented in C will work just fine in C++, i.e., you could port that tree logic to a C++ program and compile it with your C++ compiler. But in C++ there may be a better way to do it. The prototype you've shown suggests encapsulation of the tree logic into a Tree class, which may have operations to insert, search, display, etc. This could be nice, but not required. If you didn't have a Tree class, a tree insert function might look like this:



    void treeInsert(treeNode *root, int value);



You don't need to "pass the tree through arguments", just pass a pointer to the root. When the tree is encapsulated in a class, you don't need to pass that root pointer, since it would be maintained by your Tree object. The Tree class will likely be a better design, so you should work toward that.
peteams
2011-10-06 15:24:57 UTC
C++ is just C with object-orientation bolted on. You say you'd normally just pass the tree to the function. That is exactly what C++ is doing for you.



When you write:



class Tree {

int data;

void Insert(int x);

};



That's almost the same has writing in C:



struct Tree {

int data;

}

void Insert(Tree* this, int x);



So the C++ code



tree.Insert(x);



Is expanded by the C++ compiler to be:



Insert(&tree, x);



C++ member functions are little more than C functions with an automatic way of specifying and passing the first parameter.
?
2011-10-06 13:22:08 UTC
it's completely based on your making of the "tree".

with C++ we have objects and classes, it's object oriented, there is an object tree (which is known) then

the "Tree.insert(int x)" says for this Tree which is known we have defined a function that inserts a node, in C way u counldnt do this so you would use a pointer to access the tree.
?
2011-10-06 13:17:37 UTC
You can either do it just like in C, or you can make an object called "Tree" and use that. The function prototype you presented uses this.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...