How to declare an operator that return another class object?

This is a discussion on How to declare an operator that return another class object? within the C++ Programming forums, part of the General Programming Boards category; For example (not tested): class a { private: int x public: void set_a(int y) {x=y;} }; class b { private: ...

  1. #1
    Niy
    Guest

    How to declare an operator that return another class object?

    For example (not tested):

    class a {
    private:
    int x
    public:
    void set_a(int y) {x=y;}
    };

    class b {
    private:
    int var;
    public:
    a operator-(const b &oper2) {
    a result;
    result.set_a(var - oper2.var);
    return(result);
    }

    Obviously this won't work since the class b didn't know anything about class
    a. And thus it cannot return an object with type a. How to make it possible?
    Thanks.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    class b can know about a. The following is a legal program.

    Code:
    class a {
    private:
    	int x;
    public:
    	void set_a(int y) {x=y;}
    };
    
    class b {
    private:
    	int var;
    public:
    	a operator-(const b &oper2) {
    		a result;
    		result.set_a(var - oper2.var);
    		return(result);
    	}
    };
    
    int main() {
    	b b1;
    	b b2;
    	
    	a a1 = b2 - b1;
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Niy
    Guest

    Unhappy

    You are right, what you have written is compilable.
    However, I have written a similar programme(not quotable since it is too long). But when compiling the following error message occurs:

    syntax error before `operator'
    parse error at null character
    In function `int main()':
    no match for `b & - b &'

    Dunno why

  4. #4
    Yin
    Guest

    Lightbulb

    Oh, sxxt!

    I now know why.

    If you try the following, you will know what I mean:

    code:
    --------------------------------------------------------------------------------
    class b {
    private:
    int var;
    public:
    a operator-(const b &oper2) {
    a result;
    result.set_a(var - oper2.var);
    return(result);
    }
    };

    class a {
    private:
    int x;
    public:
    void set_a(int y) {x=y;}
    };


    int main() {
    b b1;
    b b2;

    a a1 = b2 - b1;
    return 0;
    }
    --------------------------------------------------------------------------------

    But another problem arises. What if I want use class a in class b, as well as class b in class a? Which class should I put first?

  5. #5
    gull
    Guest
    I'm a bit new to this OO stuff, but;

    why not declare a friend operator function after declaring class a & class b and overload it?

    such as;
    class a{
    ~
    };

    class b{
    ~
    public:
    a friend operator-(&a,&b);
    b friend operator-(&b,&a);
    }

    a operator-(&a,&b){
    //manupulate a
    //manipulate b

    return a;
    }

    b operator-(&a,&b){
    //manipulate a or b

    return b;
    }

    that way you can return either object dependant on the order of the operands.

    If you want to return either a or b dependant upon some decision taken within the operator function, then i'm not sure what you are trying to do, but i think you might be able do that by making a and b derived classes from a common base class, then you could specify the return type to be the base class?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 02:08 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21