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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:39:21+00:00 2026-05-30T12:39:21+00:00

From http://www.learncpp.com/cpp-tutorial/97-overloading-the-increment-and-decrement-operators/ Class declaration class Digit { private: int m_nDigit; public: Digit(int nDigit=0) {

  • 0

From http://www.learncpp.com/cpp-tutorial/97-overloading-the-increment-and-decrement-operators/

Class declaration

class Digit
{
private:
    int m_nDigit;
public:
    Digit(int nDigit=0)
    {
        m_nDigit = nDigit;
    }

    Digit& operator++();
    Digit& operator--();

    int GetDigit() const { return m_nDigit; }
};

Their implementation of operator++

Digit& Digit::operator++()
{
    // If our number is already at 9, wrap around to 0
    if (m_nDigit == 9)
        m_nDigit = 0;
    // otherwise just increment to next number
    else
        ++m_nDigit;

    return *this;
}

My alternate implementation of operator++

Digit& Digit::operator++()
{
    return Digit(m_nDigit == 9 ? 0 : (m_nDigit + 1));
}

I wanted to know

  1. if there are any downsides of creating a new object like I have done, and
  2. about how to choose one of these implementations?
  • 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-05-30T12:39:22+00:00Added an answer on May 30, 2026 at 12:39 pm

    In your alternative implementation you have 2 issues:

    1. instead of m_nDigit = 9 do m_nDigit == 9. Currently m_nDigit will always be 9, and the return value will always be 0.

    2. you’re supposed to change the value of m_nDigit. When returning 0 – you don’t.

    The return statement is problematic because the operator is expected to change the value of the operand, not to create a new object.

    edit

    To clarify the problem, consider code:

    Digit x;
    x++;
    

    What would you expect x to be as the result of this code? I would expect it to be 1. Using your operator, it remains unchanged.

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

Sidebar

Related Questions

From http://www.learncpp.com/cpp-tutorial/142-function-template-instances/ class Cents { private: int m_nCents; public: Cents(int nCents) : m_nCents(nCents) {
Code example from http://www.learncpp.com/cpp-tutorial/812-static-member-functions/ : class Something { private: static int s_nValue; }; int
From http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/ , code such as class Base { public: virtual void function1() {};
From http://www.learncpp.com/cpp-tutorial/19-header-files/ How does the linker know that it needs to include the standard
//This program is taken from http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/ #include <iostream> using namespace std; class A {
At the end of the artile here: http://www.learncpp.com/cpp-tutorial/45-enumerated-types/ It mentions the following sentence: Finally,
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
From http://www.jibbering.com/faq/faq_notes/closures.html : Note: ECMAScript defines an internal [[prototype]] property of the internal Object
this is copied from http://www.zenspider.com/ZSS/Products/RubyInline/Readme.html , the home of rubyinline, adding/moding as indicated in
I used the code from http://www.rgagnon.com/javadetails/java-0580.html to get Motherboard Id, but the result is

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.