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;
operator overloading
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)
thx in advance
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
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
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?
a->b->c? or would you need to change the way you set it up competely?
if you wanted to call it from a pointer, you'd dereference
eg.
get it?
eg.
A *p;(*p)-> // this will call the overloaded operatorp-> // this will call the default (pointer) access operator
get it?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement