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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:00:04+00:00 2026-05-27T16:00:04+00:00

virtual function resolution happens with pointer/reference and not with object. Now consider below example:

  • 0

virtual function resolution happens with pointer/reference and not with object. Now consider below example:

struct Base { virtual void foo (); };
struct Derived : Base { void foo (); };

Derived d[2];
Base *p = d;
p[0].foo();  // calls Derived::foo()!

My perception was like this: for any array T arr[SIZE]; the type of arr[N] is T (and not T&), i.e. arr[N] is an object. Had it been a case, then in above sample p[0] would call Base::foo(), because p[0] should resolved to an object.

However, it’s wrong. Can someone explain, why p[0] is resolving to Base& and not Base ? Is it because p[0].foo() is equivalent to (p+0)->foo()?

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

    However, it’s wrong. Can someone explain, why p[0] is resolving to
    Base& and not Base ? Is it because p[0].foo() is equivalent to
    (p+0)->foo()?

    You got it — it is the “array indirection operation”.

    The following are equivolent:

    a[1];
    *(a + 1)
    

    It’s in the standard: Literally, the “array indirection operation” means that *(a + n) substitutes for a[n], so the result is a reference to an object, not a copy-constructed value of an object.

    This was essential to get polymorphism to work, and to ensure derive types were not “copy-constructed” to base types.

    Extending further, the following are also equivolent, and result in a reference-to-object:

    a[1][2][3];
    *(*(*(a + 1) + 2) + 3);
    

    [EDIT] To be explicit, in your example, yes, these are exactly equivalent:

    p[0].foo();
    (p+0)->foo();    // The "->" is the "*" indirection
    (*(p+0)).foo();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider: struct device{ virtual void switchon() = 0 {} }; int main() { }
I know that, how to implement virtual function call resolution is not part of
When i invoke a virtual function from a base constructor, the compiler does not
I have a base class MyBase that contains a pure virtual function: void PrintStartMessage()
If we write virtual function it adds a vtable in object of that class.
Which virtual table will be pure virtual function located? In the base class or
I have a base class with an optional virtual function class Base { virtual
I just read that we should not use virtual function excessively. People felt that
I'm trying to benchmark the difference between a function pointer call and a virtual
I have a base class with a virtual function. virtual CString& Foo(); I want

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.