operator overloading

Started by
6 comments, last by silvermace 19 years, 6 months ago
Hi all, I've oveloaded the -> operator in my VTcMemSlot class but when i do slot->getPtr the overloaded operator isn't called.(slot is of type VTcRAMSlot which inherit from VTcMemSlot)

class VTcMemSlot
{
	friend class VTcCacheManager;
	friend class VTcCacheSlotOwner;
	
public:
	VTcMemSlot(VTcCacheManager*	manager, unsigned int bytes);
	~VTcMemSlot();

	inline VTcMemSlot* VTcMemSlot::operator ->()
	{
		manager->logger->wrStr(1,"operator->");
		if(prev == NULL)
			manager->cacheListsLRU[indexInCacheList] = next;//zet de tweede op de eerste plaats
		else
		{
			//link prev and next
			prev->next = next;
			next->prev = prev;
		}
		//move slot to end of linked  list
		manager->lastUsedSlots[indexInCacheList]->next = this;
		prev = manager->lastUsedSlots[indexInCacheList];
		next = NULL;
		manager->lastUsedSlots[indexInCacheList] = this;
		return this;
	}
	
	void	splitSlot		();
	void	setSizeInBytes	(unsigned int bytes){this->bytes = bytes;}

	virtual void*	getPtr() = 0;
	

thx in advance
Advertisement
It would be helpful to see the code in which you call the overloaded operator. That aside, I'll explain what I think the problem might be.

Overloaded operators only work when you call them on the type they are defined for. For instance, if you define:

A::operator->()

then this won't call your operator:

A *a;
a->whatever

because a is a pointer, and not an object of the class A. In order for it to work, you need to call it like this:

A a;
a->whatever

I don't know which one you meant to call in VTcMemSlot::getSizeInBytes, but since this is a pointer, you're not going to be able to call the overloaded -> operator on it unless you dereference it first.

There. Clear as mud?

-RC
thx that's the solution. Didn't know you could do -> on an obj.
You can, so long as you overload it ;).

Tis' the beauty of overloading.
-------------------------Rayoom Sledge Hammer Productions - Programmer
just to examine this further what if you really did need to call it from a pointer would you do something like this?

a->b->c? or would you need to change the way you set it up competely?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
if you wanted to call it from a pointer, you'd dereference
eg.
A *p;(*p)-> // this will call the overloaded operatorp-> // this will call the default (pointer) access operator

get it?
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
i dont get whats so great about operator overloading. Wouldnt it be just as easy to write a funtion or macro that has 2 parameters? That way people looking at your code would realize whats happening instead of being mystified.
Quote:Original post by _Phalanx_
i dont get whats so great about operator overloading. Wouldnt it be just as easy to write a funtion or macro that has 2 parameters? That way people looking at your code would realize whats happening instead of being mystified.

one way to look at it, its just a convenience feature really :)
in most situations using an operator can make things clearer

a.setValue( b.objPtr->next.getValue() + c.getValue() )

could become
a = b->next+ c

isnt that a wee bit easier to read?

PS. thats BS code, but get the picture?
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement