vector that holds two items

Need to make a vector that stores two object, so that each index of the vector has 1. a generic object
2. a float object.

What's the best way to do this? I don't want to have an inner class with getters and setters, are there any other possibilities?
¿eh?
std::pair< generic_object, float_object >
Without using the STL...
How about using a struct? (I'm not really sure if typename can be used to define though).
1
2
3
4
5
struct pair
{
    typename generic_object;
    float float_object;
};



EDIT: okay that didn't work. But how about this? It'll store your generic object as a void pointer. Then return the address to it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class pair
{
	void* generic_object;
	int generic_size;
	float float_object;
public: 
	// Set functions
	void setFloat(float _f) { float_object = _f; }
	template <class T> void setGeneric(const T& t) { generic_object = &t; }
	
	// Get functions
	float getFloat() { return float_object; }
	void* getGeneric()	{ return generic_object;	}
};

The trick is that I'm not sure how to keep the memory reserved. If the original object is destroyed then I think this pointer just points to an empty place in memory.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
template < typename T > struct my_pair
{
    T generic_object ;
    float float_object ;

    // constructors etc
};

// ....

std::vector< my_pair<std::string> > my_vector ;

Okay that works better.
aka reinventing the wheel
Is there any way to do this using pointers, avoiding an inner class? [see OP]
Make two vectors
Topic archived. No new replies allowed.