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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:17:21+00:00 2026-05-28T05:17:21+00:00

I was playing around with a bunch of casting operators a while ago, which

  • 0

I was playing around with a bunch of casting operators a while ago, which apparently I did not really get at the time. I was looking over my code now and I saw this:

Base(Derived &p_derived) : m_state(static_cast<Base>(p_derived).m_state){}

I think I have now got a better understanding of casting of pointers and references thanks to a very good answer here on stackoverflow, but now there’s objects involved. What exactly happens when the reference is casted to an object? Or what if p_derived was the object itself and not just a reference? Would it be creating new objects and if so, how would these be instanciated?

I’m also surprised that this could be compiled at all since in Base.h, where this code is found, Derived is only forward declared meaning it shouldn’t know that it actually derives from Base. I tried at another place to do a static cast from a Derived* to a Base*, but that would rightfully not compile due to the types being incompatible as far as that translation unit was concerned.

  • 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-28T05:17:21+00:00Added an answer on May 28, 2026 at 5:17 am

    When you use a reference, you’re generally actually using the object it refers to. That’s the magic transparency of reference syntax that you don’t get with pointers; arguably it’s their reason for being.

    This scenario is no exception: you’re taking the p_derived referee and constructing a new Base out of it via slicing.

    static_cast<Base&>(p_derived) will cast the reference itself, if that’s what you’re after (and even if it’s not :P).

    I’m also surprised that this could be compiled at all since in Base.h, where this code is found, Derived is only forward declared meaning it shouldn’t know that it actually derives from Base.

    Yea, that surprises me, too. Are you sure? (Demo / Demo – not sure which you’re using)

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

Sidebar

Related Questions

While playing around with regexps in Scala I wrote something like this: scala> val
been playing around with ResolveClientUrl(~/Confirmation.aspx) and other methods.. I am tryin go get the
EDIT - After playing around with a bunch of potential solutions (using backgroundworker and
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
I've been reading a bunch and playing around with Castle Windsor 1.0 RC3 lately
been playing around with Capistrano to get an automated deploy between my server and
I've been playing around with cryptocat , which is an interesting online chat service
Playing around with id() . Began with looking at the addresses of identical attributes
Im playing around with Recurly, the app enables real time push notifications. My question

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.