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

The Archive Base Latest Questions

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

Please help me to understand the following issue. Look at the code example below:

  • 0

Please help me to understand the following issue.

Look at the code example below:

#include <iostream>

class Shape {
public:
  virtual wchar_t *GetName() { return L"Shape"; }
};
class Circle: public Shape {
public:
  wchar_t *GetName() { return L"Circle"; }
  double GetRadius() { return 100.; }
};

int wmain() {
  using namespace std;

  auto_ptr<Shape> aS;
  auto_ptr<Circle> aC(new Circle);

  aS = aC;
  wcout << aS->GetName() << L'\t' << static_cast<auto_ptr<Circle>>(aS)->GetRadius() << endl;

  return 0;
}

Why I am not allowed to do this:

static_cast<auto_ptr<Circle>>(aS)->GetRadius()

Compiler (MSVCPP 11):

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory(911): error C2440: 'initializing' : cannot convert from 'Shape *' to 'Circle *'
1>          Cast from base to derived requires dynamic_cast or static_cast
  • 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:42:24+00:00Added an answer on May 27, 2026 at 4:42 pm

    auto_ptr doesn’t behave the same way as a pointer in this respect. There are special rules in the language to allow Shape* to be static_cast to Circle* when Circle derives from Shape. The downcast is not entirely type-safe, since it relies on the user to provide a pointer value that actually does point to the Shape base class sub-object of a Circle, but the standard allows it for convenience. auto_ptr is “just” a library class, and has no equivalent conversion.

    Even if you could do it, it would often go wrong. When you copy an auto_ptr, the original loses ownership of the resource. 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 expression). In your example that’s fine, since it’s going to be destroyed at return anyway, but generally speaking you don’t want to copy auto_ptr except at a function call parameter or return value, to indicate transfer of ownership from caller to callee, or vice versa.

    What you can do instead is static_cast<Circle*>(aS.get())->GetRadius(), or better yet restructure your code to avoid the need for a downcast. If you know that your object is a Circle, keep it in an auto_ptr<Circle>[*]. If you keep it in an auto_ptr<Shape>, then don’t rely on it being a Circle.

    [*] Or, if your implementation provides them, a better smart pointer such as unique_ptr, scoped_ptr or shared_ptr. Even if your implementation doesn’t provide them, there’s Boost.

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

Sidebar

Related Questions

Please help me to understand why the following code will not compile: #include <stdio.h>
Please help me to understand why the following code works: <script> var re =
Could someone please help me to understand why the following block of code keeps
Please help me understand the following: I create a CharBuffer using CharBuffer.wrapped(new char[12], 2,
Can someone please help me understand the following: In the previous version of NHibernate
Please help me understand the root cause of the following behaviour. In file a.cpp
Java docs says the following about Set interface, can someone please help me understand
Please help me understand what the following is doing. Specifically, what is variable 'c',
Please help me understand how the process goes. I understand that web browsers contain
I cannot understand how this is possible. Please help!! I have an app with

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.