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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:41:01+00:00 2026-05-11T09:41:01+00:00

I feel like this one has been asked before, but I’m unable to find

  • 0

I feel like this one has been asked before, but I’m unable to find it on SO, nor can I find anything useful on Google. Maybe ‘covariant’ isn’t the word I’m looking for, but this concept is very similar to covariant return types on functions, so I think it’s probably correct. Here’s what I want to do and it gives me a compiler error:

class Base; class Derived : public Base;  SmartPtr<Derived> d = new Derived; SmartPtr<Base> b = d; // compiler error 

Assume those classes are fully fleshed out… I think you get the idea. It can’t convert a SmartPtr<Derived> into a SmartPtr<Base> for some unclear reason. I recall that this is normal in C++ and many other languages, though at the moment I can’t remember why.

My root question is: what is the best way to perform this assignment operation? Currently, I’m pulling the pointer out of the SmartPtr, explicitly upcasting it to the base type, then wrapping it in a new SmartPtr of the appropriate type (note that this is not leaking resources because our home-grown SmartPtr class uses intrusive reference counting). That’s long and messy, especially when I then need to wrap the SmartPtr in yet another object… any shortcuts?

  • 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. 2026-05-11T09:41:02+00:00Added an answer on May 11, 2026 at 9:41 am

    Both the copy constructor and the assignment operator should be able to take a SmartPtr of a different type and attempt to copy the pointer from one to the other. If the types aren’t compatible, the compiler will complain, and if they are compatible, you’ve solved your problem. Something like this:

    template<class Type> class SmartPtr {     ....     template<class OtherType> SmartPtr(const SmartPtr<OtherType> &blah) // same logic as the SmartPtr<Type> copy constructor      template<class OtherType> SmartPtr<Type> &operator=(const SmartPtr<OtherType> &blah) // same logic as the SmartPtr<Type> assignment operator }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this must have been asked before, but I've looked at Knockout
This question has been asked before, but I would like a little more detail
I feel like this question has to have been asked but for the life
I know this question has been asked before, but I feel it wasn't asked
I know this question has been asked 100x but I can't seem to find
I feel like this question has probably been asked a thousand times already, so
I feel like this is a stupid question, but I can't think of a
I feel like this is a common problem but nothing I've researched has worked
Although I feel like this question has been beat to death. I still haven't
edited for clarity I feel like this question already has an answer, but I

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.