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

  • Home
  • SEARCH
  • 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 8198789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:01:39+00:00 2026-06-07T06:01:39+00:00

In Effective C++ (Item 18: Make interfaces easy to use correctly and hard to

  • 0

In Effective C++ (Item 18: Make interfaces easy to use correctly and hard to use incorrectly), I saw a code sample similar to the following:

class Month
{
public:
    static Month Jan()
    {
        return Month(1);
    }

    static Month Feb()
    {
        return Month(2);
    }

    //...

    static Month Dec()
    {
        return Month(12);
    }

private:
    explicit Month(int nMonth)
        : m_nMonth(nMonth)
    {
    }

private:
    int m_nMonth;
};

Date date(Month::Mar(), Day(30), Year(1995));

Are there any drawbacks of changing the functions so that they return static const reference to Month?

class Month
{
public:
    static const Month& Jan()
    {
        static Month month(1);
        return month;
    }

    static const Month& Feb()
    {
        static Month month(2);
        return month;
    }

    //...

    static const Month& Dec()
    {
        static Month month(12);
        return month;
    }

private:
    explicit Month(int nMonth)
        : m_nMonth(nMonth)
    {
    }

private:
    int m_nMonth;
};

I thought the second version is a bit more efficient than 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-07T06:01:42+00:00Added an answer on June 7, 2026 at 6:01 am

    Reason 1: It’s not better.

    Returning by value incurs the cost of copying the entire object.

    Returning by reference incurs the cost of copying what’s effectively a pointer, plus the cost of dereferencing that pointer.

    Since Month is the size of an int:

    • Copying a reference is no faster than copying a Month
    • Dereferencing would be incurred every time that reference is accessed.

    So in general, returning by const reference is an optimization chosen to prevent what would be a costly copy.

    Reason 2: static makes it worse

    Unlike C, C++ promises that a static variable in a function will be constructed at the time of the function’s first call.

    In practice this means that every call to the function must begin with some unseen logic to determine if it’s the first call.

    See also Vaughn Cato’s answer

    See also ildjam’s comment

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

Sidebar

Related Questions

In Scott Meyers's Effective C++ , item 18 Make interfaces easy to use correctly
From Effective Java Second Edition, Item 28 : Do not use wildcard types as
While going through Effective C++ (by Scott Meyers), I came across the following code
Item 16 of Effective Java 2nd edition, favor composition over inheritance says the following
Effective Java: Item 6: Eliminate obsolete object references. Generally speaking, whenever a class manages
I am reading Effective Java by Joshua Bloch, item 39 make defensive copy, and
Consider the following from Effective Java Item 11 (Override clone judiciously) where Josh Bloch
I am reading Bill Wagner's book Effective C# . In Item 32 he is
In Effective Java , Joshua Bloch favors interfaces over abstract classes. However, he notes
Which method is very effective and simple to code to attain this functionality ?

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.