Accessing base class functions

Started by
8 comments, last by Paradigm Shifter 10 years, 2 months ago

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++ ?

Advertisement
Base::move();


#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
  }
};

Stephen M. Webb
Professional Free Software Developer

@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

chaos, panic and disorder - my work here is finished

All base class functions are automatically inherited. All you need is a move() unless you overrode move, in which you would need Base::move().

But yeah, like //Lumia said, I never have to call a base class function if I overrode it, because I overrode it for a reason. If you find yourself needing to do this, try to break up your code into smaller bits and only override the parts that you really need to override.
what

What would be the cases where I would need too use base::Move() ?

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I was thinking of using it after but I can not think of any example.

Logging calls (on entry/exit) to the function is one example. Probably better ways to do that though.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement