C++ Pointers Get Smart: Listing 1

Definition of my_class

#include <iostream>

using namespace std;

class my_class
{
private:
    // like in Java and C#, the following static member is
    // shared by all my_class instances
    static unsigned instance_counter_;
    // about this other, instead, each instance has its own
    // like in managed languages
    unsigned instance_id_;

public:
    // Constructor.
    my_class()
    {
        cout << "Instance " << (instance_id_ = ++instance_counter_) 
        << " being created." << endl;
    }

    // Destructor.
    ~my_class()
    {
        cout << "Instance " << instance_id_ << " being destroyed." << endl;
    }

    unsigned get_id() { return instance_id_; }
};

// class members are initialized outside the class declaration in C++
unsigned my_class::instance_counter_ = 0;

About the Author

Diego Dagum is a software architect and developer with more than 20 years of experience. He can be reached at [email protected]

comments powered by Disqus

Featured

  • Top Free Extensions for Visual Studio 2022

    With Visual Studio 2022 having debuted just one month ago, there are now dozens of new extensions available specifically targeting the revolutionary 64-bit IDE.

  • Green Motherboard Closeup Graphic

    How to Compute Transformer Architecture Model Accuracy

    Dr. James McCaffrey of Microsoft Research uses the Hugging Face library to simplify the implementation of NLP systems using Transformer Architecture (TA) models.

  • VS Code Snippets Leads Recent Open Source GA Announcements

    GitHub's "Release Radar" for November shows a Visual Studio Code snippets project leading a raft of open source offerings that reached general availability in November.

  • C# Language Server Revamps VS 2022 Razor Editor for ASP.NET Core

    Microsoft is touting new capabilities in ASP.NET Core web development in Visual Studio 2022 thanks to a new Razor editor powered by a Razor Language Server.

Upcoming Events