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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:53:01+00:00 2026-05-27T17:53:01+00:00

Prerequisities: To understand this question, please, read the following question and its answer at

  • 0

Prerequisities:
To understand this question, please, read the following question and its answer at first:
Cast auto_ptr<Base> to auto_ptr<Derived>

At
Cast auto_ptr<Base> to auto_ptr<Derived> Steve answered that “Your static_cast would copy the auto_ptr to a temporary, and so aS would be reset and the resource would be destroyed when the temporary is (at the end of the statement).”

I’m interested in the process of temporary creation while static_cast is called.
I would like to have the code that I can trace in order to see this effect.
I cannot use static_cast<auto_ptr<Circle>> ... because it cannot be compiled, so I need to write some simulation class instead of auto_ptr and watch the process of temporary creation.

I also understand that temporary creation is closely connected with copy constructor call.
auto_ptr‘s ownership loosing is simulated with copy assignment that set the _radius field of source to negative value (I need the simple logical model of auto_ptr).

So, I suggest the following Circle class:

#include <iostream>

class Shape {};

class Circle: public Shape {
  double _radius;
public:
  explicit Circle(double radius = .5): _radius(radius) {}
  Circle &operator =(Circle &circle) {
    _radius = circle._radius;
    circle._radius = -1.;
    return *this;
  }
  Circle(Circle &circle) { *this = circle; }
  double GetRadius() { return _radius; }
};

int wmain() {
  using namespace std;

  Circle c1(100), c2(200), c3(300);
  c2 = c3;

  Shape *s1, s2;
  s1 = &c1;
  wcout << static_cast<Circle *>(s1)->GetRadius() << endl;

  return 0;
}

Ok. Here we can see that “ownership transferring” is taking place in c2 = c3.
BUT I cannot achieve temporary creation in static_cast.

The question is: how to make a small simulation of temporary object creation while static_cast?

I believe Steve that temporary object is created while casting. The only thing I want is to write an example that shows temporary creation. This target has academic reasons.

Can someone clarify how to achieve the effect described in Steve’s answer that he posted at the referred topic?

  • 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-27T17:53:01+00:00Added an answer on May 27, 2026 at 5:53 pm

    In your previous question, auto_ptr is the class that has ownership, and resets the source’s pointer to null when it is copied.

    Now, Circle is a class that simulates ownership, by resetting its radius to -1 when it is copied. So it’s like an auto_ptr in that way, but not in any other way.

    So, to observe loss of simulated ownership you need to copy a Circle, which is what you do with copy assignment in the line c2 = c3. Casting a Circle* doesn’t copy the object, just the pointer, but casting a Circle does copy the object:

    int main() {
        Circle c1(100);
        static_cast<Circle>(c1);
        std::cout << c1.GetRadius() << '\n';
    }
    

    Output is -1.

    Or if you specifically want to see it with a cast to a derived class:

    struct SpecialCircle: Circle {
        SpecialCircle(Circle &circle) : Circle(circle) {}
        explicit SpecialCircle(double radius = .5): Circle(radius) {}
    };
    
    int main() {
        SpecialCircle s1(100);
        Circle &c1 = s1;
        static_cast<SpecialCircle>(c1);
        std::cout << c1.GetRadius() << '\n';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I understand correctly, ClickOnce only checks for prerequisites with the first install of
One sign is that target does not exist, understand this. Another is by comparing
This question is probably language-agnostic, but I'll focus on the specified languages. While working
I'm trying to execute a single target calling make cleanlists. This target has no
First of all, I am entirely new to web development, so I apologize in
Before I start explaining the code I will first give my use case so
There is a WCF service which is running under IIS. This service accesses the
My question is similar to avoiding-re-building-prerequisites-in-ant , except that my need doesn't involve created
How do I startup a computer from C# code at a given time? This
In the first one, I put the minOccurs and maxOccurs in the sequence element:

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.