How do interpreters written in a C ++ class basically

I am working on a project on data structures. Firstly, I wrote everything basically, but it looks like C. But, as I found out, I tried to think about OOP and do as little as possible in my main () methods.

I implemented some operation in my class, for example, add, remove, find.it is too easy to implement.

class ARB
{
        private:
                struct BT
                {
                        int data;
                        BT *l;
                        BT *r;
                };
                struct BT *p;
       public
                ARB();
                ~ARB();
                void del(int n);
                void add(int n); 
};

    void ARB::del(int num)
{
//The code ,don't care about it 

  };  

main()
{
// 
    BTR T;
    T.add(3);
    T.add(5);

};

But I came to a big program How to define a method that should use a binary tree and get a stack

STACK ARB::MyFunct(BT* p)
{
// The code don't care about it 
}

How can I apply it in the main program

main()
{
// 
    BT T;
    T.add(3);
    T.add(5);
    STACK S;
    BT* p
    S=T.MyFunct(p); // error C2664 cannot convert parametre 1

};

** mention: I implement the STACK class

+3
source share
4 answers

. -, add() - ARB, BT. , BT ARB, main(). p ARB ( ), , , ARB. p, p , ARB.

, ARB BT p , add() del() p, MyFunct() BT . , MyFunct() p .

main() :

ARB arb;
arb.add(3)
arb.add(5)
STACK s = arb.myFunct(); // which should maybe be makeStack() or such

, .

0

, < STACK " , :

class STACK {
public:
    ...
    STACK& operator=(STACK& right); // copy assign
    ...
};

, right, STACK, ARB::MyFunct(), . STACK(STACK const& right), ++ , . , R- (, Visual Studio 2010), " ", :

class STACK {
public:
    STACK& operator=(STACK const& right); // copy assign: note 'const'
    STACK& operator=(STACK&& right); // move assign: note no 'const', double '&'.
    ...
};

, .

:

class STACK {
public:
    STACK();
    STACK(STACK const& right); // copy construct
    STACK(STACK&& right); // move construct
    STACK& operator=(STACK const& right); // copy assign
    STACK& operator=(STACK&& right); // move assign
    ...
};

int main() {
    STACK S = T.MyFunct(p); // move construct S (right side is temporary)
    S = T.MyFunct(p); // move assign S (right side is temporary)

    STACK K = S; // copy construct K (right side is not temporary)
    K = S; // copy assign K (right side is not temporary)
}
0

, ? , p. , , BT, .

, :

S=T.MyFunct();

this ? , , .

, BT, BT , ( MyFunct - BT). -, , , - .

. ++ .

0

-, , 5 6 .

/* :
1 BT T;
2 T.add(3);
3 T.add(5);
4 STACK S;
5 BT * p
6 S = T.MyFunct(p);// C2664 1
*/

Line 5: BT * p; → BT * p = & T;

Line 6: S = T. MyFunct (p) → S = ARB :: MyFunct (p);

Now, believing that MyFunct is doing what it should do, it should work. The main problem with the program was that the pointer p was not initialized, actually reducing the parameter to void. In addition, I assume that MyFunct has no object relationship, so in this case it makes no sense to refer to T. That's why I would suggest a later version instead.

0
source

Source: https://habr.com/ru/post/1739239/


All Articles