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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:17:51+00:00 2026-05-22T16:17:51+00:00

ok say we have the following classes class A { public: virtual void taco()

  • 0

ok say we have the following classes

class A
{
public:
    virtual void taco()
    {
        cout << "Class A" << endl;
    }
};
class B: public A
{
    public:
    virtual void taco()
    {
        cout << "Class B" << endl;
    }
};
class C : public A
{
    public:
    void taco()
    {
        cout << "Class C" << endl;
    }
};

Now if I do this

A a = A();
B b = B();
C c = C();
a.taco(); //Class A
b.taco(); //Class B
c.taco(); //Class C
deque<A> aa = deque<A>();
aa.push_back(a);
aa.push_back(b);
aa.push_back(c);
for(int i=0;i<aa.size();i++)
    aa[i].taco();//All Class A
A r = B();
r.taco(); //Class A

Now you’ll notice when I initialize A as B or C, it won’t fire the functions from B or C. I was wondering if there was any way around this? I understand the concept that since the object is A it uses A’s taco function, but I was just wondering if there was some trick to getting at the other functions. My project is fairly complicated, and I can’t know all the classes that will override A(due to plugins overriding a class). Also, I kinda need to have the base virtual function have a body to add default behavior. Thanks.

  • 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-22T16:17:52+00:00Added an answer on May 22, 2026 at 4:17 pm

    You must store pointers in the deque, since polymorphism only works with reference & pointer types. When you insert those objects into the deque, copies are made of type A, “slicing” off the parts that made them B or C originally.

    Similarly, A r = B() just creates a temporary B and copies the A part of it into an A called r.

    BTW by A a = A(); you might as well write A a;. They’re not completely equivalent, but they do the same job here, and you likely meant for the simpler version.

    A a;
    B b;
    C c;
    a.taco(); //Class A
    b.taco(); //Class B
    c.taco(); //Class C
    
    // With pointers and containers
    deque<A*> aa;
    aa.push_back(&a);
    aa.push_back(&b);
    aa.push_back(&c);
    for (int i=0; i<aa.size(); i++)
        aa[i]->taco(); // Hurray!    
    
    // With refs
    B q;
    A& r = q;
    r.taco(); // Class B!
    

    (Just remember that those objects a, b and c have automatic storage duration. The moment they go out of scope, if the deque still exists then all its elements are invalid pointers. You may want to employ dynamic allocation to further control the lifetime of the A, B and C objects.. but I’ll leave that as an exercise to the reader.)

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

Sidebar

Related Questions

I have the following classes: class A { public: virtual void f() {} };
Say i have the following two classes: public class User { public int ID
Say I have the following classes: public class A { public string x; }
Say I have the following classes public class TestA { public string Blah {
Let's say I have two classes like the following: Class A { public: ..
Say we have the following two classes, A is the base class with virtual
With Castle Windsor, let's say I have the following classes: public class LowLevelComponent {
Say I have the following abstract class: class AbstractClass { public: AbstractClass() {} virtual
Let's say I have following classes. (only most important things included) public class Client
Lets say that I have the two following classes public class OtherClass { public

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.