473,769 Members | 7,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

private virtual functions and pure virtual functions with bodies


Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A. Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).

Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)

Feedback appreciated,

Thanks,

JG

Dec 4 '06 #1
10 4805

John Goche wrote:
Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A.
No it won't, if A::f is private, it's private for all including for the
B derivative.
And even if A::f is public, calling B::f doesn't invoke A::f.
Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).
What?
>
Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)
nope. The class is abstract. That does not prevent you from
implementing the pure-virtual if you choose to. A nice feature to have.
>
Feedback appreciated,
Nothing prevents you from calling a pure virtual function, unlike other
languages.

#include <iostream>

class Base
{
public:
virtual ~Base() = 0;
virtual void foo() const = 0;
};

Base::~Base() { std::cout << "~Base()\n" ; }
void Base::foo() const { std::cout << "Base::foo()\n" ; }

class Derived : public Base
{
public:
Derived() { }
~Derived() { std::cout << "~Derived() \n"; }
void foo() const
{
std::cout << "Derived::foo() \n";
Base::foo();
}
};

int main()
{
Derived derived;
derived.foo();
}

/*
Derived::foo()
Base::foo()
~Derived()
~Base()
*/

Dec 4 '06 #2
On Dec 4, 12:57 pm, "John Goche" <johngo...@gmai l.comwrote:
Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A.
No, a private function can not be accessed from the derived class.
Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).
Yes, you can declare a function public if it was private or protected
in the base-class.
Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)
Consider this:

class Base {
virtual int foo() = 0;
public:
virtual int bar();
}

int Base::foo() { return 1; }

int Base::bar() return foo(); }

class Derived : public Base {
virtual int foo();
public:
virtual int bar();
}

int Derived::bar() { return A::bar(); }

int Derived::foo() { return 0; }

int main() {
B b;
std::cout << b.bar(); // will print 1
}

This allows you to use a private pure function with a body, however
note that you must also provide a foo()-function in Derived, since it's
a pure virtual function in Base.

The use of private pure functions with bodies are perhaps not so great.
Public pure functions with bodies can be very useful in providing part
of the functionality wanted from the function, and still requireing any
derived classes to provide their own implementation, even if it's just
a Base::foo()-call.

--
Erik Wikström

Dec 4 '06 #3

Thank you Erik and Peter for the clarifications,

The example really shows how a pure virtual function
must be implemented in a derived class and prevents
the base class from being instantiated directly but
that an implementation of the pure virtual function
in the base class may still exist and be accessed
from some other function in the same base class
or using an access specifier from a derived class
for example.

What is not clear to me though, is, if a private
function cannot be called from a derived class,
then what is the point of making such a
function virtual???

Thanks,

JG

er****@student. chalmers.se wrote:
On Dec 4, 12:57 pm, "John Goche" <johngo...@gmai l.comwrote:
Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A.

No, a private function can not be accessed from the derived class.
Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).

Yes, you can declare a function public if it was private or protected
in the base-class.
Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)

Consider this:

class Base {
virtual int foo() = 0;
public:
virtual int bar();
}

int Base::foo() { return 1; }

int Base::bar() return foo(); }

class Derived : public Base {
virtual int foo();
public:
virtual int bar();
}

int Derived::bar() { return A::bar(); }

int Derived::foo() { return 0; }

int main() {
B b;
std::cout << b.bar(); // will print 1
}

This allows you to use a private pure function with a body, however
note that you must also provide a foo()-function in Derived, since it's
a pure virtual function in Base.

The use of private pure functions with bodies are perhaps not so great.
Public pure functions with bodies can be very useful in providing part
of the functionality wanted from the function, and still requireing any
derived classes to provide their own implementation, even if it's just
a Base::foo()-call.

--
Erik Wikström
Dec 6 '06 #4
What is not clear to me though, is, if a private
function cannot be called from a derived class,
then what is the point of making such a
function virtual???
It is a common pattern for a public function in a base class to call a
private virtual function(s). The public base function defines *what*
steps to perform but the private virtual functions define *how* each
step is implemented. The base function is more or less just the shell
of the task and the subclass can override the private virtual functions
as required to customize how each step is performed. This is the
"Template Method" as explained in "Design Patterns" book by Gamma, et.
al. I highly recommended you get your hands on a copy (as do most
seasoned developers these days it seems!).

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Dec 6 '06 #5
[ Please don't Top Post - rearranged ]
>
er****@student. chalmers.se wrote:
On Dec 4, 12:57 pm, "John Goche" <johngo...@gmai l.comwrote:
Hello,
>
page 202 of Symbian OS Explained by Jo Stichbury states
>
"All virtual functions, public, protected or private, should be
exported"
>
then page 203 states
>
"In the rare cases where a pure virtual function body
has a function body, it must be exported."
>
------------------------------
>
I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A.
No, a private function can not be accessed from the derived class.
Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).
Yes, you can declare a function public if it was private or protected
in the base-class.
Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)
Consider this:

class Base {
virtual int foo() = 0;
public:
virtual int bar();
}

int Base::foo() { return 1; }

int Base::bar() return foo(); }

class Derived : public Base {
virtual int foo();
public:
virtual int bar();
}

int Derived::bar() { return A::bar(); }

int Derived::foo() { return 0; }

