Online Book Reader

Home Category

Professional C__ - Marc Gregoire [126]

By Root 1498 0
cases require attention when overriding a method. In this section, we have outlined the cases that you are likely to encounter.

The Superclass Method Is static

In C++, you cannot override a static method. For the most part, that’s all you need to know. There are, however, a few corollaries that you need to understand.

First of all, a method cannot be both static and virtual. This is the first clue that attempting to override a static method will not do what you intend for it to do. If you have a static method in your subclass with the same name as a static method in your superclass, you actually have two separate methods.

The following code shows two classes that both happen to have static methods called beStatic(). These two methods are in no way related.

class SuperStatic

{

public:

static void beStatic() {

cout << "SuperStatic being static." << endl; }

};

class SubStatic : public SuperStatic

{

public:

static void beStatic() {

cout << "SubStatic keepin' it static." << endl; }

};

Because a static method belongs to its class, calling the identically named methods on the two different classes will call their respective methods:

SuperStatic::beStatic();

SubStatic::beStatic();

Will output:

SuperStatic being static.

SubStatic keepin' it static.

Everything makes perfect sense as long as the methods are accessed by class. The behavior is less clear when objects are involved. In C++, you can call a static method using an object, but since the method is static, it has no this pointer and no access to the object itself, so it is equivalent to calling it by classname::method(). Referring to the previous example classes, you can write code as follows, but the results may be surprising.

SubStatic mySubStatic;

SuperStatic& ref = mySubStatic;

mySubStatic.beStatic();

ref.beStatic();

The first call to beStatic() will obviously call the SubStatic version because it is explicitly called on an object declared as a SubStatic. The second call might not work as you expect. The object is a SuperStatic reference, but it refers to a SubStatic object. In this case, SuperStatic’s version of beStatic() will be called. The reason is that C++ doesn’t care what the object actually is when calling a static method. It cares about only the compile-time type. In this case, the type is a reference to a SuperStatic.

The output of the previous example is as follows:

SubStatic keepin' it static.

SuperStatic being static.

static methods are scoped by the name of the class in which they are defined, but are not methods that apply to a specific object. A method in a class that calls a static method calls the version determined by normal name resolution. When called syntactically by using an object, the object is not actually involved in the call, except to determine the type.

The Superclass Method Is Overloaded

When you override a method by specifying a name and a set of parameters, the compiler implicitly hides all other instances of the name in the superclass. The idea is that if you have overridden one method of a given name, you might have intended to override all the methods of that name, but simply forgot, and therefore this should be treated as an error. It makes sense if you think about it — why would you want to change some versions of a method and not others? Consider the following subclass, which overrides a method without overriding its associated overloaded siblings:

class Super

{

public:

virtual void overload() { cout << "Super's overload()" << endl; }

virtual void overload(int i) {

cout << "Super's overload(int i)" << endl; }

};

class Sub : public Super

{

public:

virtual void overload() { cout << "Sub's overload()" << endl; }

};

If you attempt to call the version of overload() that takes an int parameter on a Sub object, your code will not compile because it was not explicitly overridden.

mySub.overload(2); // BUG! No matching method for overload(int).

It is possible, however, to access this version of the method from a Sub object. All you need is a pointer or a reference to a Super object.

Sub

Return Main Page Previous Page Next Page

®Online Book Reader