• Advertisement
Sign in to follow this  

Accessing base class functions

This topic is 1463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Edited by ryt

Share this post


Link to post
Share on other sites

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

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement