Jump to content

  • Log In with Google      Sign In   
  • Create Account

Accessing base class functions


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
9 replies to this topic

#1   Members   

294
Like
0Likes
Like

Posted 16 January 2014 - 03:21 PM

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



#2   Moderators   

9752
Like
6Likes
Like

Posted 16 January 2014 - 03:31 PM

*
POPULAR

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


zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#3   Members   

650
Like
2Likes
Like

Posted 16 January 2014 - 04:01 PM

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)



#4   Members   

8321
Like
3Likes
Like

Posted 16 January 2014 - 06:56 PM

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

#5   Members   

661
Like
0Likes
Like

Posted 17 January 2014 - 04:17 AM

@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


#6   Members   

779
Like
2Likes
Like

Posted 17 January 2014 - 09:32 AM

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

#7   Members   

294
Like
0Likes
Like

Posted 17 January 2014 - 10:34 AM

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


Edited by ryt, 17 January 2014 - 10:36 AM.


#8   Members   

5832
Like
0Likes
Like

Posted 17 January 2014 - 01:57 PM

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, 17 January 2014 - 01:57 PM.

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

#9   Members   

294
Like
0Likes
Like

Posted 17 January 2014 - 02:46 PM

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



#10   Members   

5832
Like
0Likes
Like

Posted 17 January 2014 - 03:06 PM

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




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.