Jump to content
  • Advertisement
Sign in to follow this  
Freaker13

operator overloading

This topic is 5454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
if you wanted to call it from a pointer, you'd dereference
eg.
A *p;
(*p)-> // this will call the overloaded operator
p-> // this will call the default (pointer) access operator

get it?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!