N
N
nadom2014-11-02 00:10:33
Programming
nadom, 2014-11-02 00:10:33

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

3 answer(s)
V
Vasily, 2014-11-02
@Applez

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

B
bogolt, 2014-11-02
@bogolt

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;
};

X
XF0, 2014-11-02
@XF0

Everything has already been invented before us - Visitor Pattern

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question