Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:18:07+00:00 2026-05-25T15:18:07+00:00

I have a class: class Base; Also I have an interface class Interface; Next

  • 0

I have a class:

class Base;

Also I have an interface

class Interface;

Next i’m creating a class

class Derived : public Base, public Interface;

If I have Base *object = new Derived;

How can i cast object to Interface ? (of course if i know than object is actually a derived class)

EDIT:

I’ve tried dynamic_cast and static_cast (not compiled).
So let me explain the problem a bit more:

I have:

class Object {...}

class ITouchResponder
{
public:
    virtual bool onTouchBegan(XTouch *touch) = 0;
    virtual void onTouchMoved(XTouch *touch) = 0;
    virtual void onTouchEnded(XTouch *touch) = 0;
};

class Ball : public Object, public ITouchResponder {...};

class TentacleSensor : public Object, public ITouchResponder {...}

Object have a bool touchable_ property. If it’s true then object is implementing ITouchResponder interface.

When I use it:

bool Level::onTouchBegan(XTouch *touch)
{
    ...
    ITouchResponder *responder = callback.nearestTouchable();
    if (responder)
    {
        if (responder->onTouchBegan(touch))
        {
            if (responder != ball_)
            {
                touch->setUserData(responder);
            }
        }
    }

    return true;
}

ITouchResponder *QueryCallback::nearestTouchable() const
{
    for (list<Object*>::const_iterator it = objects_.begin(); it != objects_.end(); ++it)
    {
        if ( (*it)->isTouchable() ) return (*it)->asTouchResponder();
    }
    return 0;
}

asTouchResponder is a method of Object :

    ITouchResponder * Object::asTouchResponder()
    {
        assert(touchable_);
        ITouchResponder *res = dynamic_cast<ITouchResponder*>(this);
        assert(res);
        return res;
    }

I have bad excess error in xcode.

But if i make Object : public ITouchResponder everything works fine. What am i doing wrong ?

Full object class:

class Object// : public ITouchResponder
{
public:
    struct Def
    {
        Def()
        {
            level = 0;
            world = 0;
            touchable = false;
            acceptsContacts = false;
            body = 0;
            node = 0;
        }
        Level *level;
        b2World *world;
        bool touchable;
        bool acceptsContacts;
        b2Body *body;
        XNode *node;
    };

    Object(const Def &def);
    virtual ~Object();

    virtual void update(float dt);
    bool isTouchable() const {return touchable_;}

    void addDependantObject(Object *object);
    void removeDependantObject(Object *object);

    virtual void objectWillBeRemoved(Object *object) {} //this function is automatically called to every dependant object when object is removed

    virtual XVec2 position() const;
    virtual float rotation() const;

    bool acceptsContacts() const {return acceptsContacts_;}

    b2Body *body() const {return body_;}

    Level *level() const {return level_;}
    b2World *world() const {return world_;}

    ITouchResponder *asTouchResponder();
    /*
    virtual bool onTouchBegan(XTouch *touch) {
        return false;
    }
    virtual void onTouchMoved(XTouch *touch)
    {
    }
    virtual void onTouchEnded(XTouch *touch) 
    {
    }*/

protected:
    Level *level_;
    b2World *world_;
    bool touchable_;
    bool acceptsContacts_;

    XNode *node_;
    b2Body *body_;

    list<Object*> dependantObjects_;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:18:07+00:00Added an answer on May 25, 2026 at 3:18 pm

    If Base has virtual function (even be it virtual destructor), then:

    Derived *pDerived = dynamic_cast<Derived *>(object);
    

    Else, use

    Derived *pDerived = static_cast<Derived *>(object);
    

    Note that if Base doesn’t have virtual function, then dynamic_cast will NOT compile. In dynamic_cast, only the source has to be a polymorphic object, in order to compile, and if the destination isn’t polymorphic, then dynamic_cast will return null pointer:

    Suppose A and B are polymorphic type, and C is non-polymorphic, then

    A *pA = dynamic_cast<A*>(new C()); //error - source is not polymorphic!
    
    A *pA = dynamic_cast<A*>(new B()); //ok
    if ( pA == 0 )
          cout << "pA will be null if B is not derived from A" << endl;
    
    C *pC = dynamic_cast<C*>(new B()); //ok
    if ( pC == 0 )
           cout << "pC must be null" << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class, defined as below (I'm also using DevExpress components): public
I have a base class called Component. I also have 2 interfaces I2DComponent, and
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
In C++, is it possible to have a base plus derived class implement a
I have the following base abstract class defined as: public abstract class BaseObject<T> :
Let's say I have a base class that implements the INotifyPropertyChanged interface and I
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
I have my class X which inherits from Qt's class Base . I declared
I have a base class with an optional virtual function class Base { virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.