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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:03:43+00:00 2026-06-11T21:03:43+00:00

Rather than a long explanation, I think a few lines of code will be

  • 0

Rather than a long explanation, I think a few lines of code will be clearer to explain my problem. Lets say I have 2 classes, one inheriting from the other :

#include <iostream>
class A{
    public: 
        void parse() 
        {
            treatLine();
        };
        void treatLine()
        {
            std::cout << "treatLine in A" << std::endl;
        }
};

class B : public A{
    public:
        void treatLine()
        {
            std::cout << "treatLine in B" << std::endl;
        }
};

int main()
{
    B b;
    b.parse();
    return 0;
}

When executing, surprisingly for me, it prints “treatLine in A”. Is it possible to make function treatLine from class B to be called whith an object of type B (ie “treatLine in B” would be print for the code above)? And keep the possibility of creating an object of type A that would print “treatLine in A”

  • 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-11T21:03:44+00:00Added an answer on June 11, 2026 at 9:03 pm

    Make treatLine virtual:

    virtual void treatLine() {
        std::cout << "treatLine in A" << std::endl;
    }
    

    That way, the function will be called based on the runtime type of the object (called the “dynamic type”), instead of the compiletime type (called the “static type”) of the object.

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

Sidebar

Related Questions

Is it any faster to use myString.replace(/foo/g,bar) rather than myString.split(foo).join(bar) for long strings in
Rather than have everything in one big dialog, I'm looking at being able to
Rather than have a NULL in a column, I want a 0 to be
I have a few infinite generator methods, including some long-running and infinitely-long-running generators. IEnumerable<T>
Is there an API convert long/lat to city,country rather than Google API? I found
In which I mean rebasing in the dictionary, rather than git definition... I have
I have a server running virtual hosts that get changed quite often. Rather than
I have a LONG .txt file that contains almost 6000 lines! Sometimes I need
How to open an URL from code in the built-in web browser rather than
We have a web application that saves survey data in XML format, rather than

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.