1

I want to call all object exist in a project. how can i do that?

Class1 Abc;
Class1 Xyz;
Class1 asd[100];
for each (Class1 k in ???)
{
    k.dosomething();
}

4 Answers4

2

C++ has no notion of "project", so there's no formally correct answer as to how to call a method on all objects of a given class in a project.

Possibly you mean in a "program".

One way is to register a pointer to each object in a global set of such object pointers. A main problem with that is that ideally it's the constructor that has to do the registering job, and the constructor doesn't know if this object will end up as const. And so you can inadvertently end up calling a modifier method on a const object.

One (limited) solution is to have do_something as const method, when that's practical.

Another (general but more complex) solution is to restrict creation of objects of that class to factory functions, because a factory function can register an object after it's turned const after successful construction.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

C++ doesn't have type introspection, so it's impossible.

You can solve it by storing all object of Class1 in a container, and then iterate over that. For example, you could have a static member variable, a vector, and in the constructor you simply add this to the vector. It only works for that single class though, and there's no good or standard way to generalize it.

There are libraries that can add simple type introspection functionality, but it always requires you to add extra code to your classes, and usage of macros.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

C++ language does not support this but you can implement it as an object pool pattern. This pattern is especially useful for efficient memory management and economical usage of memory and other resources in tightly constrained environments, where even small performance gains are of vital importance.

fatihk
  • 7,789
  • 1
  • 26
  • 48
0

You can use Curiously recurring template patterns to inherit from a class which updates a container including all the classes for you.

#include <iostream>
#include <unordered_set>

template <typename T>
class IterableType{
public:
  std::unordered_set<T const*>& allClasses(){
    return classes_;
  }

protected:
  IterableType(){
  IterableType<T>::classes_.insert(static_cast<T*>(this));
}

IterableType(const IterableType<T>& t){
  IterableType<T>::classes_.insert(static_cast<T*>(this));    
}

IterableType(const IterableType<T>&& t){
  IterableType<T>::classes_.insert(static_cast<T*>(this));        
}

IterableType<T>& operator=(const IterableType<T>& t){
  return *this;
}

IterableType<T>& operator=(const IterableType<T>&& t){
  return *this;
}  


~IterableType(){
    IterableType<T>::classes_.erase(static_cast<T*>(this));
}

private:
  static std::unordered_set<T const*> classes_;
};

template <typename T>
std::unordered_set<T const*> IterableType<T>::classes_;

The constructor and destructor are protected as they are only supposed to be called from the derived classes. As the set is declared with const members it will never allow to modify const classes. However, if you want to modify them in them while iterating you need to use const_cast or remove the const from declaration. But as said these would allow the modification of const objects.

To use this you just inherit from this class using the child class as a template argument.

class Class1:public IterableType<Class1>{
public:
  Class1(int n):n_(n)
  {}

  void print() const{
    std::cout << n_ << std::endl;
  }

private:    
  int n_;
};

int main(){
  Class1 a(3),b(7),c(8);
  for (auto i:a.allClasses()){
    i->print();
  }
  return 0;
}

Edit: Implemented copy and move constructors and made the set const

Ari Hietanen
  • 1,749
  • 13
  • 15