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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:29:46+00:00 2026-05-16T12:29:46+00:00

I’ve been programming a while now at school, and I’m working on my first

  • 0

I’ve been programming a while now at school, and I’m working on my first independent large project. I’ve been discovering a lot of things about programming that I haven’t known before and it’s been great.

However, more and more, I feel like I no longer understand C++ as a language the more I delve into it. I’d like to get some of my (mis) conceptions about references and pointers straight before continuing, and I hope that you, stackoverflow, could correct me if I’m wrong.

Thanks in advance, and here we go!

1. When converting between classes, what actually gets converted is the virtual table.

Example:

class A{
public:
    A() : x(0) {};
    int x;
    virtual void doStuff() {
        cout << x <<endl;
    }
};

class B : public A{
public:
    B() : y(1) {};
    int y;
    virtual void doStuff() {
        cout << y <<endl;
    }
};

If I had converted an object b of type B to A, what would happen internally is the virtual table of b would be discarded, and replaced with a corresponding virtual table of type A, and the destructor of y would be called because there is no longer a reference to it. Similarly, the doStuff in b would be made to point to the function address of A::doStuff instead of B::doStuff. The address pointing to x would stay the same however.

2. Which implies that the only way to take advantage of polymorphism is through pointers and references

As a consequence of point 1, the only way to take advantage of polymorphism in virtual methods of classes would be to use references and pointers, because if we passed by value, the classes itself would be automatically converted into the base class.

Example:

void doALotOfStuff(A a1, A a2) {
    a1.doStuff();
    a2.doStuff();
}

int main(){
    A a;
    B b;
    doALotOfStuff(a,b);
    return 0;
}

would print out

0
0  

because the compiler would generate code to convert b into A.

3. Furthermore, the only way to take advantage of this kind of polymorphism with arrays and STL containers would be to use pointers because references cannot be stored

Since vector would not work because references are not assignable, it follows that if I wanted to have a vector of base classes, I would need to create a vector of pointers of A in order to preserve the virtual table of elements of Type B.

Sorry if this was TL;DR, but it’s be kind of bothering me for a while when I was doing some class design on my project, and I realized I couldn’t really get away from just using either pointers or references because of library interfaces and polymorphism issues.

  • 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-16T12:29:46+00:00Added an answer on May 16, 2026 at 12:29 pm

    While you have some nice ideas in point #1, that’s not actually how it works. Conversion is not done in-place, it is done by creating a new object which copies the members it knows about from the conversion source (which is to say, the base class members, unless you have some really weird forward declarations in your base class). The instance of the derived object is not changed in any way. Of course the copy has a different memory address from the original object. Passing by value ALWAYS involves copying.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.