answersLogoWhite

0


Best Answer

Overloading the "function call" operator, i.e. operator()(), can be achieved by defining a method with the following form:

operator()() {}

For example, here's how it would look in the simplest case (no argument or return value):

class Callable {// ...public:voidoperator()() {// do something interesting}// ...};

Overloading the array subscript operator, i.e. operator[](), is just as easy. This operator always takes a single argument (the subscript). Here's a template for a method which overloads this operator:

operator[]( index) {}

For example:

class Subscriptable {// ...public:doubleoperator[](unsigned index) {// compute and return a double}// ...};

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

I think you really mean overriding, but either way you cannot do this. At least not with standard arrays. And nor would you want to.

The plus operator, along with all the other arithmetic operators (including subtract, multiply, divide and modulo, as well as their assignment variants) must always evaluate to a predictable and intuitive evaluation. But what meaning does the plus operator have within the context of an array? None whatsoever.

To some, the plus operator might mean that two arrays could be concatenated together to produce a larger array. After all, many programming languages permit two strings (character arrays) to be concatenated together using the plus operator. Fair enough. But you cannot do this in C nor C++, and rightly so. After all, an array name is simply a reference, an alias for the array's starting address, and you simply cannot sum two references together. You also cannot sum two pointers together.

If you really do intend to overload the plus operator, how would the sum of an array and another object entirely be evaluated? Suppose the other object happens to be an integer. Are we to assume that this integer will be added to every element within the array? What happens when the elements are not intrinsically integers? Or does it really mean that the array should be re-allocated in order to accommodate N new elements as specified by the integer's value? Or would it mean something else entirely?

The point is that the operator alone cannot tell the user what they can expect from that operator. Looking up online help in order to determine the purpose of an otherwise well-established operator is hardly the makings of an intuitive interface. In short, just don't go there.

If you're not planning on releasing your code and it's purely for your own purpose, then you are of course free to decide for yourself whether it is appropriate to override the plus operators as you see fit. However, if you plan on releasing your code, you'd be well-advised to implement these operators in a more intuitive manner.

Rather than overload the plus operator (and all its sibling operators), it is far better to provide clearly-defined methods to perform the operations one might expect with arrays. In order to do so, you must encapsulate your array in a class wrapper. The class will handle all the memory management of the array itself and expose an interface to manipulate that array. This should include common functions such as realloc() to expand and shrink the array, as well as join(), merge(), concatenate(), sort(), etc. The only operator that really needs to overridden is the index operator[], so your class can be treated just as if it were a normal array, allowing access to its elements in the traditional manner.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you overload function call and Array sub-scripting operators?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What do string and vector and array all inherit from that has the size method so you can take it as an argument?

string, vector and array do not have a common base class. Overload your function to accept either a string, a vector or an array.


In VB SCRIPT the first array index is always?

All languages use zero-based subscripting to index array elements because the first element is always allocated zero elements from the start address of the array. Very few languages default to a one-based subscript, however some languages, including VBScript, do allow you to change the lower-bound. Some algorithms are easier to implement with one-based subscripting, however you can also choose any lower and upper bound as appropriate.


How do you swap two adjecent no in array in c?

Option 1) Use a temporary variable: int x = array[i]; array[i] = array[i+1]; array[i+1] = x; Option 2) Use bit operators: array[i] ^= array[i+1] ^= array[i];


What is the array function in CAD?

there r 2 types of array in cad - rectangular array and polar array...........


Why the array is starting from zero?

By design; it makes the compiler's work easier. 1-based array's addressing-function: Address (array, index) = Address (array) + (index-1)*Elemsize(array) 0-based array's addressing-function: Address (array, index) = Address (array) + index*Elemsize (array)


How is an array name interpretedwhen it is passed to a function?

An array is still an array, regardless of how you pass it into functions. It is still an array-type variable at the beginning of a function. However, the function itself may manipulate the array by "imploding" it into a string with a delimiter, using array values with only specific keys, and such.


What is an Access function for an array?

.


What is the purpose of AND array?

AND array is used for developing digital circuit it is used in PLA programmable logic array and PAL programmable array of logic to implement the function. the number of and array will depend on how long you has function to be implemented.These are Digital circuits.


How can and array be passed to a funaction?

try this: <function-name> ( <array-name> )


How do you return an array from function?

By returning a pointer to the first element of the array.


How do you merge two array without using function?

Take another array big enough to hold both array copy content of these two array into new one. You merged two array and haven't used a single function.!


When an array name is passed to function in c?

It 's address is received by the function . that any changes in the value of array elements in the function will result in actual change.