int main() {
B b;
std::cout << b.bar(); // will print 1
}

This allows you to use a private pure function with a body, however
note that you must also provide a foo()-function in Derived, since it's
a pure virtual function in Base.

The use of private pure functions with bodies are perhaps not so great.
Public pure functions with bodies can be very useful in providing part
of the functionality wanted from the function, and still requireing any
derived classes to provide their own implementation, even if it's just
a Base::foo()-call.

--
Erik Wikström
John Goche wrote:
Thank you Erik and Peter for the clarifications,

The example really shows how a pure virtual function
must be implemented in a derived class and prevents
the base class from being instantiated directly but
that an implementation of the pure virtual function
in the base class may still exist and be accessed
from some other function in the same base class
or using an access specifier from a derived class
for example.

What is not clear to me though, is, if a private
function cannot be called from a derived class,
then what is the point of making such a
function virtual???
A creator programmer is writing a "rule" for the user of the class(es)
that states that a pure-virtual function *must* be implemented in order
to satisfy the requirements - thats the logic. Imagine a system that
handles elements having the requirement that some given method() be
available. Declaring the void method() as pure-virtual essentially
guarentees that the derivatives will have that void method()
implemented somewhere with the option of overriding it. Note: a derived
class doesn't have to override that function *if* its already
implemented lower in the inheritance hierarchy.

Personally, i think that making a pure-virtual private is a bad idea.
Also, suggesting that an abstract class should never have members is a
ludicrous requirement ( such members in fact would ultimately become
part of a derived entities anyways ). An interface in Java is not
allowed to have members, but no such restriction is imposed on C++.
And, IMHO, i don't see why such a restriction should be imposed.

If a given set of composition members is common to an inheritance
hierarchy, putting them elsewhere than in an abstract class is poor
code. Even for a pure interface. I find that imposing such a rule
outright is counter-productive. Except, of course, in the case where a
particular language requires it. In the case you put forth, that sounds
like a requirement made to satisfy an IDL compiler.

Dec 7 '06 #6

Just as a note...
Here is an example of how a derived class can
invoke a private function in a base class when
such function is declared virtual:

#include <iostream>

class A {
private:
virtual void foo() = 0;
};

class B: public A {
public:
virtual void foo();
};

void B::foo() {
std::cout << "foo" << std::endl;
}

int main() {
B b;
b.foo();
}

output: foo

Dec 7 '06 #7

John Goche wrote:
Just as a note...
Here is an example of how a derived class can
invoke a private function in a base class when
such function is declared virtual:

#include <iostream>

class A {
private:
virtual void foo() = 0;
};

class B: public A {
public:
virtual void foo();
};

void B::foo() {
std::cout << "foo" << std::endl;
}

int main() {
B b;
b.foo();
}

output: foo
I hate to have to disappoint you, but thats not correct. The output
comes from B::foo() (realize also that A::foo() is not even
implemented). By the way: B::foo() is already virtual.
Compile and run the following, read the output, then uncomment the call
to A::foo(). Read the error.
Note: base functions are *overriden* by virtual derived functions:
--- the derived foo() *hides* the base's foo() ---
so you must call it explicitly - and thats by design.

#include <iostream>
#include <ostream>

class A {
private:
virtual void foo() const = 0;
};

void A::foo() const {
std::cout << "A::foo()\n ";
}

class B: public A {
public:
void foo() const;
};

void B::foo() const {
// A::foo(); // must be public in class A
std::cout << "B::foo()\n ";
}

int main() {
B b;
b.foo();
}

/*
B::foo()
*/

Finally, consider what happens if you derive from class B like this:

class C : public B
{
};

int main()
{
C c;
c.foo(); // this works !!!
}

Do you understand now If you don't overide foo() what happens?
Class A does not require all/each of its derivatives to implement
foo(), as long as foo() is indeed implemented somewhere down the
hierarchy, its indeed callable.

Dec 7 '06 #8
Salt_Peter <pj*****@yahoo. comwrote:
Personally, i think that making a pure-virtual private is a bad idea.
Guru Sutter disagrees with you:

"When should virtual functions be public, protected, or private?"

The short answer is: Rarely if ever, sometimes, and by default,
respectively - the same answer we've already learned for other kinds
of class members.

http://www.gotw.ca/publications/mill18.htm

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Dec 7 '06 #9

Marcus Kwok wrote:
Salt_Peter <pj*****@yahoo. comwrote:
Personally, i think that making a pure-virtual private is a bad idea.

Guru Sutter disagrees with you:

"When should virtual functions be public, protected, or private?"

The short answer is: Rarely if ever, sometimes, and by default,
respectively - the same answer we've already learned for other kinds
of class members.

http://www.gotw.ca/publications/mill18.htm
I'm already aware of the above. And my response is that if anything
classifies a virtual function as one that should be private or
protected, that *anything* shouldn't be placed in the virtual function
at all. Place private parts in private member functions instead.
Needless to say, that can't always lead to a viable solution.

Dec 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

62
3393
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously bloats quickly for each new vtable needed. Execution slows down considerably as well. You can work around this by using interfaces referemnces which have a pointer to the object and a pointer to an external function lookup table. This technique...
11
4368
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
37
4179
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
12
21241
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
10
7315
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
14
4210
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
17
3547
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.