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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:14:30+00:00 2026-06-11T01:14:30+00:00

Consider the following example : class MyClass { // Getters by copy ? public:

  • 0

Consider the following example :

class MyClass
{
    // Getters by copy ?
    public:
        inline std::string fullName() const {return _firstName+_lastName;}

    // Getters by reference ?
    public:
        inline const std::string& firstName() const {return _firstName;}
        inline const std::string& lastName() const {return _lastName;}

    // Data members
    protected:
        std::string _firstName;
        std:::string _lastName;
}

In the documentation of my code, I would like to distinguish between getters by reference (direct access of the class data members) and getters by copy (access to data constructed on class data members). What words can I use to name these two different categories ?

  • 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-11T01:14:32+00:00Added an answer on June 11, 2026 at 1:14 am

    The first question that comes to mind is why you want to distinguish the operations in the name. Specially since that separation implies that you are leaking details of your implementation to the users and losing encapsulation.

    Consider for example that during profiling of the real application you find out that 90% of the time callers use fullName() and don’t make copies, then you might want to modify your type to cache the result and avoid the cost:

    class MyClass {
       // ...
       std::string _fullName;
    public:
       const std::string& fullName() const {
          return _fullName;
       }
       // One of the options for caching: update on set
       // Could use other, like update on first request...
       void setName( std::string firstName ) {
          _firstName = std::move(firstName);
          _fullName = _firstName + _lastName;
       }
    };
    

    If your naming convention differentiates both types of functions, then you would either have to seek all uses of the function in your project and replace them for a change in the implementation, or else your function would be lying as the naming implies a copy, but the implementation does not.

    That being said, I have seen this done in the past, where accessor that just return a reference to the member were named either by the nember or getMember, and operations that create new objects were named as a sentence with a verb that implied the construction: composeFullName to imply that there is a cost associated with calling the operation. This change in the naming conventions, where I have seen it, was specific to operations where the cost of executing it was high.

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

Sidebar

Related Questions

Consider the following example: class MyContainer { std::vector<void *> v; public: void Put(void *x)
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following example: public class BaseClass { public string StringInBaseClass {get;set;} public int
Consider the following example: public interface ITask { void Execute(); } public class LoggingTaskRunner
Consider the following class in a file MyClass.cs using System; public class MyClass :
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following example: interface IPropertyCollection { public MethodWrapper GetPropertySetterByName(string name); //<<-- I want
Consider the following example: Public Class ParentClass Public Sub GenerateReport Dim Col As Collection
Consider the following: class Example : boost::noncopyable { HANDLE hExample; public: Example() { hExample
To illustrate my question consider the following example: @Entity public class Box implements Serializable

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.