operator[] ? lvalues!?
operator[] ? lvalues!?
How can I make an object so that I can go MyObject[5]=93;
it says that operator[] takes exactly 1 argument, so how the crap is it possible?
it says that operator[] takes exactly 1 argument, so how the crap is it possible?
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re: operator[] ? lvalues!?
Return a non-const reference to the item that you want to receive the value 93. The 1 argument is the index -- 5 in your example. The assignment happens after operator[] returns.hckr83 wrote:How can I make an object so that I can go MyObject[5]=93;
it says that operator[] takes exactly 1 argument, so how the crap is it possible?
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re: operator[] ? lvalues!?
int &operator[](int index);hckr83 wrote:How can I make an object so that I can go MyObject[5]=93;
it says that operator[] takes exactly 1 argument, so how the crap is it possible?
Re: operator[] ? lvalues!?
ahum.. because none of the members change it should be:Candy wrote:int &operator[](int index);hckr83 wrote:How can I make an object so that I can go MyObject[5]=93;
it says that operator[] takes exactly 1 argument, so how the crap is it possible?
Code: Select all
int &operator[](int index) const;
Author of COBOS
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re: operator[] ? lvalues!?
If you take "const" literally, yes, but it is not always a good idea to do so. Think about the bigger picture. Do you really want to allow code like this?os64dev wrote:ahum.. because none of the members change it should be:Candy wrote:int &operator[](int index);hckr83 wrote:How can I make an object so that I can go MyObject[5]=93;
it says that operator[] takes exactly 1 argument, so how the crap is it possible?
Code: Select all
int &operator[](int index) const;
Code: Select all
const MyClass MyObject; // Should NEVER change!
MyObject[5] = 93; // Whoops, we made operator[] const and let something change...
Last edited by Colonel Kernel on Wed Oct 17, 2007 11:35 am, edited 1 time in total.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
maybe i misunderstood but:ok but f you take "const" literally, yes, but it is not always a good idea to do so. Think about the bigger picture. Do you really want to allow code like this?
Code: Select all
const MyClass MyObject; // Should NEVER change! MyObject[5] = 93; // Whoops, we made operator[] and let something change...
Code: Select all
int &operator[](int index) const;
Author of COBOS
Actually it tells the compiler that this function is valid for const objects. Without the trailing const in the function declaration, a function can only be called for non-const objects.os64dev wrote:maybe i misunderstood but:just tells the compiler that this member function does not change the content of the current object and can generate more efficient code.Code: Select all
int &operator[](int index) const;
Do you really want to disallow code like this (which you would if you did not declare the operator const)?Colonel Kernel wrote:Do you really want to allow code like this?
Code: Select all
const MyClass myObject( ... );
cout << myObject[5];
Code: Select all
class MyClass {
public:
MyClass const & operator[]( int index ) const;
MyClass & operator[]( int index );
};
Every good solution is obvious once you've found it.
Author of COBOS
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Of course not.Solar wrote:Do you really want to disallow code like this (which you would if you did not declare the operator const)?
Code: Select all
const MyClass myObject( ... ); cout << myObject[5];
Exactly.Depending on the actual semantics of MyClass, you could write something like this to disallow the code you objected to:
That way, the reference returned by operator[]() is const if the MyClass object is const, and non-const otherwise.Code: Select all
class MyClass { public: MyClass const & operator[]( int index ) const; MyClass & operator[]( int index ); };
Next time I will endeavour to tell more than just half the story.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
Re: operator[] ? lvalues!?
Actually, if you return a reference to a pointer-contained object in this object, this would compile for const objects and be wrong - since you do change the state (or by const-capable actions you perform allow const object visible behaviour to change). Use the method Solar suggested - make two, one for const and one for non-const. Oh, and if you still use MSVC, kick it. VS2005 can't distinguish between const and non-const in at least the copy constructor, so I'm somewhat doubting it can do the other cases.os64dev wrote:ahum.. because none of the members change it should be:
Code: Select all
int &operator[](int index) const;
I refute your attempt to pass MSDN as C++ documentation. Not to mention the non-HTML compatible link.os64dev wrote:constant member function
i use a gcc/g++ cross compiler so no issues hereOh, and if you still use MSVC, kick it. VS2005 can't distinguish between const and non-const in at least the copy constructor, so I'm somewhat doubting it can do the other cases.
lolI refute your attempt to pass MSDN as C++ documentation. Not to mention the non-HTML compatible link.
Author of COBOS
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
You "could" do it the manual way.
then to clean up:
Code: Select all
myObject *myArray;
int NumberOfItems = 5;
myArray[] = new char[NumberOfItems * sizeof(myObject)];
for(int i = 0; i < NumberOfItems; i++)
myArray[i].myArray(); // call the constructor
Code: Select all
for(int i = 0; i < NumberOfItems; i++)
myArray[i].~myArray(); // call the deconstructor
delete myArray;
My OS is Perception.
And what exactly, aside from a load of code and a high complexity, does this add overMessiahAndrw wrote:You "could" do it the manual way.then to clean up:Code: Select all
myObject *myArray; int NumberOfItems = 5; myArray[] = new char[NumberOfItems * sizeof(myObject)]; for(int i = 0; i < NumberOfItems; i++) myArray[i].myArray(); // call the constructor
Code: Select all
for(int i = 0; i < NumberOfItems; i++) myArray[i].~myArray(); // call the deconstructor delete myArray;
Code: Select all
myObject myArray[5];
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
In my way you could unwrap the loop that calls the constructors so that each array element can be passed individual constructor parameters.
My OS is Perception.