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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:14:09+00:00 2026-06-10T09:14:09+00:00

I’m having a problem with polymorphism and parent-child relationships. Take the following two classes,

  • 0

I’m having a problem with polymorphism and parent-child relationships. Take the following two classes, and imagine in my application I create an object of ‘child’. Outside of the objects, the only function I call is update(), which in turn invokes everything else I need within the object. The only function in this sequence of calls that differs in ‘parent’ and ‘child’ is dosomething2().

class parent 
{
    public:
        void dosomething1() {}; //amongst other things, calls dosomething2
        void dosomething2() {};
        void update() {}; //amongst other things, calls dosomething1
}

class child : public parent
{
    void dosomething2() {}
}

I realized that if I create an object of ‘child’ and call update(), only the parent class functions will be called. I had to copy update() and dosomething1() to the child class, and then everything worked as expected. That’s not a very nice solution though because there is a lot of duplicate code – is there a way to improve this?

UPDATE:
Thanks to the answers provided, I updated the parent class with a virtual dosomething2() function. Now the correct function is invoked, and I don’t have to copy dosomething1() to the child class. Unfortunately, if I do not copy update() to the child class, I get a symbol lookup error (both parent and child are in a library together).

class parent 
{
    public:
        void dosomething1() {}; //amongst other things, calls dosomething2
        virtual void dosomething2() {};
        void update() {}; //amongst other things, calls dosomething1
}

class child : public parent
{
    void dosomething2() {}
    /*void update() {}; //symbol lookup error if this is left uncommented */
}

UPDATE2
The symbol lookup error was not related to the code. I did a make clean, and then make (which builds a .so library and the executable) and now things work as expected.

  • 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-10T09:14:11+00:00Added an answer on June 10, 2026 at 9:14 am

    You should use keyword virtual in parent-class if you want dynamic-polymorphism. And then reimplement or no reimplement virtual functions in child class.

    class parent 
    {
        public:
            void dosomething1() {}; //amongst other things, calls dosomething2
            virtual void dosomething2() {};
            void update() {}; //amongst other things, calls dosomething1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from

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.