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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:28:56+00:00 2026-06-10T22:28:56+00:00

I have a question about inheritance and template methods. Suppose I have this two

  • 0

I have a question about inheritance and template methods. Suppose I have this two classes

class Base
{
public:
   template<typename T>
   void print(const T& s) {std::cout << "Base (templated) prints " << s << "\n";}
   virtual void print(int i) {std::cout << "Base prints " << i << "\n";}
};

class Derived : public Base
{
public:
   void print(int i) {std::cout << "Derived prints " << i << "\n";}
}

int main()
{
   Derived d;
   d.print(3);     // works fine
   std::string s = "hi";
   d.print(s);  // does not compile
   return 0;
}

The compiler tells me ”no matching function for call to ‘Derived::print(std::string&)’’.
But Derived, inheriting from Base, should also allow a call to the template method print(..), no?

Things are also weird cause if I don’t define a method “print” in the derived class, then everything works fine and the compiler calls the base class template method.

Things work fine also if I define the template method also in the derived class, which calls the base class one, but that does not seem right to me…

Thanks for your help.

  • 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-10T22:28:58+00:00Added an answer on June 10, 2026 at 10:28 pm

    Declaring a function in the derived class hides any functions with the same name in the base class. You can unhide them with a using declaration:

    class Derived : public Base
    {
    public:
        // Add this
        using Base::print;
    
        void print(int i) {std::cout << "Derived prints " << i << "\n";}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some rudimentary question about inheritance in C#. I have two classes. I
I've got question about nested inheritance in C++. I have three classes: Base ,
Say I have 4 classes: class I { public: virtual void X() = 0;
I have a question about inheritance. From this source: gSpan.h struct Edge { int
Just a small question about c++/cli. Abstract classes have abstract methods to be implemented
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I have a general question about inheritance in the .NET framework, lets say you
I have a question about Hibernate. I'm using 3.1.3 version and I have two
If I have for example two classes A and B , such that class

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.