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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:25:23+00:00 2026-05-20T00:25:23+00:00

i have a function that does this: static MyClass* MyFunction(myparams) { return new MyClass(myparams)

  • 0

i have a function that does this:

static MyClass* MyFunction(myparams)
{
    return new MyClass(myparams)
}

and i would be able to call this function inside another one that has the following signature:

void MyFunction2(std::auto_ptr<MyClass> myparam)

but when I try to do it i have a compiler error:

Impossible to convert the first param
from MyClass * to std::auto_ptr<_Ty>

why? Thank you for any help

EDIT 1
As asked the myparams types are normal but there is also a T param because the function is inside a template class

  • 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-20T00:25:23+00:00Added an answer on May 20, 2026 at 12:25 am

    std::auto_ptr<> has an explicit constructor, like any other smart pointer. That means that there is no implicit conversion from T* to std::auto_ptr<T> to prevent from accidentally deleting an object. Hence you need to convert your raw pointed to std::auto_ptr<> explicitly:

    MyFunction2(std::auto_ptr<MyClass>(MyFunction()));
    

    It is also a good idea to make your factory functions return a smart pointer instead of a raw pointer, it makes it clear to the reader that the ownership of an object is being transferred to the caller:

    static std::auto_ptr<MyClass> MyFunction(myparams)
    {
        return std::auto_ptr<MyClass>(new MyClass(myparams));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++, when you have local variables in a static member function, does it
Aloha, I have a custom control that I need to instantiate from within a
Her is my code: class MyClass { public $prop; public function method () {
Hey php gurus. I'm running into some bizarre class scope problems that clearly have
I have a working implementation of NDK library and corresponding Java-class. But I am
I created a widget that works great until I restart the phone, then the
I am trying to render an openGL es surface from the NDK, but got
I am currently trying to make a GUI for some software and I'm having
Sorry in advance for the question length, but I wanted to summarize my knowledge

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.