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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:14:16+00:00 2026-05-15T01:14:16+00:00

I have a piece of code like this class Base { public: Base(bool _active)

  • 0

I have a piece of code like this

class Base
{
public:
Base(bool _active)
{
 active = _active;
}
void Configure();
void Set Active(bool _active);
private: 
bool active;
};
class Derived1 : public Base
{
public:
Derived1(bool active):Base(active){}

};

similarly Derived 2 and Derived 3

Now if i call derived1Object.Configure, i need to check how many of the derived1Obj, derived2Obj,derived3Obj is active. Should i add this in the “Base” class like a function say, GetNumberOfActive()?

And If the implementation is like this:

class Imp
{
public:
 void Configure()
 {
  //Code instantiating a particular Derived1/2/3 Object 
  int GetNumberOfActiveDerivedObj();
  baseRef.Configure(int numberOfActiveDerivedClasses);
 }
prive:
Derived1 dObj1(true);
Derived2 dObj2(false);
Derived3 dObj3(true);
};

should i calculate the numberOfActive Derived Objects in Imp Class?

THanks

  • 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-15T01:14:17+00:00Added an answer on May 15, 2026 at 1:14 am

    You could use CRTP in conjunction with a static counter variable: Wikipedia Link

    edit: some code

    #include <iostream>
    
    template <typename T> struct counter {
        counter() { ++objects_alive; }
        virtual ~counter() { --objects_alive; }
        static int objects_alive;
    };
    template <typename T> int counter<T>::objects_alive( 0 );
    
    
    class Base {
    public:
        void Configure();
        //more stuff
    };
    
    class Derived1 : public Base, counter<Derived1> {
    public:
        void Configure() {
            std::cout << "num Derived1 objects: "<< counter<Derived1>::objects_alive << std::endl;
        }
    };
    
    class Derived2 : public Base, counter<Derived2> {
    public:
        void Configure() {
            std::cout << "num Derived2 objects: " << counter<Derived2>::objects_alive << std::endl;
        }
    };
    
    int main (int argc, char* argv[]) {
    
        Derived1 d10;
        d10.Configure();
        {
            Derived1 d11;
            d11.Configure();
            Derived2 d20;
            d20.Configure();
    
        }
        Derived1 d12;
        d12.Configure();
        Derived2 d21;
        d21.Configure();
    
    
        return 0;
    }
    

    Output:

    $ g++-4 -pedantic crtp1.cpp -o crtp1 && ./crtp1
    num Derived1 objects: 1
    num Derived1 objects: 2
    num Derived2 objects: 1
    num Derived1 objects: 2
    num Derived2 objects: 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 412k
  • Answers 412k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer your language to another language is never extremely hard to… May 15, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer List<double> productQty = //Filled with your quantity values int i… May 15, 2026 at 8:14 am
  • Editorial Team
    Editorial Team added an answer Try putting this function in your Form class: protected override… May 15, 2026 at 8:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.