473,799 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

private member functions

Is there ever a good reason to declare private non-virtual member
functions in a class definition?

As far as private virtual function are concerned, my understanding is
that, if you have a (public) function which calls an private virtual
function in the base class, the behavior can be changed by overriding
the private virtual function in the derived class (but leaving the
calling function unchanged). This at least seems useful.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #1
5 11315
That all depends. and it's most a matter of preference.
One example that comes to mine is:
lets say that you have methods A and B which do something that are kinda
similar, methods could be virtual or may not besides the point. so you as a
good programmer will isolate the similarities and put in method C. Now
calling method C out of context could be disasterus and also no one other
than this on class will ever use it. If it was me, it would be a private
method. You might say why not protected, I guess there is still a danger of
someone using it in a derived class.

Ali R.

"David Rubin" <bo***********@ nomail.com> wrote in message
news:3F******** *******@nomail. com...
Is there ever a good reason to declare private non-virtual member
functions in a class definition?

As far as private virtual function are concerned, my understanding is
that, if you have a (public) function which calls an private virtual
function in the base class, the behavior can be changed by overriding
the private virtual function in the derived class (but leaving the
calling function unchanged). This at least seems useful.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown

Jul 19 '05 #2
"Ali R." wrote:
"David Rubin" <bo***********@ nomail.com> wrote in message
news:3F******** *******@nomail. com...
Is there ever a good reason to declare private non-virtual member
functions in a class definition?

[snip - top-posting fixed] lets say that you have methods A and B which do something that are kinda
similar, methods could be virtual or may not besides the point. so you as a
good programmer will isolate the similarities and put in method C.


What I was really after was whether to declare a non-virtual private
method in the class interface or to declare a static function in the
implementation translation unit. I guess the major difference is that a
member function knows about the calling instance, and has access to
other private data. So, it depends on what the function does.

Thanks,

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #3

"David Rubin" <bo***********@ nomail.com> wrote in message
news:3F******** *******@nomail. com...

What I was really after was whether to declare a non-virtual private
method in the class interface or to declare a static function in the
implementation translation unit. I guess the major difference is that a
member function knows about the calling instance, and has access to
other private data. So, it depends on what the function does.


Certainly. Your question is a little confusing. What issue are you
concerned about? Non-virtual? Static? Private function? Private data?
Do you have a specific example in mind?
Jul 19 '05 #4
David Rubin wrote:
"Ali R." wrote:

"David Rubin" <bo***********@ nomail.com> wrote in message
news:3F****** *********@nomai l.com...
Is there ever a good reason to declare private non-virtual member
functions in a class definition?

[snip - top-posting fixed]
lets say that you have methods A and B which do something that are kinda
similar, methods could be virtual or may not besides the point. so you as a
good programmer will isolate the similarities and put in method C.

What I was really after was whether to declare a non-virtual private
method in the class interface or to declare a static function in the
implementation translation unit. I guess the major difference is that a
member function knows about the calling instance, and has access to
other private data. So, it depends on what the function does.


I don't like adding private member functions to a class. In
my experience private methods are usually either internal
implementation details, helper methods for the class, or
encapsulate some common code and I don't think this sort of
stuff belongs in the class header.

Personally I'd consider creating an auxillary class for
those private methods. It keeps the implementation details
out of the primary classes interface, so you can fiddle
about with it without causing client code to recompile, and
frequently the 'private methods' are actually useful outside
of the class anyway. In a large system you can often find
that a method you are about to write is already lurking
around as some private method in some obscure class.

In some cases though a private method makes sense, for
example if the method is making non-trivial usage of the
classes internal data, but I'd consider the auxillary class
first.

Jul 19 '05 #5
In article <3F************ ***@nomail.com> , bo***********@n omail.com
says...
Is there ever a good reason to declare private non-virtual member
functions in a class definition?
Yes -- semi-regularly, AAMOF (YYMV, of course). One that comes up
fairly frequently is having a ctor that you'd like to have call another.
Unfortunately, that doesn't work; the usual cure is to write a private
function for use by both ctors.
As far as private virtual function are concerned, my understanding is
that, if you have a (public) function which calls an private virtual
function in the base class, the behavior can be changed by overriding
the private virtual function in the derived class (but leaving the
calling function unchanged). This at least seems useful.


It is. Just FWIW, the function is often a friend, and frequently an
overloaded operator.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #6

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

Similar topics

3
21394
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG rajeshgarg@opussoft.com
34
3116
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is this in any way used to create singletons. Can someone say how? Cheers, Andy
6
2179
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >> (istream &, abc &); private:
3
2327
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke a private method of that same class?
27
6728
by: Derek | last post by:
The company where I work uses a naming convention that I have never used before. They use mixed-case letters for public member functions, but lower-case with underscores for the rest, like this: class Foo { public: void somePublicMemberFunction(); protected:
3
2070
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is what I would call relatively large (about 1Mb). I have written methods for this class so that the struct can be correctly filled with the corerct data and certain parts of the struct can be extracted. But the problem I face is that I now have...
4
2298
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
23
2612
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
11
6449
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
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,...
1
7564
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
6805
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
5463
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
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.