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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:24:08+00:00 2026-05-30T02:24:08+00:00

I’m working on a C++ API which exports several classes from a DLL. A

  • 0

I’m working on a C++ API which exports several classes from a DLL.

A public class interface should follow the following conventions:

  • All functions return an error code.
  • Output parameters are used for additional return values.
  • Pass by pointer is used for Output parameters.
  • Pass by const reference is used for Input parameters (pass by value for primitive types).
  • When the client should take ownership of output parameter shared_ptr is used, otherwise a normal pointer.

Example interface:

typedef std::shared_ptr<Object> ObjectPtr;

class APIClass
{
    ErrorCode SetSomething(int i);
    ErrorCode IsSomethingSet(bool* ask);
    ErrorCode DoSomething();
    ErrorCode GetSomething(ObjectPtr* outObj);
}

Example usage:

ErrorCode res;
ObjectPtr obj;
res = myApiClass->GetSomething(&obj);

GetSomething implementation:

ErrorCode APIClass::GetSomething(ObjectPtr* outObj)
{
   ObjectPtr temp(new Object(), CleanUpFunction<Object>);

   // Do something with object temp.
   ...

   *outObj= temp;

   return OK;
}

Is it save to use a shared_ptr in this way or are there possible issues I should be aware of?

  • 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-30T02:24:10+00:00Added an answer on May 30, 2026 at 2:24 am

    This is fine, but I’d ask whether a shared pointer is really necessary in this case. Mostly because you can’t release a pointer from a shared_ptr in any sane way… this can lead to problems later. And shared_ptr really means unspecified or shared ownership of the underlying resource.

    I typically document the function and use something like:

    // Caller must delete the outObj once done.
    ErrorCode APIClass::GetSomething( Object* & outObj )
    {
      // I use auto_ptr so I can release it later...
      // Mostly I hate auto_ptr, but for this its invaluable.
    
      auto_ptr<Object> obj( new Object );
      ...
      outObj = obj.release();
      return OK;
    }
    

    This way it is up to the client what they want to store the pointer into, and it is clear that ownership of the object passes to the caller.

    Client code can then use an appropriate container.

    Object * obj_raw;
    ErrorCode ec = apiClass.GetSomething( obj_raw )
    if( ec!=OK ) { .. do something with ec .. }
    shared_ptr<Object> obj( obj_raw );
    

    or

    auto_ptr<Object> obj( obj_raw );
    

    or

    scoped_ptr<Object> obj( obj_raw); 
    

    etc.

    Note that this can be made neater if you change your function definition to:

    // Caller must delete the return value.
    // On error, NULL is returned and e filled in appropriately.
    Object* APIClass::GetSomething( ErrorCode & e )
    {
       auto_ptr<Object> obj( new Object );
       ..
       e = OK;
       return obj.release();
    }
    
    //Now using it looks like this:
    ErrorCode ec;
    shared_ptr<Object> obj( apiObject.GetSomething(ec) ); 
    if(!obj)
    {
       .. do something with ec ..
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Does anyone know how can I replace this 2 symbol below from the string

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.