Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Overloaded Operators

Last post 08-21-2008, 6:33 AM by Toji. 2 replies.
Sort Posts: Previous Next
  •  08-20-2008, 9:44 PM 2707

    Overloaded Operators

    I'm playing around with Squirrel to determine it's feasibility for an upcoming project. What I'm hung up on now is trying to expose a class with two "-" operators (one unary, one binary). D3DXVECTOR3 is a good example of the type of class I'm looking at:

    // Shortened for easy reading
    struct D3DXVECTOR3
    {
    public:
        // unary operators
        D3DXVECTOR3 operator + () const;
        D3DXVECTOR3 operator - () const;

        // binary operators
        D3DXVECTOR3 operator + ( CONST D3DXVECTOR3& ) const;
        D3DXVECTOR3 operator - ( CONST D3DXVECTOR3& ) const;
        D3DXVECTOR3 operator * ( FLOAT ) const;
        D3DXVECTOR3 operator / ( FLOAT ) const;

        float x;
        float y;
        float z;
    }


    I only care about exposing the binary operator, and am attempting to do it like so:

    SQClassDef<D3DXVECTOR3>(_T("D3DXVECTOR3")).
                    func(&D3DXVECTOR3::operator +, _T("_add")).
                    func(&D3DXVECTOR3::operator -, _T("_sub")).
                    func(&D3DXVECTOR3::operator *, _T("_mul")).
                    func(&D3DXVECTOR3::operator /, _T("_div")).
                    var(&D3DXVECTOR3::x, _T("x")).
                    var(&D3DXVECTOR3::y, _T("y")).
                    var(&D3DXVECTOR3::z, _T("z"));


    this is giving me an error, however:

    cannot deduce template argument as function argument is ambiguous


    How can I indicate to SqPlus which operator I intend to use?
  •  08-21-2008, 4:41 AM 2708 in reply to 2707

    Re: Overloaded Operators

    Hello,

    This is default C++ behaviour. You have to do something like:

      D3DXVECTOR3 (D3DXVECTOR3::*fopadd) operator + ( CONST D3DXVECTOR3& ) const = &D3DXVECTOR3::operator +;

    And then:

      SQClassDef<D3DXVECTOR3>(_T("D3DXVECTOR3")).
                    func(fopadd, _T("_add")).

    For overloaded functions, no other way.

    Regards
    // ATS.
  •  08-21-2008, 6:33 AM 2709 in reply to 2708

    Re: Overloaded Operators

    Wow, I still learn something new about C++ every day. Just haven't worked with function pointers that much I guess.

    Thanks for the tip, I got it working great. For the sake of anyone else who's trying this in the future, though, you did make one minor syntax mistake:

    instead of this:
    D3DXVECTOR3 (D3DXVECTOR3::*fopadd) operator + ( CONST D3DXVECTOR3& ) const = &D3DXVECTOR3::operator +;

    it should be this:
    D3DXVECTOR3 (D3DXVECTOR3::*fopadd)( CONST D3DXVECTOR3& ) const = &D3DXVECTOR3::operator +;

    A small difference, but we all know how much compilers like to whine about small differences. :) Also, for anyone looking to use the D3DX Vector in the future, here's my setup code:

    //Somewhere outside of main()
    DECLARE_INSTANCE_TYPE(D3DXVECTOR3)

    //Somewhere inside of main()
    D3DXVECTOR3 (D3DXVECTOR3::*fopadd)( CONST D3DXVECTOR3& ) const = &D3DXVECTOR3::operator +;
    D3DXVECTOR3 (D3DXVECTOR3::*fopsub)( CONST D3DXVECTOR3& ) const = &D3DXVECTOR3::operator -;
    D3DXVECTOR3 (D3DXVECTOR3::*fopunm)( ) const = &D3DXVECTOR3::operator -;

    SQClassDef<D3DXVECTOR3>(_T("D3DXVECTOR3")).
            func(fopadd, _T("_add")).
            func(fopsub, _T("_sub")).
            func(fopunm, _T("_unm")).
            func(&D3DXVECTOR3::operator *, _T("_mul")).
            func(&D3DXVECTOR3::operator /, _T("_div")).
            func(&D3DXVECTOR3::operator ==, _T("_cmp")).
           
            var(&D3DXVECTOR3::x, _T("x")).
            var(&D3DXVECTOR3::y, _T("y")).
            var(&D3DXVECTOR3::z, _T("z"));


    This exposes the following operators: +, -, -(unary), *, /, +=, -=, *=, /=, ==, !=

    Thanks again, and I hope this post helps someone else out in the future!
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems