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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:03:00+00:00 2026-06-15T16:03:00+00:00

I am trying a basic thing with two classes and a free functions. First

  • 0

I am trying a basic thing with two classes and a free functions. First I have two classes :

struct BASE{
    BASE(int a = 0):a_(a){};
    virtual ~BASE(){};
    virtual void foo(){std::cout << "BASE " << std::endl;}
    int a_;
};

struct DERIVED: public BASE{
    DERIVED():BASE(){};
    void foo(){std::cout << "DERIVED " << std::endl;}
};

then I fill up a std::vector (or boost::ptr_vector)

std::vector<BASE* > vec;    
vec.push_back( new BASE(2));
vec.push_back( new DERIVED);

If I call my function foo, no pb the virtual stuff works well,
but if I create two free functions :

void foo(BASE*   a, DERIVED*   b){
    std::cout << " mix base/derived " << std::endl;
}

void foo(BASE*   a, BASE*   b){
    std::cout << " full base " << std::endl;
}

if I do

foo(vec[0], vec[1]); //failed

I will get the message

full base

Well it is logical because I have vector of BASE*, but does it exist anyway
to get the good call to my free function ? I need the inheritance so I can not really separate the 2 classes because I must fill up this container

Moreover at the end my vector will be fill up randomly, so I can not know by advance how cast properly.

  • 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-15T16:03:01+00:00Added an answer on June 15, 2026 at 4:03 pm

    The simplest (and quickest) way to get workaround is to introduce one more virtual function to unique identify derived classes (using int, enum values or typeid). So later you may call it and realize what exact derived class (or maybe base) you’ve got behind the BASE*, and do dynamic_cast to that type.

    You gave no details about what problem you are trying to solve… just make sure that you’ve done a complete reaseach for existed solutions if you want to implement a Double Dispatch (or some kind of)…

    But usually, if you need dynamic_cast it means that smth wrong with you design (OOP model of domain)…

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

Sidebar

Related Questions

I'm trying a basic thing. I have two files: curl.php and form.php curl.php <?php
I'm trying to learn this basic thing about processors that should be taught in
I've recently been trying to teach myself Perl and have been doing some basic
I have two objects of classes Person and Employee. Both classes have common attribute
I'm just learning PHP and am trying the most basic thing: capturing info from
I am trying to do a very basic thing, which I though I should
I am trying to make a basic captcha module for jQuery. I have a
I have been trying to validate my web page for the last two hours,
I have a basic 8-bit ALU described in Verilog. I am trying to implement
I've been trying to debug this for 2 hours, have to sleep. First, I've

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.