Next: , Previous: , Up: Methods   [Index]


Overloading

Overloading Methods

You can always implement a method in different classes. For example, you can define a ‘+’ method in Integer, LongInteger, Character, and Float classes, and Ctalk calls the method defined for that class of the receiver object.

Some operators also have methods implemented in a superclass, like Magnitude in this case, which can provide any extra error checking and processing that may be necessary, for example, if you try to use a unary minus (‘-’) operator with a receiver like a Pen or Rectangle object.

When you overload methods within a class, however, Ctalk does some extra checking. The compiler needs to examine the expression to find out how many arguments the statement has, whether the operator is a prefix operator, or whether the method’s argument is a block of code or a variable argument list, or whether the method uses a C calling convention.

Ctalk can overload math operators when they are also used as prefix operators. Two examples of these are the unary minus (‘-’) and pointer (‘*’) methods, which have different methods than the operators that perform subtraction and multiplication. Writing methods that are prefix operators is described above. See Prefixes.

Overloading Parameters

Exactly when you should write methods that overload things like parameters and variable arguments is somewhat within the philosophy of programming languages. For example, a common use of method overloading based on the number of parameters is the, “getter/setter,” type of method, which retrieves and sets an object’s private data.

In Ctalk, these are much less necessary than in other languages, because Ctalk can address an object’s private data with a message that has the same name as an instance or class variable. Since these messages bind more tightly to receiver objects than messages that refer to methods, these types of methods might not work the way you think they would. So be sure that if you write a method of this type, that the program is actually using a method message, and not an instance data message.

Here is an example of overloading parameters. Because the String class already has a concat method (it overloads the ‘+’ operator), we overload the method, myConcat, to concatenate one or two strings to the receiver.

As long as the program is relatively simple, it’s easy to keep track of which methods already exist in a class. In a bigger application, though, you might want to define a subclass of String class for this program.


String instanceMethod myConcat (String s1) {

  self = self + s1;

}

String instanceMethod myConcat (String s1, String s2) {

  self = self + s1 + s2;

}

int main () {
  String new s1;
  String new s2;
  String new s3;

  s1 = "Hello, ";
  s2 = "world! ";
  s3 = "Again.";

  s1 myConcat s2;
  printf ("%s\n", s1);
  
  s1 myConcat s2, s3;
  printf ("%s\n", s1);

}

We should mention that the myConcat method changes its receiver. So the arguments to the second myConcat message simply get added to the receiver again. The output should look something like this.

Hello, world!
Hello, world! world! Again.

Next: , Previous: , Up: Methods   [Index]