I am trying to initialize a vector inside a class's constructor, and I want the vector to be of type 'this'
std::vector<this> didnt work,
void* type = this;
std::vector<type> that didn't work either, is there any other way to do it?
I am trying to initialize a vector inside a class's constructor, and I want the vector to be of type 'this'
std::vector<this> didnt work,
void* type = this;
std::vector<type> that didn't work either, is there any other way to do it?
Short answer: this is not a type — it is an expression (a pointer to the current object). Templates need type names, so one cannot write std::vector<this>. For the goal described by (grouping instances by their actual class), a safe pattern is to register each instance in a central registry keyed by its dynamic type (or use a per-type static container via CRTP). The registry below shows a compact, practical approach: automatic registration in ctor/dtor, grouping by typeid, and a typed getter.
#include <unordered_map>
#include <typeindex>
#include <typeinfo>
#include <vector>
#include <algorithm>
#include <mutex>
class BaseClass {
public:
BaseClass() { registerInstance(this); }
virtual ~BaseClass() { unregisterInstance(this); } // makes BaseClass polymorphic
template<typename T>
static std::vector<T*> getInstancesOf() {
std::lock_guard<std::mutex> lk(mutex_);
auto it = registry_.find(std::type_index(typeid(T)));
if (it == registry_.end()) return {};
std::vector<T*> out; out.reserve(it->second.size());
for (BaseClass* p : it->second) out.push_back(static_cast<T*>(p));
return out;
}
private:
static void registerInstance(BaseClass* p) {
std::lock_guard<std::mutex> lk(mutex_);
registry_[std::type_index(typeid(*p))].push_back(p);
}
static void unregisterInstance(BaseClass* p) {
std::lock_guard<std::mutex> lk(mutex_);
auto &vec = registry_[std::type_index(typeid(*p))];
vec.erase(std::remove(vec.begin(), vec.end(), p), vec.end());
if (vec.empty()) registry_.erase(std::type_index(typeid(*p)));
}
static std::unordered_map<std::type_index, std::vector<BaseClass*>> registry_;
static std::mutex mutex_;
};
// definitions
std::unordered_map<std::type_index, std::vector<BaseClass*>> BaseClass::registry_;
std::mutex BaseClass::mutex_; Notes and cautions:
typeid(*p) returns the dynamic (derived) type only if BaseClass is polymorphic (hence the virtual dtor above). Registrable<Derived>. That is lighter and type-safe when the set of derived types is known.This builds on earlier suggestions: storing pointers (as and others advised) and grouping by type/name (as suggested) but gives an implementation that groups by runtime type, handles registration automatically, and exposes a typed accessor.
Jump to Post— ivailosp 11I am trying to initialize a vector inside a class's constructor, and I want the vector to be of type 'this'
std::vector<this>didnt work,
void* type = this; std::vector<type>that didn't work either, is there any other way to do it?
try
void* type = …
Jump to Post— Member #248612Do you mean something like this:
template <class T> struct Test { std::vector<Test<T> *> v; T t; }; int main() { Test<int> test; test.v.push_back(&test); }I don't know what your needs really are, but your class design looks sort of strange to me.
Jump to Post— mrinal.s2008 0I am trying to initialize a vector inside a class's constructor, and I want the vector to be of type 'this'
std::vector<this>didnt work,
void* type = this; std::vector<type>that didn't work either, is there any other way to do it?
In any of the member functions of …
I am trying to initialize a vector inside a class's constructor, and I want the vector to be of type 'this'
std::vector<this>didnt work,
void* type = this; std::vector<type>that didn't work either, is there any other way to do it?
try
void* type = (void*)this;
std::vector<void*>
Do you mean something like this:
template <class T>
struct Test
{
std::vector<Test<T> *> v;
T t;
};
int main()
{
Test<int> test;
test.v.push_back(&test);
} I don't know what your needs really are, but your class design looks sort of strange to me.
I am trying to initialize a vector inside a class's constructor, and I want the vector to be of type 'this'
std::vector<this>didnt work,
void* type = this; std::vector<type>that didn't work either, is there any other way to do it?
In any of the member functions of a class, max number of this pointers that can be available is 1. I wonder why would you want to create a vector of many this pointers!
I wonder why would you want to create a vector of many this pointers!
Maybe the vector is static and holds pointers to all instatiations of the class.
Maybe the vector is static and holds pointers to all instatiations of the class.
In such case, instead of calling it a vector of this pointers, better call it a vector of pointers to objects of a class, as you said
std::vector<Test<T> *> v; Your question is quite un-clear,
Is it that you wish to create a vector member to your class. Of the type in which it is declared,
So, I think that you are using a template class,
If that is the case
template <class T> class classname{
public:
std::vector<T> val_list;
};
classname <int> sky; Now I guess the variable sky would have the val_list of type int.
>I want the vector to be of type 'this'
What do you mean by 'this'?
Do you want a vector of pointer to the same class you are in.
Or Do you want a vector of the same class you are in.
If you wan't the former one, look at post#3
If you are interested in the latter, read the code below:
#include <iostream>
#include <vector>
template <typename T>
class MyClass
{ public:
T data;
MyClass():data(0){} //default constructor
MyClass(T t):data(t){} //one argument constructor
std::vector< MyClass<T> > myvec; //vector of type same as *this
};
int main()
{
MyClass<int> c;
MyClass<int> d(5);
c.myvec.push_back(d);
std::cout<<c.myvec[0].data;
} I have a BaseClass, which everything inherits in one way or another, and what I want to do, is whenever one of the classes that inherits this BaseClass calls it's constructor, it adds a std::vector<instanced class> into the list of classes that BaseClass has, and whenever a class tries to add an instance into this list, and that class already has an instance of itself in the list, add it to the sublist inside the class list that has the same type as itself.
I think this would be better expressed into code:
std::vector< std::vector<void*> > classInstances;
BaseClass(std::string className)
{
if(already has class with that className)
find the vector that has the class in it, and
add the newly instanced class to that vector;
else
classes.push_back(std::vector<class being instanced>)
}
void* getClassHandle(std::string className)
{
this is for getting the class that has the classname that
is equal to the className paramater; no idea how to do this either
} All and any help will and would be appreciated
Thanks in advance
What about:
std::map<std::string, std::vector<std::pair<std:string, BaseClass *> > > Key for the map is the class name. This classname references a vector. The vector (Value) holds pairs of instance names and the corresponding pointers to the class instances. The class name (Key) can be obtained using typeid(). RTTI has to be activated in your compiler options for that, iirc.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.