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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:24:32+00:00 2026-06-09T00:24:32+00:00

Here I attached a minimal case, where in child class, cmp will replace the

  • 0

Here I attached a minimal case, where in child class, cmp will replace the one in parent class, but it doesn’t work, it always calls the one from the parent

#include <iostream>

template <typename T>
class A
{
    public:
        void tell(T a, T b)
        {
            std::cout << (cmp (a, b) ? a : b) << " is better" << std::endl;
        }
    protected:
        bool cmp (T & a, T & b)
        {
            return a > b;
        }
};

template <typename T>
class B: public A<T>
{
    protected:
        bool cmp (T & a, T & b)
        {
            return a < b;
        }
};

int main ( int argc , char **argv ) 
{
    B<int> b;
    // should be: 3 is better here
    b.tell(5, 3);
    return 0;
}
  • 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-09T00:24:34+00:00Added an answer on June 9, 2026 at 12:24 am

    Your cmp function must be declared virtual in A.

    protected:
        virtual bool cmp (T & a, T & b)
        {
            return a > b;
        }
    

    It will implicitly remain virtual in derived classes, although I’d explicitly declare cmp virtual in B as well, for clarity’s sake.

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

Sidebar

Related Questions

Here is my working page . I attached buttons to white bishop and you
I've implemented the attached command behavior pattern found here and it works well to
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here is my work environment: Eclipse Juno as IDE with maven2 plugin on it
as you can see in the image I attached here: http://img202.imageshack.us/img202/6497/subversion.png the button to
i have attached TinyScrollBar(from [here] ) plugin with my project. It was working some
SharePoint newbie here. I have a web part that is attached to SharePoint's edit
Here is a flow that I can not figure out how to work. (
So here's my problem. I attached an image of what I'm working with to
This is quite difficult to explain so I have attached an excel file here:

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.