Overloading Square Bracket [] Operator for Class Constructor Array Output in Main

Pages: 12
I never said there was an advantage or disadvantage with either. My point was that returning an int& is unnecessary with primitive types since a copy is pretty inexpensive. You seem to have the impression that hamsterman and myself are saying not to return by reference, and that is NOT what we are saying.
I'm confused are you referring to pass by reference or return by reference?
@clanmjc
Perhaps I should have said its unnecessary to pass or return a reference with primitive types since the copy is inexpensive with primitives. Or am I missing something?
@naraku9333
Ok, now it's clear as long as you are just talking about the inexpensive copies. In this context with overloading the [] operator though you do need to return the reference, even though the data is primitive. I just wanted to clarify for anyone that looked at this thread later on.
> Perhaps I should have said its unnecessary to pass or return a reference with primitive types
> since the copy is inexpensive with primitives. Or am I missing something?

Yes, you are. Passing or returning an int by reference is sometimes useful. A couple of trivial examples:

1
2
3
4
void homegrown_swap( int&, int& ) ;  

int& errno_for_this_thread() ;
#define errno errno_for_this_thread() 


Passing or returning an int by reference to const is worse than merely useless; it is an abomination. Particularly when it appears in an object-oriented or generic interface.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct A
{
    virtual ~A() {}
    
    // repugnant to the idea of A being an interface; it removes implementation 
    // choices - the string that holds the name must have an appropriate life-time
    // for example, it can't be something that is read on the fly from a file. 
    virtual const std::string& name() const = 0 ;
    
    // ...
};

struct B
{
    virtual ~B() {}
    
    // this is a far more flexible approach. However there is some extra 
    // overhead involved in making a copy of a string, which (in some cases)
    // we may want to avoid.    
    virtual std::string name() const = 0 ;
    
    // ...
};

struct C
{
    virtual ~C() {}
    
    // this is an abomination - voluntarily opting for only minuses with no plus 
    virtual const int& value() const = 0 ;
    
    // ...
};





@clanmjc
Yes, that is what I was talking about. I see your point about operator[] requiring a reference, and honestly after thinking, it does make sense. Apologies to therockon7throw I may have completely missed the context of your point.

@JLBorges

I agree completely on the usefulness, again my context was pretty narrow. I dont quite understand what you mean by
JLBorges wrote:
Passing or returning an int by reference to const is worse than merely useless; it is an abomination. Particularly when it appears in an object-oriented or generic interface.
but I will look into that more when I have some time.
Last edited on
I'm still confused, here's what I've got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

class intArray
{
public:
intArray()
{
int list[10];
int *pA;
pA = list;
*(pA+0)  = 32;
*(pA+1)  = 35;
*(pA+2)  = 54;
*(pA+3)  = 31;
*(pA+4)  = 90;
*(pA+5)  = 41;
*(pA+6)  = 56;
*(pA+7)  = 98;
*(pA+8)  = 99;
*(pA+9)  = 21;
}
intArray operator[](intArray);
};


int main()
{
intArray mainArray;
cout << mainArray[0] << endl;

return 0;
}


That compiles, but nothing happens when I execute a.out because I didn't tell it to do anything yet... I just need to know, how to overload a bracket operator and why you even need to overload it in the 1st place. I just don't get this: (from the instructions) "In your main program output the contents of an object of this class by using the [] operator. You will have to overload the [] in the class"

the full instructions are:
"Write a class that creates and populates an array of integers (any size) using a pointer. Make sure the constructor initializes and populates the array. In your main program output the contents of an object of this class by using the [] operator. You will have to overload the [] in the class. Also, show that you cannot output an array element that is out of bounds."

I appreciate the replies but I'm still stuck

Thanks guys,

-Sean


Oh pfff, i just saw viliml's post ( http://www.cplusplus.com/forum/general/62240/#msg337508 ) that makes sense
Last edited on
That is because your operator is incorrect...

1
2
3
4
5
6
7
int& operator[](int position)
{
  //...Validate the position
  //if(position .....
  //... if this is valid return... list[position]
  //otherwise the position is out of bounds so return a sentinel values (whatever you want to make up, maybe the first element for instance.  list[0]
}
Yeah, what's the & needed for?
So you could use your class like this...

 
mainArray[0] = 5555;  //assignment, and we all know if you returned by value this would change the copy and not the actual variable you intended to. 
@clanmjc: No, you can't assign to the copy returned by the function: http://ideone.com/WarJ1
It's a good thing too, or this would go unnoticed.
@LB: Touche, it does need to be an lvalue, it's been a while... however, the point of it being a reference is so you can assign (forget the value/copy crap I mentioned @Sean Walker)
Topic archived. No new replies allowed.
Pages: 12