overriding private and protected virtual functions

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 4233 Views - Last Post: 22 October 2011 - 12:35 PM Rate Topic: -----

#1 r_a_v_i_1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 01-May 09

overriding private and protected virtual functions

Posted 19 October 2011 - 06:21 AM

Is it possible to override private and protected virtual functions?i tried it but the gcc compiler gives error.can anybody answer with an example
Is This A Good Question/Topic? 0
  • +

Replies To: overriding private and protected virtual functions

#2 Elijah Becker  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 16-October 11

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 06:59 AM

You can override protected functions if you inherit from that class, but private functions are private.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,184
  • Joined: 21-June 11

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 07:11 AM

Yes, it's allowed to override private virtual functions. Here's an example:

#include<iostream>

class base {
public:
  int bla() {return foo();}
private:            
  virtual int foo() {return 42;}
};

class derived : public base {
private:
  virtual int foo() {return 23;}
};
        
int main()
{
  derived d;
  std::cout << d.bla() << std::endl;
}



The above code compiles without errors and prints 23 when run. The same goes for protected methods.

View PostElijah Becker, on 19 October 2011 - 03:59 PM, said:

You can override protected functions if you inherit from that class, but private functions are private.


You're wrong. I mean technically what you said is true: private functions are indeed private - that's a tautology. But your implication that private virtual functions can't be overridden is wrong.
Was This Post Helpful? 2
  • +
  • -

#4 r_a_v_i_1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 01-May 09

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 07:43 AM

but your program is giving segmentation fault
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,184
  • Joined: 21-June 11

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 07:49 AM

View Postr_a_v_i_1, on 19 October 2011 - 04:43 PM, said:

but your program is giving segmentation fault


It doesn't for me. Are you sure you copied my code verbatim?
Was This Post Helpful? 0
  • +
  • -

#6 r_a_v_i_1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 01-May 09

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 07:49 AM

#include<iostream>
	using namespace std; 
	class base {
	public:
	  int bla() {return foo();}
	private:           
	  virtual int foo() {return 42;}
	};
	 
	class derived : public base {
	public:
	   int foo() {return 23;}
	};
	         
	int main()
	{
          base *b; 
	  derived d;
          b = &d;
                
          cout << b->foo() << endl;
	}


Sorry,sepp2k the your program is not giving any error.But i modified your program as above and compiler gives error.can u tell me why?

This post has been edited by macosxnerd101: 19 October 2011 - 08:27 AM
Reason for edit:: Please use code tags: [code] Code Goes Here [/code]

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,184
  • Joined: 21-June 11

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 07:52 AM

Your code is giving an error because you're trying to call foo, which is private in the base class, through a pointer to the base class. You need to call bla, not foo.
Was This Post Helpful? 0
  • +
  • -

#8 r_a_v_i_1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 01-May 09

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 07:57 AM

But base pointer is holding derived object.so at the time of function resolution it should see only derived class access right?
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,184
  • Joined: 21-June 11

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 08:04 AM

View Postr_a_v_i_1, on 19 October 2011 - 04:57 PM, said:

But base pointer is holding derived object.so at the time of function resolution it should see only derived class access right?


Nope. When you're operating on a pointer to base, all that matters (in regard to what's allowed and what isn't) is the definition of base. The compiler has no idea that the pointer points to a derived object. So you're only allowed to call methods which are declared as public in the base class. If they're private in the base class (or not defined in the base class at all), you can't call them through a base pointer.

The basic principle is that when you're working on a base pointer, everything you do must be a valid operation for all objects of type base or one of its subtypes. So if a certain operation would only be allowed on derived objects, you're not allowed to do that through a base pointer.
Was This Post Helpful? 0
  • +
  • -

#10 andy_pleasants  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 122
  • Joined: 08-July 10

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 09:01 AM

Quote

#include<iostream>

class base {
public:
  int bla() {return foo();}
private:            
  virtual int foo() {return 42;}
};

class derived : public base {
private:
  virtual int foo() {return 23;}
};
        
int main()
{
  derived d;
  std::cout << d.bla() << std::endl;
}



This is not overriding this is redefining.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,184
  • Joined: 21-June 11

Re: overriding private and protected virtual functions

Posted 19 October 2011 - 09:21 AM

