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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:51:17+00:00 2026-06-03T05:51:17+00:00

In Java, where all classes are really references, I do this: Car getCar(int mileage)

  • 0

In Java, where all classes are really references, I do this:

Car getCar(int mileage)
{
  Car car = new Car();

  car.setMileage(mileage);
  return car;
}

In C++, how do I do this? I can put it into a reference:

void getCar(int mileage, Car& car)
{
  car.setMileage(mileage);
  return true;
}

Or, I can create a new object:

Car* getCar(int mileage)
{
  Car* car = new Car;

  car.setMileage(mileage);
  return car;
}

But then, the caller is also responsible for deleting car.

I don’t want to return a pointer. I want to return a Car:

Car getCar(int mileage)
{
  Car car;

  car.setMileage(mileage);
  return car;
}

But of course, car is a local variable which will be deleted once the function finishes.

What’s generally the ‘standard’ way of doing this? Which is the best way, and why?

  • 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-03T05:51:19+00:00Added an answer on June 3, 2026 at 5:51 am

    Your final example is the correct and idiomatic way to return an object:

    Car getCar(int mileage)
    {
      Car car;
    
      car.setMileage(mileage);
      return car;
    }
    

    Yes, car will be deleted at the end of the function, but not before it is copied into the returned object. You might invoke this function like so:

    {
        Car myCar;
        myCar = getCar(42);
    }
    

    The car that is local to getCar is copied into the calling environment’s myCar.


    What you can’t and must not do is to return a reference to or a pointer to a local variable.
    This is WRONG:

    Car& getCar(int mileage)
    {
      Car car;
      return car;
    }
    

    This is also WRONG:

    Car* getCar(int mileage)
    {
      Car car;
      return &car;
    }
    

    In each of these cases, you are allowing the calling function access to an object which no longer exists.

    You mustn’t return a pointer or reference to a local. You may return a copy of a local.

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

Sidebar

Related Questions

I'm a java developer new to python. In java, you can access all classes
I use Java in this question but this really applies to all modern app
In Java there are methods to go about scanning the classpath/all classes/within a package
In Java, all classes are extending Object class implicitly , on top of that
Are all final classes in Java immutable. String and Integer both are final classes
Using reflection on Java classes to access all field, methods, and so on: Is
I know that you can run almost all Java in Dalvik's VM that you
So I have all these classes put together for all the connectivity between Predators
Is there a good tool that can help to reverse engineer Java classes to
I'm working on a little Java game in which all sorts of events can

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.