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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:28:24+00:00 2026-05-11T10:28:24+00:00

I have a base class and a derived class. Each class has an .h

  • 0

I have a base class and a derived class. Each class has an .h file and a .cpp file.

I am doing dynamic_cast of the base class object to the derived class in the following code:

h files:

class Base {   public:     Base();     virtual ~Base(); };  class Derived : public Base {   public:     Derived(){};     void foo(); };  class Another {   public:     Another(){};     void bar(Base* pointerToBaseObject); }; 

cpp files:

Base::Base() {     //do something.... } Base::~Base() {     //do something.... } void Derived::foo() {     Another a;     a.bar(this); } void Another::bar(Base* pointerToBaseObject) {     dynamic_cast<Derived*>(pointerToBaseObject) } 

From some strange reason, the casting fails (returns NULL). However, the casting succeeds if I move the implementation of Derived class’s constructor from .h to the .cpp file.

What can cause it?

The compiler is gcc 3.1, on Linux-SUSE. BTW, I see this behavior only on this platform, and the same code works fine in Visual Studio.

  • 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. 2026-05-11T10:28:25+00:00Added an answer on May 11, 2026 at 10:28 am

    The code, as posted, shouldn’t fail, provided you have a virtual function in the base class (as litb pointed out).

    But I believe every current compiler generates a ‘Base class is not polymorphic’ kind of error if you hadn’t, so that probably won’t be the problem.

    The only thing I can think of is that due to some weird bug everything gets inlined and no vtable gets generated. But if you put the constructor in the C++ file, the compiler decides not to inline everything, triggering the creation of a vtable, causing your cast to work.

    But this is very wild guesswork, and I don’t think any compiler would have such a mistake in it (?)

    If you want a definite answer, post more code. And the compiler / platform used.

    EDIT: Seeing the updated code

    I think you should at least derive Derived from Base 😉 (I suppose that’s a typo)

    But after seeing the code, the only thing I can think of is that gcc (wrongly) inlines everything and doesn’t generate a vtable for Derived. For what it’s worth, this runs fine compiled with gcc 4.0

    3.1 is over 7 years old by now… if there’s any possibility to upgrade I’d go for it.

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

Sidebar

Related Questions

I have an abstract base class and derived class: type TInterfaceMethod = class public
I have a base class called Component which has a number of classes derived
Suppose I have a base class B, and a derived class D. I wish
I have a base class which is non-generic with a derived generic class. The
Let's say I have a Base class and several Derived classes. Is there any
In C++, is it possible to have a base plus derived class implement a
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
I have a base class object array into which I have typecasted many different
I have a class hierarchy, and each class in it has an exception class,
my application has the following structure: public class Transaction { public int TransactionID {

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.