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

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Stand-alone REPL (no browser/DOM, just JavaScript): JavaScript Shell from the… May 11, 2026 at 2:41 pm
  • added an answer I have always used the following: If Request.QueryString('MyQueryString') IsNot Nothing… May 11, 2026 at 2:41 pm
  • added an answer A low-level solution is to use flock(). Each instance would… May 11, 2026 at 2:41 pm

Related Questions

Can anyone figure out a nice way to get the following code to work?
I have a base class Node which contains a list of child nodes. Node
If I have a table in MySQL which represents a base class, and I
I have a class, lets call it A, and within that class definition I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.