Non-replacement of types



  • class A;
    class B
    {
        public:
            B (A *p):y(p) {}
            ~B() {delete y;} 
            operator A const& () const {return *y;}
            operator A& () {return *y;}
        private:
            A *y;
    };
    class A
    {
        public:
            A(int ax):x(ax) {}
            A* Clone() const 
            {
                return new A(x);
            } 
            B operator + (A const &v2) const
            {
                A *pv=v2.Clone();
                pv->x+=x;
                return B(pv);
            }
            friend ostream& operator << (ostream &os, A const &v)
            {
                os << x;
                return os; 
            }
        protected:
            int x;
    };
    int main()
    {
        A v(4),v1(5);
        cout << v+v1; // <----------
    }
    

    Why doesn't this instruction work and why does it appear? ?

    [Warning] possible problem detected in invocation of delete operator:

    [enabled by default]



  • At the point where the compilation takes place ~B()I can't see. Total Definition A♪ Therefore, the compiler cannot determine exactly what he should do when it comes to delete

    Try to split the project into parts: class definitions separately, implementation separately. So you can compil a destructor. B in .cpp, insert header with complete definition A


    With regard to the second part, I've got a gcc on it if I take it. friend ostream& operator << outside the class: http://ideone.com/SV56y2




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2