简体   繁体   中英

Returning other classes variables with function and declaration order

Im trying to do a C++ class function that can return other classes values. The code works if class A is defined first but i have more code that i dont want to mangle around. I figured i need somekind of forward declaration for class A.

What kind of forward declaration do i need to get this work? All my code is in one file. Does this problem dissapear if i properly split my classes to multiple files and include them to project or does it make any difference to VC++ compiler?

Semi pseudo code below.



    // forward declaration
    class A;

    // class deifinitions
    class B {
    private:
        int testvalue;
    public:
        void settestvalue(A &Aobj);
    }

    void B::settestvalue(A &Aobj) {
        testvalue = Aobj.settestvalue();
    }


    class A {
    private:
        int test = 10;
    public:
        int testvalue();
    };

    int A::testvalue() {
        return test;
    }


    // mainloop


    A Aobj;
    B Bobj;

    Bobj.settestvalue (Aobj);

只是将B的成员函数的定义放在A的类定义之后。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM