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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:01:32+00:00 2026-06-06T22:01:32+00:00

Warning: Note that this is a dumb question of something that I would probably

  • 0

Warning: Note that this is a dumb question of something that I would probably never solve in this manner. Hence the contradicting title. But since I have actually seen something similar in code I have gone through on a very large system I became intrigued. 🙂

Let’s say we have a simplified class:

class Foo
{
  void a();
  virtual void b();
}

and then another class

class Bar : public Foo
{
  void a();
}

If a general part of the program handles all these classes as the base class of type Foo, how do I in the best manner call the “correct” version of function a within b? Since there is a possibility that the object is of type Bar.
And say you, for legacy reasons, could not change the existing code and make the base class a function virtual etc.

What I did was to make a virtual in the base class and implement b within Bar since I had that option. But let’s, for the sake of argument, say that this was impossible or disallowed. How “wrong” would it be to implement a workaround using something like this.

void b() {
  ...

  Bar* dabar;
  if((dabar = dynamic_cast<Bar*>(this)) != NULL) {
    dabar->a();
  }
  else {
    a();
  }
}

As I said, note that this is when dealing with base classes and wanting to call the function of a child class. Not the other way around.

  • 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-06T22:01:39+00:00Added an answer on June 6, 2026 at 10:01 pm

    If there is a possibility to make your function virtual in the base class,
    this is the way to go, including the overhead of calling a different company
    to change there code, as this is clearly a design flaw.

    If you do not have this possibility, for what reason whatsoever,
    your code will work, except when you want to derive from Bar.
    What do you think will happen, when you have a Bar* and call “a” on that.

    Like:

    Chair : public Bar {...}
    
    Bar *bar = new Chair;
    bar->a();
    // what the heck?
    

    And, of course, dynamic_cast will imply a serious disadvantage on performance.

    Write clean code, write what you want to express, do not use tricks!

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

Sidebar

Related Questions

Warning: This is inherited legacy code that was initially put together in the early
Warning: This question has been heavily edited. I tried my best to guess the
Another cry for help about this warning. First of all I have looked at
(Note that I've searched online for the warnings I'm describing below, and have come
Fair warning: I am no expert, but I did manage to get this far.
How can I prevent the following warning from Homebrew appearing whenever I install something?
We've been avoiding this PMD warning by moving most of our constructor code into
I'm trying to do a simple sprintf like this: snprintf(dest, maxlen_dest, %'lu, along); Note
Alright, I presented this question on the MSDN forums but have yet to receive
WARNING: content not work-appropriate This does not retrieve image successfully from a web page

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.