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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:32:35+00:00 2026-06-13T15:32:35+00:00

Assume there is a need to implement getters/setters (also I try to avoid them

  • 0

Assume there is a need to implement getters/setters (also I try to avoid them whenever possible because they often indicate bad design). Which variant of the following two is better and what are the implications of each?

Possibility (1):

class Foo;
class Bar
{
public:
    const Foo& getFoo() const {return foo_;}
private:
    Foo foo_;
};

or (2):

class Foo;
class Bar
{
public:
    void getFoo(Foo& foo) const {foo = foo_}
private:
    Foo foo_;
};

Personally, I use always the first variant, because I find it more intuitive. In the second variant you have to construct the Foo object, which will hold the result of getFoo, before calling getFoo and then pass getFoo a reference to this object. This variant feels counterintuitive to me, but there are people, who prefer the secodn variant. For what reason might be the second variant superior to the first one?

  • 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-13T15:32:36+00:00Added an answer on June 13, 2026 at 3:32 pm

    Output arguments should be considered criminal offence. It’s more difficult to use, but especially much more difficult to maintain, because it’s just not visible in the code. If something is assigned to, it’s clear. If a method is invoked on object, it’s kind of expected it might modify it. But if it’s only passed to a function, normal maintenance programmer won’t suspect it’s being modified. Plus most of the time it requires extra line for declaring a temporary variable to accept the value.

    This kind of thing is mainly written by people who stopped at C89 and somehow learned C++ syntax. But with C++ copy elision and move semantics of C++11 I don’t see any valid reason worth making the code more difficult to read.

    Of course if you are returning a member, you can return const reference to it. If the value is computed, just return by value and copy elision will take care of it most of the time.

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

Sidebar

Related Questions

Assume we have a popular site. We need to implement mail-like messaging between users.
assume there is a grid with some points in it just like in the
Assume there are two separate web applications: a sports site with admin backend an
Assume there is a TFS project Project with the subfolders trunk and 1.0 .
Let's assume there is a static .Net method static void RegisterDelegate(Action<array<Object^>^>^ handler); defined in
I think Im tying myself up in knots with this and I assume there's
I have container of geometrical entities. Let's assume there is circle, ellipse, line, arc.
Assume that there's no problem with my header file and some included library. I'm
We all know that most applications out there assume class names to follow the
Let's assume at us there is a certain page on a site and when

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.