Language : C++

I have had a bit of confusion with overloading the script operator. Not really sure on how to incorporate the operator. I am almost finished coding a program assignment except for this problem with the srcipt operator.

The script operator info taken directly from assignment instructions is the following:

"operator[]

The indexing operator should be overloaded to provide accessor methods for the class. Subscript 0 should provide access to the x component value of the vector; subscript 1 to the y component and subscript 2 to the z component. Subscript values other than 0, 1, or 2 produce undefined results. For speed, no error checking needs to be done.

The choice of how the data members are implemented in the class could greatly affect the complexity of overloading this particular operator.

Don't forget that this operator needs to be overloaded twice, once for getting a value and once for setting a value".

This is what the class looks like.

class V
{
                // Friends
friend V operator*(float, const V &);
friend V operator*(const V &, float);
friend ostream & operator<<(ostream & , const V &);

                // Class data members
private:
float x,
        y,
        z;
                // Method Prototypes
public:
V(int  , int , int);
bool operator ==(const V& r);
V operator +(const V & right); 
V operator -(const V & right);
float operator*(const V & right);
};

With the data members of the class set as float, how can the overloaded script operator come together to work with float members ? How can the overloaded script operator be coded?

Using a subscript operator implies a container of some sort: an array, a vector, a linked list, etc. Subscript operators often come in pairs.

In your case, x, y, and z might be seen as elements to your "container" -- to which elements 0, 1, and 2 might be subscript inspector/mutator.

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.