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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:32:01+00:00 2026-06-05T06:32:01+00:00

For example in the code below : class HowMany { static int objectCount; public:

  • 0

For example in the code below :

class HowMany {
    static int objectCount;
    public:
        HowMany() { 
            objectCount++; 
        }
        static void print(const string& msg = "") {
             if(msg.size() != 0) 
                 cout << msg << ": ";

             cout << "objectCount = " << objectCount << endl;
        }
        ~HowMany() {
            objectCount--;
            print("~HowMany()");
        }
};

int HowMany::objectCount = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
    x.print("x argument inside f()");
    return x;
}

int main() {
    HowMany h;
    HowMany::print("after construction of h");
    HowMany h2 = f(h);
    HowMany::print("after call to f()");
}

Why does the compiler doesn’t create the copy-constructor automatically for the class HowMany, and bit-wise copy takes place when the call to f(h) takes place ?

In what cases the compiler creates the default copy-constructor and in what cases it doesn’t create?

It gives output as:

after construction of h: objectCount = 1

x argument inside f(): objectCount = 1

~HowMany(): objectCount = 0

after call to f(): objectCount = 0

~HowMany(): objectCount = -1

~HowMany(): objectCount = -2

Many many thanks in advance

  • 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-05T06:32:10+00:00Added an answer on June 5, 2026 at 6:32 am

    In C++98 and C++03 the compiler always creates a copy constructor, that performs a memberwise copy of your fields, unless you specify explicitly that you wrote your own1.

    This is what happens in your code: the compiler-generated copy constructor doesn’t do anything particular – in particular, it doesn’t increment objectCount – so you end up with a negative object count (all the copied objects didn’t increment the counter, but they did decrement it).

    To obtain the result you expected, you would have to write something like:

    HowMany(const HowMany &) { 
            objectCount++; 
    }
    

    1. the default copy constructor is not created even if you write the copy constructor prototype but don’t implement it, and/or mark it as private – actually, that’s how you create a noncopyable class. C++11 also supports a special syntax to tell the compiler not to generate any copy constructor.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see the example code below: class A { private: class B { public:
Below is an example class hierarchy and code. What I'm looking for is a
My understanding is that literals are automatically interend. Let's take below code example. string
I've got a Model in Django, example code below (not my actual code): class
Please review the example code below, I have a class file that is loaded
i saw a example for login form same blow code class Form_Login extends Zend_Form
The example code below works as as a server process. But when I add
I know how to do this... I'll give example code below. But I can't
UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
Given the example source code below, is it possible for someone to see the

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.