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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:01:19+00:00 2026-06-13T18:01:19+00:00

In C++, should my method return an object or a pointer to an object?

  • 0

In C++, should my method return an object or a pointer to an object? How to decide? What if it’s an operator? How can I define?

And one more thing – if the pointer turns to be a vector, how can I find out its size after returned? And if it’s impossible, as I think it is, how should I proceed to correctly return an array without this limitation?

  • 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-06-13T18:01:20+00:00Added an answer on June 13, 2026 at 6:01 pm

    In C++, should my method return an object or a pointer to an object?
    How to decide?

    Since C++11 we have move semantics in C++ which means that it as easy as before and now also fast to return by value. That should be the default.

    What if it’s an operator? How can I define?

    Many operators such as operator= normally return a reference to *this

    X& X::operator=(X rhs); 
    

    You need to look that up for each operator if you would like to comply with the usual patterns (and you should). Start here: Operator overloading

    As pointed out by Ed S. return value optimization also applies (even before C++11) meaning that often object you return need neither be copied or moved.

    So, this is now the way to return stuff:

    std::string getstring(){ 
       std::string foo("hello");
       foo+=" world";
       return foo;
    }
    

    The fact that I made a foo object here is not my point, even if you did just do return "hello world"; this is the way to go.

    And one more thing – if the pointer turns to be a vector, how can I
    find out its size after returned? And if it’s impossible, as I think
    it is, how should I proceed to correctly return an array without this
    limitation?

    The same goes for all copyable or movable types in the standard (these are almost all types, for example vectors, sets, and what not), except a few exceptions. For example std::arrays do not gain from moving. They take time proportional to the amount of elements. There you could return it in a unique_ptr to avoid the copy.

    typedef std::array<int,15> MyArray;
    std::unique_ptr<MyArray> getArray(){ 
      std::unique_ptr<MyArray> someArrayObj(new MyArray());
      someArrayObj->at(3)=5;
      return someArrayObj;
    }
    
    int main(){
      auto x=getArray();
      std::cout << x->at(3) <<std::endl; // or since we know the index is right: (*x)[3]
    }
    

    Now, to avoid ever writing new anymore (except for experts in rare cases) you should use a helper function called make_unique. That will vastly help exception safety, and is as convenient:

    std::unique_ptr<MyArray> getArray(){ 
      auto someArrayObj=make_unique<MyArray>();
      someArrayObj->at(3)=5;
      return someArrayObj;
    }
    

    For more motivation and the (really short) implementation of make_unique, have a look here:
    make_unique and perfect forwarding

    Update

    Now make_unique is part of the C++14 standard. If you don’t have it, you can find and use the whole implementation from the proposal by S.T.L.:

    Ideone example on how to do that

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

Sidebar

Related Questions

What return type should one return from a method that is getting rows from
How can I specify that a method should take as parameter a pointer to
In C++, is a const method returning a pointer to a non-const object considered
I have a method that returns lot of data, should I use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) for
I wrote a PHP CUrl Class, if i execute Methods which should return the
Which method should I override to be guaranteed to always get the correct size
What method should I follow in java to produce WordWord from Word#$#$% Word 1234
I just want that my program or method should run at specific date and
There is a programming rule that says that a method should instead of asking
i don't understand this example from the doc : the timeIntervalSinceNow method should show

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.