*argh* me, and my [] overloading problem on pointers

Started by
19 comments, last by DJSnow 20 years, 2 months ago
hi. great, perhaps i''m to dumb to figure this out, or... don''t what''s wrong today: i have overloaded the "[]" operator exactly, for my buffer class. it works, if i do:

BufferClass MyBuffer;
MyBuffer.Create( 100 ); // create with 100 elements
MyBuffer[10] = 10;
MyBuffer[99] = 5;
// and so on
 
BUT, if i do:

BufferClass *MyBuffer; //pointer now !!
MyBuffer = new BufferClass( 100 ); // elements in constructor this time !
MyBuffer[10] = 10;
MyBuffer[99] = 10;
 
as you can guess, the second way with the pointer don''t work - because, the compiler sees this as a pointer to, and if i do MyBuffer[10] it looks up ten pointer-sizes away and is doing there the insert - protection fault !!! how the f****** hell do i have to overload my operators that this works ??? DJSnow --- this post is manually created and therefore legally valid without a signature
DJSnow---this post is manually created and therefore legally valid without a signature
Advertisement
You need to dereference your pointer... something like this:

(*MyBuffer)[10] = 10;
(*MyBuffer)[99] = 10;


or

BufferClass& bc = *MyBuffer;
bc[10] = 10;
bc[99] = 10;

no - that''s impossible !
this is goofy, looks ugly and is totally inapropriate.

there must be another way to do this !?

(except the "SetValueAt( index, value )" function!)

any other hints ??


DJSnow
---
this post is manually created and therefore legally valid without a signature
DJSnow---this post is manually created and therefore legally valid without a signature
Maybe... and this is a maybe dont get ur hopes up...

friend void BufferClass::operator [] ( BufferClass*, int);

Not an expert, so the first half might be wrong, but the idea is 2 arguments, not just int?

hope it at least points (not pun intended) you in the right direction.

Stu
Yratelev
You don''t. You have to dereference the pointer.

You cannot overload operator[] (BufferClass *, int) because operator[] is required to be a member function.

Even if you could, what would you do if you ever wanted to maintain an array of BufferClass objects? You wouldn''t be able to.

CoV
CoV
@mayrel:

>>maintain an array of BufferClass objects?

no - i don't want to hold an array - that's the point !
i'd like to have the class instanced via:
BufferClass *MyBuffer = new BufferClass( 100 );  

and then to access each element of the buffer via MyBuffer[x].
you are right, yes !


@Yratelev:
>>but the idea is 2 arguments, not just int?
mmh....dunno, to be honest - are the the parameters of "
MyBuffer[25] = 10 
"
handled as the two parameters of the operator [] ?
sorry, i haven't stepped so deep into operator-overloading. so i may seem a bit "unhelped"

DJSnow
---
this post is manually created and therefore legally valid without a signature

[edited by - DJSnow on January 23, 2004 3:40:19 PM]
DJSnow---this post is manually created and therefore legally valid without a signature
You cannot do it. What you're trying to do is replace the default behavior of pointers in C++, and you're not allowed to do that.

Your choices are:

(*MyBuffer)[10] = 10;
or
MyBuffer->operator[](10) = 10;
Your choice.

Also, I think it's possible that you're confused about how constructors work, since your comment seems to indicate that you think constructor arguments can only be passed to new. This code works fine:


BufferClass MyBuffer(100); // elements in constructor this time !
MyBuffer[10] = 10;
MyBuffer[99] = 10;

"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on January 23, 2004 3:44:59 PM]

[edited by - sneftel on January 23, 2004 3:46:54 PM]
You cannot explicitly do what you want to do...that''s the way the language is. You realize that operator overloading is just syntactic sugar, right?

If, for some reason, you HAVE to use a pointer (maybe it''s a class member and you don''t know the size upon the class'' creation? maybe you want to ensure it''s on the heap??) and you REALLY WANT to use the [] notation without a lot of ughly notation, then I would just use a reference as already stated...

For the ultimate in indirection, you could wrap up the pointer in another class that has operator[] defined and forwards all calls to the operator[] to the pointer''s operator[]. Very silly though just for some nice syntax:

class BufferPointer {protected:  BufferClass* m_pBuff;  void copy(const BufferPointer& o) {    if(o != this) {      if(m_pBuff) delete m_pBuff;      m_pBuff = new BufferClass(o.m_pBuff); // assumes deep copy for BufferClass    }  }public:  BufferPointer() : m_pBuff(0) {}  BufferPointer(int size) {    m_pBuff = new BufferClass(size);  }  explicit BufferPointer(BufferClass* pBuf) {     if(m_pBuff) delete m_pBuff;     m_pBuff = pBuf;   }  BufferPointer(const BufferPointer&) {    copy(o);  }  BufferPointer& operator=(const BufferPointer& o) {    copy(o);    return *this;  }  virtual ~BufferPointer() { delete m_pBuff; }  int& operator[](unsigned int i) { assert(m_pBuff); return (*m_pBuff)[i]; }};class OtherClass {  BufferPointer buf;  void func() {    buf = BufferPointer(new BufferClass(100));      buf[10] = 5;  }};

P.S. This is why I never found overloading operators was a great solution. It''s often easier just to use an at() function...it''s a small name and looks just fine to me:

templateclass BufferClass {  T* array;public:  BufferClass(unsigned int size) { array = new T[size]; }  virtual ~BufferClass() { delete array; }  T& at(unsigned int index) { return array[index]; }};BufferClass* pbuf = new BufferClass(100);pbuf->at(5) = 8; 


Regards,
Jeff
quote:Original post by DJSnow
no - i don''t want to hold an array - that''s the point !

You may wish to in the future.
quote:
i''d like to have the class instanced via:
BufferClass *MyBuffer = new BufferClass( 100 );   

and then to access each element of the buffer via MyBuffer[x].
you are right, yes !

I can see what you want to do, but you can''t do that. At all. Ever.
quote:
mmh....dunno, to be honest - are the the parameters of "
MyBuffer[25] = 10  
"
handled as the two parameters of the operator [] ?
sorry, i haven''t stepped so deep into operator-overloading. so i may seem a bit "unhelped"

No, operator[] accepts only one argument, which is the index. If it returns a reference to the selected element, then you can set that element with =. If it doesn''t return a reference, then you can''t set it with =.

CoV
CoV

This topic is closed to new replies.

Advertisement