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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:11:18+00:00 2026-05-31T14:11:18+00:00

Lets say I have a parent Class: Class Parent{ public: virtual void doSomething(){} }

  • 0

Lets say I have a parent Class:

Class Parent{
public:
virtual void doSomething(){}
}

and two children:

Class Son: public Parent{
public:
  void doSomething(){
  // Do one thing
  }
}

Class Daughter: public Parent{
public:
  void doSomething(){
  // Do another thing
  }
}

If I setup an instance of a child class like this:

Parent obj = Son();

How do I properly invoke the doSomething() method that is defined by Son and not the empty function in Parent

  • 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-05-31T14:11:19+00:00Added an answer on May 31, 2026 at 2:11 pm

    In order to do this you need to make the Parent declaration a pointer or a reference.

    Parent* obj = new Son();
    

    In it’s current form your declaring obj to be an instance of Parent. This means the assignment from Son() doesn’t create a reference to a Son instance, instead it slices the object into a Parent value.

    • http://en.wikipedia.org/wiki/Object_slicing
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two tables - child and parent with many-to-one relation. What
I have a simple 1:many aggregate relationship, lets say: public class Parent { public
So, lets say I have a store of parents children and the parent has
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have a parent class class parent { } ..... This parent
Let's say I have these two very basic entities: public class ParentEntity { public
Let's say I have a simple parent->child class structure as shown below Public Class
Let's say I have this simple structure class FooDefinition { public FooDefinition Parent {
Ok so lets say I have 3 Jpanels - 1 parent and 2 Children
Let's say that I have classes like this: public class Parent { public int

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.