Sign in to follow this  
Thirthe

ABC and not found methods in derived classes

Recommended Posts

Hey. I have an ABC 'Object3D' and one derived class 'MobileObject3D' with an extra public method 'setAcceleration' that isn't defined in the ABC. Because of this i'm getting the following error: ".cpp|69|error: 'class Object3D' has no member named 'setAcceleration'|" I thought i was able access the public methods of the derived classes, but it seems i cannot.. why is that? Isn't polymorphism similar to inheritance? Object3D.h
class Object3D
{
	public:
		Object3D( const unsigned int quad_num );
		virtual ~Object3D();

        virtual void BuildRectSolid( const Vertex3 origin, const float width, const float height, const float depth, const unsigned char color_id ) = 0;

        virtual void Build() = 0;
        virtual void Render() = 0;

        Mesh *mesh;

	protected:
        GLint DL;           // DL

        GLfloat *vertices;  // VA
        GLubyte *indices;   // VA
        GLfloat *color;     // VA


};
MobileObject3D.h
class MobileObject3D : public Object3D
{
	public:
		MobileObject3D( const unsigned int quad_num );
		~MobileObject3D();

        void setAcceleration( const GLfloat accel );

		void BuildRectSolid( const Vertex3 origin, const float width, const float height, const float depth, const unsigned char color_id );

        //void Move();

        void Build();
        void Render();

        Vertex3 coords;
        GLfloat w, h, d;
        GLfloat acceleration;

    private:
        void NextFrame();

};
main.cpp
    // test mobile cube
    cube = new MobileObject3D( 0 );

    cube->setAcceleration( 0.2 );
    cube->BuildRectSolid( tmp, 10, 10, 10, 1 );
ty

Share this post


Link to post
Share on other sites
If you are accessing a derived type through a reference or pointer of type Object3D, you only have access to the members that are visible by Object3D interface. To access the MobileObject3D members you would need to downcast your reference to that type, OR provide a virtual setAcceleration stub in Object3D's interface.

Share this post


Link to post
Share on other sites
The whole point of polymorphism is to forget that you actually have an instance of the derived class. You don't care; you're just going to treat it like an instance of the base class, and be pleasantly surprised when it does the right thing.

This is mutually incompatible with the idea of calling functions that are unique to the derived class.

Dealing with this kind of thing properly requires thought. Why does the derived class have this function, and the base class not, exactly? Does the calling code need to know that it actually has a derived object? If so, why not just use a pointer-to-derived? (For that matter, do you actually need to allocate the object dynamically at all?)

polymorphism and inheritance are related concepts, but to ask if one is "similar to" the other is nonsensical. It's like asking if getting to work on time is similar to a car.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this