Domanda

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

È stato utile?

Soluzione

just put the defination of B's member-function after A's class definition.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top