View Postandy_pleasants, on 19 October 2011 - 06:01 PM, said:

This is not overriding this is redefining.


Then why does it still print 23 if I change the code to access d through a base pointer instead of directly (without changing the class definitions), i.e. why does the following code still print 23:

#include<iostream>

class base {
public:
  int bla() {return foo();}
private:
  virtual int foo() {return 42;}
};

class derived : public base {
private:
  virtual int foo() {return 23;}
};

int main()
{
  derived d;
  base* b = &d;
  std::cout << b->bla() << std::endl;
}



If derived did not override the foo method, then surely the above code should print 42, no? Actually given that I'm never calling foo directly, but only call it through base's bla method, even the original code should print 42 if derived did not override foo.
Was This Post Helpful? 0
  • +
  • -

#12 andy_pleasants  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 122
  • Joined: 08-July 10

Re: overriding private and protected virtual functions

Posted 20 October 2011 - 08:09 AM

Just tried it myself and used the debugger n it does seem that vfptr does point to this private function :S

Now I'm a little confused, I come from Java and private methods are private, cannot be inherited or overriden
Was This Post Helpful? 0
  • +
  • -

#13 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: overriding private and protected virtual functions

Posted 20 October 2011 - 08:34 AM

View Postandy_pleasants, on 19 October 2011 - 09:01 AM, said:

Quote

#include<iostream>

class base {
public:
  int bla() {return foo();}
private:            
  virtual int foo() {return 42;}
};

class derived : public base {
private:
  virtual int foo() {return 23;}
};
        
int main()
{
  derived d;
  std::cout << d.bla() << std::endl;
}



This is not overriding this is redefining.


This is overriding.

I don't understand the confusion, sepp2k said all there is to say in his first response.

The private/public/protected controls who has access to a function it is not part of the function signature. So overriding behavior is totally independent from access control and you can combine the two any way you like.

I don't see anything wrong with overriding a private function with a private function.

Overriding public functions with private functions or protected functions would be signs of a poor design as it will allow client code to run a private/protected function but it is allowed.
Was This Post Helpful? 0
  • +
  • -

#14 andy_pleasants  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 122
  • Joined: 08-July 10

Re: overriding private and protected virtual functions

Posted 21 October 2011 - 12:54 AM

Confused mainly because it doesn't seem to make sense, I understand now that yes it is possible to override private methods. But if a method is private it's by definition part of the class' implementation and not the class' interface.
Was This Post Helpful? 0
  • +
  • -

#15 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: overriding private and protected virtual functions

Posted 21 October 2011 - 04:20 PM

View Postandy_pleasants, on 21 October 2011 - 08:54 AM, said:

Confused mainly because it doesn't seem to make sense, I understand now that yes it is possible to override private methods. But if a method is private it's by definition part of the class' implementation and not the class' interface.
What that means is that the class supports parts of its implementation being overridden to change its behaviour; or maybe that derived classes need to be constrained with certain pre/post conditions in order to override certain functions.

For example, sometimes you may want to be able to enforce certain behaviour on your derived classes by ensuring that the overridden method must be part of a chain of other events; the way to do that is to make sure that the user of the class is unable to access the virtual functions directly and force them to use a non-virtual 'proxy' member function instead which does all of the mandatory common behaviour.

e.g. a class providing its derived classes with a threadsafe way to access a std::vector buffer
class safe_buffer
{
public:
    safe_buffer()
    {
        initialise(mutex);
    }
    void update()
    {
        acquire(mutex);
        update_buffer(buffer);
        release(mutex);
    }
private:
    std::vector<unsigned char> buffer;
    mutex_type mutex;
    virtual void update_buffer(std::vector<unsigned char>&) = 0;
};

class funky_buffer : public safe_buffer
{
    void update_buffer(std::vector<unsigned char>& buffer)
    {
        //TODO: modify the buffer now that the base class mutex has ensured it is safe to do so
    }
}; 
creating an instance of funky_buffer would not present an 'update_buffer' function to the user, but it would present the thread-safe 'update' - the person implementing the funky_buffer would not need to go worrying about thread safety, and the user would not need to care about its implementation either.

if update_buffer were public, then it simply wouldn't work - update_buffer could be called without acquiring the mutex, and then it wouldn't be threadsafe any more.

This post has been edited by Bench: 21 October 2011 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2