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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:14:33+00:00 2026-05-19T17:14:33+00:00

I am just learning C++, and I’ve come across the following conundrum: As a

  • 0

I am just learning C++, and I’ve come across the following conundrum:

As a C++ newbie, I’ve read that using reference instead of pointers (when possible) is generally a good idea, so I’m trying to get into the habit early. As a result, I have a lot of methods which have the general form of

void myMethod(ParamClass const& param);

Now, I’m wondering what is the best way to call these methods. Of course, each call will need a different object passed as a parameter, and as far as I know the only way to create it is the new operator, so now I’m doing the following:

myObject.myMethod(*new ParamClass(...));

While this method totally works, I’m wondering if there isn’t another already-established “c++ way” of doing this.

Thanks for the help!
Dan

  • 1 1 Answer
  • 3 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-19T17:14:33+00:00Added an answer on May 19, 2026 at 5:14 pm

    You should try not to use new, to begin with, as using it brings the trouble of memory management.

    For your example, just do the following:

    int main(int, char*[])
    {
      SomeObject myObject;
    
      // two phases
      ParamClass foo(...);
      myObject.myMethod(foo);
    
      // one phase
      myObject.myMethod(ParamClass(...));
    
      return 0;
    }
    

    I recommend the first method (in two times) because there are subtle gotchas with the second.

    EDIT: comments are not really appropriate to describe the gotchas I was referring to.

    As @Fred Nurk cited, the standard says a few things about the lifetime of temporaries:

    [class.temporary]

    (3) Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception. The value computations and side effects of destroying a temporary object are associated only with the full-expression, not with any specific subexpression.

    (5) The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference [note: except in a number of cases…]

    (5) [such as…] A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.

    This can lead to two subtle bugs, that most compilers do not catch:

    Type const& bound_bug()
    {
      Type const& t = Type(); // binds Type() to t, lifetime extended to that of t
      return t;
    } // t is destroyed, we've returned a reference to an object that does not exist
    
    Type const& forwarder(Type const& t) { return t; }
    
    void full_expression_bug()
    {
      T const& screwed = forwarder(T()); // T() lifetime ends with `;`
      screwed.method(); // we are using a reference to ????
    }
    

    Argyrios patched up Clang at my request so that it detects the first case (and a few more actually that I had not initially thought of). However the second can be very difficult to evaluate if the implementation of forwarder is not inline.

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

Sidebar

Related Questions

I'm just learning nHibernate and have come across what probably is a simple issue
I just learning C. Could someone explain that what are they using for ?
Just learning LINQ and i've come to a newbie roadblock in my test project.
I am just learning actionscript, so come across the problem In my application I
Just learning C++, encountered an issue that i'm not really sure why the following
Just learning how to work with php and mysql, using CodeIgniter. Running into that
I am just learning Go and wrote the following struct ( Image ) to
I am just learning how to use spl_autoload_register I have a folder structure that
I'm just learning codeigniter, and I seem to have run into something that is
Just learning about mercurial's --style and --template options that can be used on hg

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.