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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:39:17+00:00 2026-06-05T12:39:17+00:00

I was going through an article by Don Clugston on Codeproject. It is a

  • 0

I was going through an article by Don Clugston on Codeproject. It is a beautiful article and is quite famous. In the following code snippet, I found a particular notion very difficult to comprehend:

class A {
 public:
       virtual int Afunc() { return 2; };
};

class B {
 public: 
      int Bfunc() { return 3; };
};

// C is a single inheritance class, derives only from A
class C: public A {
 public: 
     int Cfunc() { return 4; };
};

// D uses multiple inheritance
class D: public A, public B {
 public: 
    int Dfunc() { return 5; };
};

The snippet is followed by the following paragraph:

Suppose we create a member function pointer for class C. In this
example, Afunc and Cfunc are both member functions of C, so our member
function pointer is allowed to point to Afunc or Cfunc. But Afunc
needs a this pointer that points to C::A (which I’ll call Athis),
while Cfunc needs a this pointer that points to C (which I’ll call
Cthis).
Compiler writers deal with this situation by a trick: they
ensure that A is physically stored at the start of C. This means that
Athis == Cthis. We only have one this to worry about, and all’s well
with the world.

The one and the only thing I want to understand is the line in BOLD and ITALICS in the above paragraph.

I do not fully comprehend Afunc needs a pointer to C::A while Cfunc needs a pointer to C is quite natural.

Any help would be appreciated.

  • 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-05T12:39:19+00:00Added an answer on June 5, 2026 at 12:39 pm

    By calling a member function in C++, what internally happens is that the instance is passed as a hidden first argument (note that this behaviour is strictly implementation-defined behaviour, though. The C++ standard has nothing to say on this topic, it’s just a very common way of implementing it):

    x.f(y); // is treated internally as:
    f(&x, y);
    

    This first argument is then available via the this pointer.

    now, Afunc in the example above internally has the signature void Afunc(A* const this) while CFunc has the internal signature void CFunc(C* const this).

    Note that the argument types in both cases are different, so when you call the functions on the same object, a different pointer has to be passed. C++ solves this by defining an implicit conversion from any derived object to its base object. That is, in the following code:

    C* pc = something;
    pc->Afunc();
    

    This code is treated internally similar to the following (pseudo-code):

    C* pc = something;
    Afunc(static_cast<A*>(pc));
    

    This cast is, for single-inheritance, a no-operation (i.e. it can just be removed) through the trick mentioned in the quote: the C object and its parent A object are stored at the same physical address. An object of type C which is stored at an address x in memory is physically laid out in such a way that its parent object of type A is also stored at the address x, and is followed by all other members that C may have (but in your case it has no members, and sizeof(C) == sizeof(A)).

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

Sidebar

Related Questions

I was going through an article today when it mentioned the following: We've found
Going through Javascript documentation, I found the following two functions on a Javascript object
While going through Effective C++ (by Scott Meyers), I came across the following code
I am going through the article on JSF LifeCycles . I have the following
I am going through condition variable article at following location http://software.intel.com/en-us/blogs/2010/10/01/condition-variable-support-in-intel-threading-building-blocks/ Here we have
Just going through the sample Scala code on Scala website, but encountered an annoying
While going through the syslog file, I found a message like shutdown via gdm
I was going through this article today. http://vinothbabu.com/2010/05/08/update-and-insert-differences-in-syntax-is-an-inconvenience/ I was not able to understand
I was going through an article on event bubbling in asp.net and came to
Going through this article class ThreadTest { static void Main() { Thread t =

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.