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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:07:06+00:00 2026-06-06T21:07:06+00:00

I’m working on a project that delivers statistics to the user. I created a

  • 0

I’m working on a project that delivers statistics to the user. I created a class called Dog,
And it has several functions. Speak, woof, run, fetch, etc.

I want to have a function that spits out how many times each function has been called. I’m also interested in the constructor calls and destructor calls as well.

I have a header file which defines all the functions, then a separate .cc file that implements them. My question is, is there a way to keep track of how many times each function is called?

I have a function called print that will fetch the “statistics” and then output them to standard output. I was considering using static integers as part of the class itself, declaring several integers to keep track of those things. I know the compiler will create a copy of the integer and initialize it to a minimum value, and then I’ll increment the integers in the .cc functions.

I also thought about having static integers as a global variable in the .cc. Which way is easier? Or is there a better way to do this?

Any help is greatly appreciated!

  • 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-06T21:07:06+00:00Added an answer on June 6, 2026 at 9:07 pm

    Using static member variables is the way to go. However, the compiler will not “create a copy of the integer and initialize it to a minimum value”; you’ll have to provide a definition for each one in the .cc file and initialize it to 0 there. (Things are a bit different if you’re using C++11, but the basic idea is the same.)

    There’s no reason to use static global variables instead of static members.

    foo.h:

    class Foo {
      static int countCtor_;
      static int countDtor_;
      static int countprint_:
      Foo();
      ~Foo();
      static void print();
    };
    

    foo.cc:

    #include <iostream>
    #include "foo.h"
    
    int Foo::countCtor_ = 0;
    int Foo::countDtor_ = 0;
    int Foo::countprint_ = 0;
    
    Foo::Foo() {
      ++countCtor_;
      // Something here
    }
    Foo::~Foo() {
      ++countDtor_;
      // Something here
    }
    void Foo::print() {
      ++countprint_;
      std::cout << "Ctor:  " << countCtor_ << "\n"
                << "Dtor:  " << countDtor_ << "\n"
                << "print: " << countprint_ << "\n";
    }
    

    But if you’ve got a lot of functions, the repetition involved is a bit annoying—it’s very easy to accidentally do ++countBar_ when you meant ++countBaz_ (especially if you copy and paste the boilerplate), so you may want something a bit fancier, such as a static map and a macro that increments counts[__FUNC__], so you can just use the exact same line in each function. Like this:

    foo.h:

    #include <map>
    class Foo {
      static std::map<const char*, int> counts_;
      Foo();
      ~Foo();
      void print();
    };
    

    foo.cc:

    #include <iostream>
    #include "foo.h"
    
    std::map<const char *, int> Foo::counts_;
    
    #define INC_COUNT_() do { ++counts_[__FUNC__]; } while (0)
    
    Foo::Foo() {
      INC_COUNT_();
      // Something here
    }
    Foo::~Foo() {
      INC_COUNT_();
      // Something here
    }
    void Foo::print() {
      INC_COUNT_();
      for (std::map<const char *, int>::const_iterator it = counts_.begin(); 
           it != counts_.end(); ++it) {
        std::cout << it->first << ": " << it->second << "\n";
      }
    }
    

    In the example code above, __FUNC__ is a placeholder. Unfortunately, there is no standard-compliant value you can use in its place. Most compilers have some subset of __func__, __FUNC__, __FUNCTION__, __FUNCSIG__, and __PRETTY_FUNCTION__. However, none of those are standard in C++03. C++11 does standardize __func__, but only as an “implementation-defined string”, which isn’t guaranteed to be useful, or even unique. On top of that, the values will be different on different compilers. Also, some of them may be macros rather than identifiers, to make things more fun.

    If you want truly portable code, in C++11, you can use something like string(__func__) + “:” + STRINGIZE(__LINE__)—this will be somewhat ugly, but at least each function will have a unique name. And in C++03, there is no equivalent. If you just need “portable enough”, consult the documentation for every compiler you use, or rely on something like autoconf.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into

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.