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?

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.

This article has been dead for over six months. Start a new discussion instead.