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

  • SEARCH
  • Home
  • 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 6388793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:19:24+00:00 2026-05-25T03:19:24+00:00

I wanted to see if it’s possible to create interfaces, inherit them, and then

  • 0

I wanted to see if it’s possible to create “interfaces”, inherit them, and then check at runtime if any random class implements that interface. This is what I have:

struct GameObject {
    int x,y;
    std::string name;

    virtual void blah() { };
};

struct Airholder {
   int oxygen;
   int nitrogen;
};

struct Turf : public GameObject, public Airholder {
   Turf() : GameObject() {
      name = "Turf";
   }

   void blah() { };
};

void remove_air(GameObject* o) {
   Airholder* a = dynamic_cast<Airholder*>(o);
   if(!a) return;
   a->oxygen   = 0;
   a->nitrogen = 0;
};

Now, it works. The documentation says that it works, the test example works.. But also, it didn’t compile until I added a virtual method to GameObject. The thing is, I really don’t know if the feature is intended to be used like that. What made me wonder there is the fact that I have to declare a virtual function for the class I’m checking. But obviously, there is none, the class I’m checking itself has no virtual functions, in fact my whole code has nothing to do with virtual functions, it’s an entirely different approach.

So, I guess my question is: If what I’m doing really works, why do I need a virtual function to give my class a vtable? Why can’t I declare the class a “runtime type” or something without virtual functions?

  • 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-25T03:19:25+00:00Added an answer on May 25, 2026 at 3:19 am

    [EDIT] According to the comments (people way smarter than me) my answer is completely wrong. However, make your destructors virtual anyway. [/EDIT]

    In C++, I consider upcasting to a base type is only safe if the destructor is virtual. Technically it’s safe, but in reality, you almost always want a virtual destructor. For instance:

    class Base {
       int thingy;
    };
    class Derived : Base{
       int *array;
       Derived() {array = new int[100];}
       ~Derived() {delete [] array;}
    };
    int main() {
        std::auto_ptr<Base> obj(dynamic_cast<Base*>(new Derived));
    }
    

    In this example, when obj goes out of scope, the auto_ptr automatically calls the Base’s destructor, but does not call the Derived deconstructor because the type is a Base, not a Derived. [Edit: corrections] This causes Undefined behaviour (at the very best, it causes a memory leak). I haven’t any idea why C++ doesn’t require a virtual destructor to compile down casts, it really should.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I wanted to see if there are any users currently on my web
I'm messing around with Tamir.SharpSsh and wanted to see if it was possible to
I wanted to see if it is possible to combine two strings and order
I don't think it's possible from what I've read, but wanted to see if
I wanted to see if there was any reason beyond needing it or requiring
I am using facebook registration plugin and wanted to see if its possible to
I just wanted to see if anyone is aware of any other debuggers capable
I wanted to see if anyone had any experience or tips on using timers
Before diving into Wordpress just wanted to see if it is possible to have
This may not be possible but just wanted to see if anyone could point

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.