Skip to main content
gamedev.net
🔒 Locked

C++ operator[] question

Started by codder88 Sep 4, 2010 at 4:08 PM 7 replies 1.2k views
Original Post
codder88
codder88
I have

template <typename T>class myClass{public:	T& operator[](int x)	{		return myVector[x]; // here give me error	}private:	std::vector<T*> myVector;};


I Use:
myClass <int> test;test[0] = 3;


When i compile the compiler give me the error C2440 because it can't convert from int* to int&
nobodynews
nobodynews
Your std::vector is storing T*. Just store T. That will solve your immediate issue.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
Zahlman
Zahlman
Quote:
Original post by codder88
When i compile the compiler give me the error C2440 because it can't convert from int* to int&


Yes, that's exactly why it gives you that error.

To fix this, you need to think more about what you are doing and why you are doing it.

You said that the return type is T&. You are trying to return an element of the vector. The vector stores T*. So the error should be no surprise: you are specifying a T* where a T& is needed.

Why do you want to put T* in the vector? Why do you want to return T&? Why do you want to do any of this at all, instead of just using a vector directly?
codder88
codder88
I want to store for example some directx pointers
and access to it

for e.g

myClass textures;

// and access to a single texture
textures[0]......

I don't want to use boost... i want to use my class

EDIT: if i convert T* to T i can store pointers?
taz0010
taz0010
You have several choices here:

a) Return T*& instead of T*. Also return T* const so the operator is usable in const situations. Since there's no reference, you're returning a copy of the pointer, which is easier than wrapping your mind around T* const & const. But it's much simpler to:

b) Have your class use T, not T*. If they want to store pointers in your container then they can instantiate with a pointer - i.e. myClass c;
Your example of "myClass textures;" won't work properly when your class uses std::vector myVector, as it results in std::vector instead.

c) Consider storing IUnknown* instead. I'm not sure what you're trying to do exactly, or even if that's useful, but it's possible as all DirecyX objects inherit from it.
rip-off
rip-off
Quote:

I want to store for example some directx pointers

Then use a container. The standard C++ library provides several. Writing a wrapper container around them is unnecessary.

Instead of using "myClass", use vector directly. Classes should have well defined responsibilities, your class (as posted) does not appear to have any purpose.
iMalc
iMalc
Quote:
Original post by codder88
I want to store for example some directx pointers
and access to it

for e.g

myClass textures;
What about ATL? For the above I would store them in a CComPtr inside the vector.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.