Game Development Community

Indexing vector value inside vector dreived class

by Frank Carney · in Torque Game Engine · 02/09/2007 (5:56 pm) · 1 replies

This has got to be a simple C++ issue, but how do you index a vector value inside vector dreived class:
this[index]

Does that work?

I need to compare a value in the vector for an insertion so I can keep my list in a sorted condition. I am feeding these values to my sorting compare function like this:
result = comparePos((void*) object,(void*) &this[index]);

Where "this" is a vectorPtr derived class. This is inside a class method.

About the author

I Started programming in HS and have never stopped. Now an 18 year vet of programming anything from assembler on a NES console to a nuclear waste processing system. If it can be programmed I may have tried to program it!


#1
02/09/2007 (7:05 pm)
Okay, "this" does not seem to work. However this does:
operator[](index)
ie
result = comparePos((void*) object,(void*) operator[](index));
Here is some code I tested under GCC (testVector.cc):
#include <stdio.h>
#include <vector>

class vclass : public std::vector<int>
{
	public:
	
	void print();
};

void vclass::print()
{
	int index;
	for(index = 0; index < size(); index++)
	{
		int val = operator[](index);
		printf("this[%d]: %d\n",index,val);
	}
}


int main(int argc, char **argv)
{
	// Create instance of vector based class.
	vclass tvclass;	

	tvclass.push_back(5);
	tvclass.push_back(3);
	tvclass.push_back(1);
	tvclass.push_back(-1);
	tvclass.print();	
}

Compile like this:
gcc testVector.cc -lstdc++ -o testVector

I guess you learn more about C++ everyday...