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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:55:43+00:00 2026-06-06T18:55:43+00:00

Ok, I was reading through this entry in the FQA dealing about the issue

  • 0

Ok, I was reading through this entry in the FQA dealing about the issue of converting a Derived** to a Base** and why it is forbidden, and I got that the problem is that you could assign to a Base* something which is not a Derived*, so we forbid that.

So far, so good.

But, if we apply that principle in depth, why aren’t we forbidding such example?

void nasty_function(Base *b)
{
  *b = Base(3); // Ouch!
}

int main(int argc, char **argv)
{
  Derived *d = new Derived;
  nasty_function(d); // Ooops, now *d points to a Base. What would happen now?
}

I agree that nasty_function does something idiotic, so we could say that letting that kind of conversion is fine because we enable interesting designs, but we could say that also for the double-indirection: you got a Base **, but you shouldn’t assign anything to its deference because you really don’t know where that Base ** comes, just like the Base *.

So, the question: what’s special about that extra-level-of-indirection? Maybe the point is that, with just one level of indirection, we could play with virtual operator= to avoid that, while the same machinery isn’t available on plain pointers?

  • 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-06T18:55:44+00:00Added an answer on June 6, 2026 at 6:55 pm
    nasty_function(d); // Ooops, now *d points to a Base. What would happen now?
    

    No, it doesn’t. It points to a Derived. The function simply changed the Base subobject in the existing Derived object. Consider:

    #include <cassert>
    
    struct Base {
        Base(int x) : x(x) {}
        int x;
    };
    struct Derived : Base {
         Derived(int x, int y) : Base(x), y(y) {}
         int y;
    };
    
    int main(int argc, char **argv)
    {
      Derived d(1,2); // seriously, WTF is it with people and new?
                      // You don't need new to use pointers
                      // Stop it already
      assert(d.x == 1);
      assert(d.y == 2);
      nasty_function(&d);
      assert(d.x == 3);
      assert(d.y == 2);
    }
    

    d doesn’t magically become a Base, does it? It’s still a Derived, but the Base part of it changed.


    In pictures 🙂

    This is what Base and Derived objects look like:

    Layouts

    When we have two levels of indirection it doesn’t work because the things being assigned are pointers:

    Assigning pointers - type mismatch

    Notice how neither of the Base or Derived objects in question are attempted to be changed: only the middle pointer is.

    But, when you only have one level of indirection, the code modifies the object itself, in a way that the object allows (it can forbid it by making private, hiding, or deleting the assignment operator from a Base):

    Assigning with only one level of indirection

    Notice how no pointers are changed here. This is just like any other operation that changes part of an object, like d.y = 42;.

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

Sidebar

Related Questions

Reading through this excellent article about safe construction techniques by Brain Goetz, I got
I was reading through this interesting post about using JavaScript to generate an image
I was reading through this: http://www.postgresql.org/about/ And I saw this: An enterprise class database,
Love this website! My issue is as follows: I'm reading a zip file that's
I've spent the morning reading through page after page of StackOverflow posts about this
When reading through parfib.hs code on github, I saw this comment about memory allocation
Reading through this MSDN article http://msdn.microsoft.com/en-us/library/dd460648.aspx I'm seeing a claim that MEF doesn't have
I'm currently reading through this jquery masking plugin to try and understand how it
I have been reading through this wonderful website regarding the recommended Python IDEs and
I was reading through this thread: Hidden Features of JavaScript? and found this post:

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.