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 7924627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:50:42+00:00 2026-06-03T17:50:42+00:00

I have a class that derives from a C struct. The class does not

  • 0

I have a class that derives from a C struct. The class does not do anything special, other than initialization in the constructor, deinitialization function during the destructor, and a few other methods that call into C functions. Basically, it’s a run-of-the-mill wrapper. Using GCC, it complained that my destructor was not virtual, so I made it that. Now I run into segfaults.

/* C header file */
struct A
{
  /* ... */
}

// My C++ code
class B : public A
{
public:
  B() { /* ... init ... */ }
  virtual ~B() { /* ... deinit ... */ }

  void do()
  {
    someCFunction(static_cast<A *>(this));
  }
};

I was always under the assumption that the static_cast would return the correct pointer to the base class, pruning off the virtual table pointer. So this may not be the case, since I get a segfault in the C function.

By removing the virtual keyword, the code works fine, except that I get a gcc warning. What is the best work around for this? Feel free to enlighten me :).

  • 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-06-03T17:50:50+00:00Added an answer on June 3, 2026 at 5:50 pm

    Both the explicit and implicit conversion to A* are safe. There is neither need for an explicit cast, nor is it going to introduce vtables anywhere, or anything like that. The language would be fundamentally unusable if this were not the case.

    I was always under the assumption that the static_cast would return
    the correct pointer to the base class, pruning off the virtual table
    pointer.

    Is absolutely correct.

    The destructor need be virtual only if delete ptr; is called where ptr has type A*– or the destructor invoked manually. And it would be A‘s destructor that would have to be virtual, which it isn’t.

    Whatever the problem is in your code, it has nothing to do with the code shown. You need to expand your sample considerably.

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

Sidebar

Related Questions

I have a class that derives from ImageView . In its onDraw method override
i have a class 'MyTextBox' that derives from the default TextBox in Silverlight. This
Say I have a class named Base and a class that derives from it
I have a class that derives from enable_shared_from_this ... (Recently been added to std
I have created a class that derives from TThread, because I wish to do
I have a class that derives from ActiveRecord::Base. This class has a subclass that
I have a class that derives from XNA's GraphicsDevice, and i want to use
I have created my own class that derives from QTableView, the associated model is
I have a class that derives from TcpClient, e.g. class ZClient : TcpClient {
I have a Player class that derives from Humanoid and Humanoid derives from Sprite

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.