answersLogoWhite

0

How would you find out the no of instance of a class?

Updated: 8/17/2019
User Avatar

Wiki User

9y ago

Best Answer

Use a static variable in the class, initialised with the value zero. Each time any constructor is invoked, increment the variable and assign it to an instance member variable. The only exception is the move constructor which should take on the identity of its argument.

class foo

{

private:

static size_t counter;

size_t identity;

public:

foo (): identity (++counter) {} // default constructor (new identity)

foo (const foo& rhs): identity (++counter) {} // copy constructor (new identity)

foo (foo&& rhs): identity (std::move(rhs.identity)) {} // move constructor (take identity from original)

foo& operator= (const foo& rhs) {} // copy assign (retain current identity)

foo& operator= (foo&& rhs) {} // move assign (retain current identity)

~foo() {}

const size_t get_identity() const { return identity; } // accessor to identity

};

// all static variables must be initialised outside of the class in which they are declared

size_t foo::counter = 0;

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How would you find out the no of instance of a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How would you find out the number of instance of a class?

That cannot be answered without more detail. What kind of class are you talking about? Where do these instances occur? Generally speaking, you find out the number of something by counting it.


WHAT IS A specific instance of a class is called?

Specific instance of a class is called object of that class.


What do you mean by Get instance?

It means create an object for a class. Instance refers to the obj of a class.


What do you mean by instance of the class?

I have no idea. Was going to ask you the same question...An instance method represent the behavior of an object


What are instance variables?

These are normal variables declared within a class that are attached to an object instance of a class.


What is the difference between classes and object in C?

An object is an INSTANCE of a class. Human is a class, while YOU are a person, an instance of Human class, but YOU do not represent the entire human class. Or, a class provides the abstraction, and an object is a typical example of that abstraction. Classes provide a blue print to build a real instance of an object.


What is difference between an instance variable and a class variable?

An instance variable is typically associated with an object instance of the class whereas class variable is not associated with any object instance. Static variables are referred to as class variables while non-static regular variables are called instance variables. Simply put, you will have as many instances of the instance variable as there are object instances. i.e., if there are 10 instances of an object, you will have 10 instances of that instance variable as well. But, there will be only one instance of the static or class variable. Instance variables are accessed as follows: objname.variableName; Class variables are accessed as follows: ClassName.variableName;


What is class and objects?

class is template of object and object is instance of a class


What is the difference between a class method and an instance method?

Instance methods can be called by the object of a Class whereas static method are called by the Class. When objects of a Class are created, they have their own copy of instance methods and variables, stored in different memory locations. Static Methods and variables are shared among all the objects of the Class, stored in one fixed location in memory.Static methods cannotaccess instance variables or instance methods directly-they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.


Would you find more organisms in a class or a family?

class


What is difference between instance variable and class variable in java?

The main difference between the class variable and Instance variable is, first time, when class is loaded in to memory, then only memory is allocated for all class variables. Usually static variables are called class variables. These variables are available throughout the execution of the application and the values are common to the class. You can access them directly without creating an object of the class. Instance variables are normal variables declared in a class, that would get initialized when you create an instance of the class. Every instance of the class would have a copy of the variable and you need a class instance (object) to access these variables


What is current instance in java?

Instance refers to one item of a particular type. for ex: you are one instance of a human, similarly I am one instance of human. An instance in object oriented terms refers to one item of a particular object type.