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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:18:35+00:00 2026-06-03T22:18:35+00:00

Generally it appears preferable in C++ that code that functions that take a parameter

  • 0

Generally it appears preferable in C++ that code that functions that take a parameter to a polymorphic object takes a pointer to it:

class Base {};
class DerivedA : public Base {};
class DerivedB : public Base {};

void OperateOnObject(Base* obj) {};

vector<Base*> objects;
objects.push_back(new DerivedA());
objects.push_back(new DerivedB());

for (size_t i=0; i<objects.size(); i++) {
  OperateOnObject(objects[i]);
}

Why is it that OperateOnObject() is usually written to take a pointer to Base rather than a reference? Are there potential problems with slicing? (eg the vtable gets lost)

I’ve seen this response to a similar-sounding question, but it doesn’t seem to address the same issue.

  • 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-03T22:18:39+00:00Added an answer on June 3, 2026 at 10:18 pm

    References do not present problems with slicing – in this respect, they are as good as pointers. There is no “hard” reason to prefer pointers, unless you want semantics of NULL to be available to your code (i.e. an ability to pass “nothing”, which is not there if you use references).

    I think the pointer types are often used as parameters of polymorphic functions to match the semantic of collections: since you cannot make a vector of references, your code will end up having to dereference pointers obtained from collections before passing them to functions. This may be annoying.

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

Sidebar

Related Questions

Highly repetitive code is generally a bad thing, and there are design patterns that
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
Generally speaking, the SQL queries that I write return unformatted data and I leave
In F#, the equality operator (=) is generally extensional, rather than intensional. That's great!
After reading some posts like this one: Choose File Dialog It appears that Android
I'm cleaning up some pretty complicated code that I didn't write and I'm looking
I have a weird condition that only appears on gcc -O0 -g output, not
I generally do the following, but I'm hoping for a library that handles this
Update: After some experimentation, it appears that the thing that is slow is when
I generally don't like asking what's wrong with my code questions, but this is

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.