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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:29:38+00:00 2026-05-28T01:29:38+00:00

I have a class A, which is parent to classes B and C. And

  • 0

I have a class A, which is parent to classes B and C.
And a class X, which is a parent to Y and Z.

class A {};
class B : public A {};
class C : public A {};


class X
{
    void foo(A) { std:: cout << "A"; }
};

class Y : public X
{
    void foo(B) {std::cout << "B"; }
};

class Z : public X
{
    void foo(c) {std<<cout <<"C"; }
};

int main()
{
    B b;
    C c;

    Y y;
    Z z;

    y.foo(b);//prints B // b is a B, and Y::foo takes a B, hence print B
    y.foo(c);//prints A // mismatch between types, fall back and print A
    z.foo(b);//prints A // mismatch between types, fall back and print A
    z.foo(c);//prints C // c is a C, and Y::foo takes a C, hence print C

    std::vector<A> v;
    v.push_back(b);
    v.push_back(c);

    //In this loop, it always prints A, but *this is what I want to change* 
    for (size_t i = 0; i < v.size(); ++i)
    {
        z.foo(v.at(i));
        y.foo(v.at(i));
    }
}

Is it possible to get the items to print the same result as the hard coded calls?
Meaning that I will treat them as their original type, and not its parent type?
or once I put them int a vector of A they will forever be of type 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-05-28T01:29:39+00:00Added an answer on May 28, 2026 at 1:29 am

    What you are seeing is Object Slicing.
    You are storing object of Derived class in an vector which is supposed to store objects of Base class, this leads to Object slicing and the derived class specific members of the object being stored get sliced off, thus the object stored in the vector just acts as object of Base class.

    Solution:

    You should store pointer to object of Base class in the vector:

    vector<X*> 
    

    By storing a pointer to Base class there would be no slicing and you can achieve the desired polymorphic behavior as well by making the functions virtual.
    The right approach is to use a suitable Smart pointer instead of storing a raw pointer in the vector. That will ensure you do not have to manually manage the memory, RAII will do that for you automatically.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
I have a MustInherit Parent class with two Child classes which Inherit from the
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
If I have 2 classes, one being the parent with the following: public class
I have two classes: public class Parent { public virtual long? ID { get;
I have a parent class which contains a child object. I am using set
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which looks something like this: public class Test { private
I have a class which constructor takes a Jakarta enums . I'm trying to

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.