class Order {

private:
vector<OrderItem> items;

public:

// Constructor
Order() {}

// accessors
OrderItem operator[](int index) const {
return items[index];
}

// mutators
OrderItem& operator[](int index) {
return items[index];
}


in this programme the accessors seems useless,and what is the use of the operator "&" in the mutator?

>> what is the use of the operator "&" in the mutator
The class is declaring the operator[] which returns a reference to one of the elements of an array. By doing this you can use it in the left-hand side of an expression. You didn't post class OrderName, so lets just assume it has an int member that you want to assign a value of 5.

Order o;
o[1].someVariable = 5;

In the above example, the class'es [] operator will be invoked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.