Suppose I have class A with a virtual function F():
class A
{
virtual void F()
{
// Do something
};
};
And I have another class B which inherits A and redefines F():
class B : A
{
void F()
{
// Do something
};
};
And a different class C which also inherits A but overrides F():
class C : A
{
void F() override
{
// Do something
};
};
What is the difference between F() in classes B and C?
Both are overrides.
When you use the keyword override you ensure a compilation failure if it should happen to not be an override.
And that's good practice.
Both B::f() and C::f() are overrides and they are exactly the same.
override is essentially a compile-time advisory term that will cause a compilation error if the function does not override one in a base class.
This can help program stability: if the name and parameter types to A::f() are changed, then a compile error will result.
Related
It is very easy that we can make sure derived class must implement interface defined in base class.
That is pure virtual function.
For example:
class BaseClass
{
...
virtual void print()=0;
...
}
class DerivedClass :public BaseClass
{
// function must be implement, otherwise compiler will complain ...
void print()
{
}
};
Can we defined a static interface in base class and make sure the interface must be implement in derivate class?
I want something like this
class BaseClass
{
...
static void print(); // base class only define static interface
...
}
class DerivedClass :public BaseClass
{
// derived class must implement interface, otherwise compiler will complain ...
static void print()
{
}
};
I have no idea about this.
Thanks for your time.
It is not possible to make a virtual static function. For the simple reason that when calling a static function, you always know the class that defines that function in compile time. Unlike virtual functions, where you don't know the type of the object whose method you're calling.
For example:
class A
{
public:
virtual void f() {printf("A");}
};
class B : public A
{
virtual void f() override {printf("B");}
};
void g(A& a)
{
a.f();
}
int main()
{
B b;
g(b);
return 0;
}
In the above example, inside the function g, the correct function is invoked (B::f). Even though while compiling the function it is not known what the type of its argument is (it could be A or any class derived from A).
Without making f() virtual, you would have overloaded the method f, rather than overridden it. Which means that in the following example, the output would be "A", even though you might expect it to be "B":
class A
{
public:
void f() {printf("A");}
};
class B : public A
{
void f() {printf("B");}
};
void g(A& a)
{
a.f();
}
int main()
{
B b;
g(b);
return 0;
}
This may cause serious bugs, and it is suggested to never overload base class methods, and to always use the override keyword when overriding a virtual method to escape those bugs.
When making a static function, you can simply overload it, it would not create a compilation error. However, you probably never should overload it, because it may hide a bug that is very difficult to track (you are certain that B::f() is being called while actually A::f() is being called).
Furthermore, it is not possible to 'force' the derived class to implement a static interface, because there is no such thing as a static interface. Because you have no virtual static functions, you may not pass a reference or pointer to the interface that would implement this function.
Suppose we have the following code:
class Base
{
public:
virtual void foo() const
{ cout << "In Base::foo\n"; }
}
class Derived : public Base
{public:
virtual void foo()
{ cout << "In Derived::foo\n"; }
}
void main()
{
Base* b = new Derived();
b->foo();
delete b;
}
It will give us the following output: In Base::foo.
Now suppose I want to get - without changing the main function - the follwing output instead the one given above:
In Derived::foo
As far as I understand, I should override the function foo() of in the base, to get the output of the method
foo() in the inheriting class which is class 'Derived'.
But the problem is that in that case I can't using the command override, becuase the method in the base is defined as constant , which in the other class it is not.
So, how should I if then overriding that method ?
In order to override the function void foo() const of the base class, you must declare the function void foo() const in the derived class. You've declared the function void foo() instead (note the lack of const), which doesn't override the function in the base, because it has a different declaration.
So, how should I if then overriding that method ?
Add the missing const qualifier.
P.S. Few other bugs in your program:
main must return int.
Class definitions must end in a semicolon.
Deleting a derived object through a pointer to base has undefined behaviour unless the destructor of the base is virtual.
I would like a class B not to be able to redefine one of the member function of its base class A. Is there a way to do that?
EDIT:
Thanks for the answers. Can I prevent non-virtual member functions from being overridden as well?
If you mean disallowing a derived class to override a virtual function of its base class, C++11 introduced a way to do that with a final virt-specifier (as it's called in the standard):
struct B{
virtual void f() final;
};
struct D : B{
void f(); // error: 'B::f' marked 'final'
};
I don't know which compilers support that, though. Probably GCC 4.7 and Clang.
If your methods are virtual, in C++11 you can prevent overriding them with final.
class A
{
public:
virtual void foo() final;
};
class B : public A
{
public:
void foo(); // <-- error
};
You can't prevent hiding.
Can I prevent non-virtual member functions from being overridden as
well?
No you can't, BUT from another side you can... if you're not shore if will you acctualy override base method or not in your derived class you can check whther you're overriding an method or not using a keywod override
when applying that keywod to your method you make shore that you accutaly realy want override that mathod.
so if signature of inherted method is the same as the signature of base method you'll get compile time error, say that derived method does not override base method.
so using override keyword you are telling the compiler that you DO NOT want to everride it.
by not using override keywod you never know wether you override it or not.
consider following example:
class A
{
public:
void f() {std::cout << "A";}
};
class B : public A
{
public:
void f() override {std::cout << "B";}
};
this result in compile time error because you can't override base method.
by not using the override keyword you'll of course be able to override it.
override keywod is introduced in C++0x standard and is supported by visual studio 2010.
Consider the next code :
#include <iostream>
using namespace std;
class A
{
public:
virtual int f() {cout <<"A:: f()\n"; return 1;}
virtual void f(int) {cout <<"A:: f(int)\n";}
virtual void g() {cout <<"A::g()\n";}
};
class B3 : public A
{
public:
void f() {cout <<"B3::f ()\n";}
};
int main()
{
return 0;
}
It produces the following error :
..\main.cpp:17: error: conflicting return type specified for 'virtual void B3::f()'
..\main.cpp:9: error: overriding 'virtual int A::f()'
but why ? in the worst case I'd think I'd have an Hiding case , but instead I get compilation error regarding A's virtual int f() {cout <<"A:: f()\n"; return 1;}
thanks ,Ronen
Don't confuse overriding with hiding. You override virtuals.
Your class definition is equivalent to:
class B3 : public A
{
public:
virtual void f() {cout <<"B3::f ()\n";}
};
Once a function declared virtual, it will remain virtual for all classes deriving from your base class, regardless of whether you explicitly declare it virtual or not. Therefore you are trying to override a virtual function and changing its return type, which is illegal in C++. If the function were not virtual, you would simply be hiding the base class implementation, therefore changing the return type is valid. There would be no ambiguity since the compiler would know where to call the function from and what return type to expect.
However, consider having:
A* a;
....
a->f();
What would a-f() return? a is a pointer to A, but can point to an object of type B3. So it either returns an int or doesn't return anything. See the ambiguity here?
Instead, no polymorphism involved,
A a;
a.f();
will cal f from a, same as b3.f would call f from B3.
All in all, overriding base class functions implies keeping the same return type. If you want to create a new function with a different return type, change its signature (either its name or its parameters - or both).
Anyway, you shouldn't even be doing this... Why would you want to have a function with the same name and no parameters return different things? Wouldn't adding a separate function be more readable?
You'd have hiding if f() would have had a different parameter list, or not declared as virtual on the base class. In the former case, since overloading doesn't cross inheritance borders, A's f would have been hidden. But this is not the case, since you have f() on both classes, which only differ in return value. Return values covariance is the only difference allowed, and since it's not the case (void does not inherit from int), you get the error.
Given the base class A and the derived class B:
class A {
public:
virtual void f() = 0;
};
class B : public A {
public:
void g();
};
void B::g() {
cout << "Yay!";
}
void B::f() {
cout << "Argh!";
}
I get errors saying that f() is not declared in B while trying do define void B::f(). Do I have to declare f() explicitly in B? I think that if the interface changes I shouldn't have to correct the declarations in every single class deriving from it. Is there no way for B to get all the virtual functions' declarations from A automatically?
EDIT: I found an article that says the inheritance of pure virtual functions is dependent on the compiler:
http://www.objectmentor.com/resources/articles/abcpvf.pdf
I'm using VC++2008, wonder if there's an option for this.
Do I have to declare f() explicitly in B?
Yes, you have to declare in the class' definition all virtual function of any base classes that you want to override in the class. As for why: That's just the way the C++ syntax is.
Note that the virtual keyword can be omitted for the declaration of overriding virtual functions:
class base {
virtual void f();
virtual void g();
};
class derived : public base {
virtual void f(); // overrides base::f()
void g(); // overrides base::g()
};
Note: A class declaration is this: class my_class;, while this class my_class { /* ... */ }; is a class definition. There's a limited number of things you can do with a class that's only been declared, but not defined. In particular, you cannot create instances of it or call member functions.
For more about the differences between declaration and definitions see here.
Ok, for the benefit of the "declaration vs. definition" debate happening in the comments, here is a quote from the C++03 standard, 3.1/2:
A declaration is a definition unless it [...] is a class name declaration
[...].
3.1/3 then gives a few examples. Amongst them:
[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b
[...]
struct S; // declares S
—end example]
To sum it up: The C++ standard considers struct S; to be a declaration and struct S { /*...*/ }; a definition. I consider this a strong backup of my interpretation of "declaration vs. definition" for classes in C++.
Yes, in C++ you have to explicitly clarify your intention to override the behavior of a base class method by declaring (and defining) it in the derived class. If you try to provide a new implementation in derived class without declaring it in class definition it will be a compiler error.
The C++ class declaration defines the content of the class. If you do not declare f() in B, it looks like you do not override it. B::f() can be implemented only if you declare it.
In your current code, you are just inheriting the function f() in class B and you do not redefine base class's member in derived class.
Is there no way for B to get all the
virtual functions' declarations from A
automatically?
If you do not mark the function f in A as pure virtual with =0, the function is automatically also present in any subclass.
class A {
public:
virtual void f(); // not =0!
};
class B : public A {
public:
void g();
};
void A::f() {
cout << "I am A::f!";
}
void B::g() {
cout << "Yay!";
}
Now:
B* b = new B();
b->f(); // calls A::f
By declaring a pure virtual function you are stating that your class is abstract and that you want to require all concrete derived classes to have an implementation of that function. A derived class which does not supply an implementation for the pure virtual function is an extension of the abstract base class and is, itself, an abstract class. Trying to instantiate an abstract class is, of course, an error.
Pure virtual functions allow you to define an interface "contract" that you expect all derived classes to adhere to. A client of that class can expect that any instantiated class with that interface implements the functions in the contract.
Another interesting tidbit... you may supply a body for a pure virtual function but it is still pure and must be overridden in a concrete derived class. The advantage to supplying the body is to provide base behavior while still forcing derived classes to implement the function. The overridden functions can then call the base function Base::F() just like other virtual functions. When the body is not defined, calling Base::F() on a pure virtual functions is an error.