In C# in the inherited class we could access base class functions with keyword base like:
void SomeFunction()
{
base.Move();
}
How can we do this in C++ ?
In C# in the inherited class we could access base class functions with keyword base like:
void SomeFunction()
{
base.Move();
}
How can we do this in C++ ?
#include <iostream>
class Base {
public:
virtual ~Base() { }
public:
virtual void a() {
std::cout << "Base::a()" << std::endl;
}
virtual void b() {
std::cout << "Base::b()" << std::endl;
}
};
class Derived : public Base {
public:
void a() {
Base::a();
std::cout << "Derived::a()" << std::endl;
}
};
int main( int, char*[] ) {
Derived a;
Base& b = a;
b.a();
b.b();
}
Hi,
I'm actually not sure if C# provides namespaces because there should be packages. But they are aiming at the same goal anyway.
So if you are working with a base class from another namespace make sure to "mark" this in your code as well.
Here is a short example:
#include <iostream>
namespace cars {
class Car {
public:
void move() {
std::cout << "The CAR is moving" << std::endl;
}
};
};
class Audi:public cars::Car {
public:
void someFunction() {
move();
cars::Car::move();
}
void move() {
std::cout << "The Audi is moving" << std::endl;
}
};
int main(int argc, char* argv[]) {
Audi audi = Audi();
audi.someFunction();
return 0;
}
The Code above results in:
The Audi is moving.
The CAR is moving.
But as long as you're not overwriting the base-class method it should be called anyway because you derived from that class.
#include <iostream>
namespace cars {
class Car {
public:
void move() {
std::cout << "The CAR is moving" << std::endl;
}
};
};
class Audi:public cars::Car {
public:
void someFunction() {
move();
cars::Car::move();
}
};
int main(int argc, char* argv[]) {
Audi audi = Audi();
audi.someFunction();
return 0;
}
This code results in:
The CAR is moving,
The CAR is moving.
In addition to that i think that this is a bad code practice. Please correct me if i'm wrong but i can't figure out a situation that forces me to do things like that (I'm either overwriting the method or using the derived one)
In addition to that i think that this is a bad code practice.
QFE
If a derived class changes the behaviour of the base class, chances are it's broken its contract with clients and you're gonna have a hard time. Invariants may be violated. Encapsulation sure is.
If you find you need to override your base class member functions, rethink your design. It may be better to express the derived functionality as virtual callouts using the NVI idom. Like so.
class Something
{
public:
void do_something()
{
do_pre_something();
// basic something
do_post_something();
}
private: /// <- note
virtual void do_pre_something() {}
virtual void do_post_something() {}
};
class FancySomething
: public Something
{
void do_pre_something()
{
// fance something
}
};
@Bregma:
This might be nitpicking, but in C++11 I would use "final" and "override" too to further define my intention:
class Something
{
public:
void do_something() final
{
do_pre_something();
// basic something
do_post_something();
}
private: /// <- note
virtual void do_pre_something() {}
virtual void do_post_something() {}
};
class FancySomething
: public Something
{
void do_pre_something() override
{
// fance something
}
};
Regards
Markus
What would be the cases where I would need too use base::Move() ?
You want to do something extra before or after the call to base::Move
Logging calls (on entry/exit) to the function is one example. Probably better ways to do that though.