Answer the question
In order to leave comments, you need to log in
What is the best way to organize a class and its objects?
I used to write in C.
Now I write in C ++, I know the materiel, but the question arose about the organization of the class structure.
Let's say there is data that will eventually be used as a binary tree (or list, which is not so important) for further adding / changing / processing this data.
Now the point is:
what is the most natural way to organize a class in this case?
1) each node of the tree is an object of the desired class
2) there is only one class object and it contains a pointer to the top of the tree
Answer the question
In order to leave comments, you need to log in
Don't try to reinvent the wheel, everything has already been invented.
If you really want "your own", I advise you to look at existing implementations, find strengths and try to eliminate shortcomings. For example Binary Tree , Linked List
If you only have a pointer to the top of the tree, then how will you continue to walk along the branches of this tree - without pointers to them?
In my opinion, for a tree node, you need something like this:
template<class T>
struct Node
{
Node* left, *right;
T data;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question