1

I have interface that looks like this :

class GameObject
{
public:
    virtual ObjType getType() = 0;
    virtual void setType(ObjType ot) = 0;

protected:
    ObjType objtype;


};

then i have class that implement this interface

class SpriteObject : public GameObject
{
    public:
        SpriteObject();
        virtual ~SpriteObject();        
        ObjType getType() { return objtype; }
        void setType(ObjType ot) { objtype = ot; }

    private:

};



class LayerObject : public GameObject
    {
        public:
            LayerObject ();
            virtual ~LayerObject ();        
            ObjType getType() { return objtype; }
            void setType(ObjType ot) { objtype = ot; }

        private:

    };

in the main class i have function which i try to pass the SpriteObject to That looks like this :

bool HelloWorld::NodeAction(GameObject* &pGameObj)
{
}

when i try to do :

SpriteObject *pSpriteObject = new SpriteObject();
NodeAction(pSpriteObject);

im getting this error:

error C2664: 'bool HelloWorld::NodeAction(GameObject *&)' : cannot convert argument 1 from 'SpriteObject *' to 'GameObject *&'

In the end i want to have global function that can get different Objects that implement GameObject .

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 3
    Why do you want `bool HelloWorld::NodeAction(GameObject* &pGameObj)` instead of `bool HelloWorld::NodeAction(GameObject* pGameObj)` – Danh Nov 26 '16 at 06:49
  • i will change the pGameObj in the function i dont need the pointer copy but if it is aproblem i can pass as pointer also , now as i try it it looks like it worked ... why i can't pass pointer reference? – user63898 Nov 26 '16 at 07:10
  • 1
    Why do you hate the pointer copy? Have you read this [How true is “Want Speed? Pass by value”](http://stackoverflow.com/q/21605579/4115625) – Danh Nov 26 '16 at 07:13
  • By the way, are you sure that your `getType` thing is not just reinventing the `dynamic_cast` or `typeid` wheel? Avoiding RTTI is not just about removing those two keywords; it concerns the application's entire class design. – Christian Hackl Nov 26 '16 at 12:59

1 Answers1

2

This function:

bool HelloWorld::NodeAction(GameObject* &pGameObj)

expects a non-cv lvalue reference of GameObject*.

Because pSpriteObject is a lvalue of type SpriteObject *, there're no match function to call for it, it will try to use implicit conversion to find an overload function by those conversion:

  • Lvalue to rvalue conversion

    A glvalue of any non-function, non-array type T can be implicitly converted to a prvalue of the same type. If T is a non-class type, this conversion also removes cv-qualifiers.

  • Pointer conversions

    A prvalue pointer to a (optionally cv-qualified) derived class type can be converted to a prvalue pointer to its (identically cv-qualified) base class.

Hence, this line

NodeAction(pSpriteObject);

expects a function with one of those prototypes:

bool HelloWorld::NodeAction(GameObject*)
bool HelloWorld::NodeAction(GameObject* const&)
bool HelloWorld::NodeAction(GameObject* &&)

All is acceptable but

bool HelloWorld::NodeAction(GameObject*)

should be preferred, see this question. With primitive type, pass by value should be faster than pass-by-reference even if pre-C++11

Community
  • 1
  • 1
Danh
  • 5,916
  • 7
  • 30
  • 45