473,837 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Friend vs. Static

Question: How do friend functions and static member functions differ in
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and either has access
only to static members of the class (ie. assuming no object of the
class is in scope - neither by arguments recieved nor by local
declarations). Any static member function like this:

//accessing static member i
static void cls::func(int funny){i=funny; }

would perform functions very similar to this function, assuming it
is a friend of cls:

//accessing static member i
void func(int funny){cls::i=f unny;}

So I'd like to know more about the differences between the two uses.
Differences that I have come to think of:
statics can be hidden behind access priveleges, while anyone may call a
friend.
Statics access members with a slightly cleaner syntax :)
Many classes may grant friendship to the same function, not possible
with statics.
Friendship is not inherited to derived classes - statics are.
And probably (I'm not sure though) static members may become virtual,
supporting run-time polymorphism.

Now I just jotted these points down from five-minutes' thought, so it
would be nice if someone went through it to see if they are right, and
if some could be added to the list.

And another question ...
I've had numerous functions being declared static in my code while
their purpose really is being friends - reason: they needed to be
hidden by a protected privilege! This even happenned when my class
didn't even have a static member ... quite bewildering for someone
trying to read my codes! Can anyone tell me what is a nice way of
getting around this? (I have even hoped that one day static and friend
would become synonymous ... LOL)

Samee

Samee

Jul 23 '05 #1
15 6600
"Samee Zahur" <sa*********@gm ail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com
Differences that I have come to think of:
statics can be hidden behind access priveleges, while anyone may call
a friend.
Not necessarily. A private member function of class A can be made a friend
of class B.
Statics access members with a slightly cleaner syntax :)
Many classes may grant friendship to the same function, not possible
with statics.
It is possible for classes B, C, D... to grant friendship to a static
function in A, though of course the static function then gets access because
it is a friend, not because it is static.
Friendship is not inherited to derived classes - statics are.
Yes, but if you wanted to access a member variable in a derived class, then
a static function defined in the base class won't be any use unless you
anticipated this in defining the static function. By contrast, you can
define the friend function after the derived class has been defined (of
course, you could also define a new static function in the derived class).
And probably (I'm not sure though) static members may become virtual,
supporting run-time polymorphism.


Static functions cannot be virtual. Since they are not bound to any class
object, the class within an inheritance hierarchy to which an object belongs
cannot be used to select between different static functions.
--
John Carson

Jul 23 '05 #2
I think that the fundamental point is that any function at all can be made a
friend --- non-member functions, regular member functions or static member
functions --- and can be made a friend of any class willing to grant
friendship.
--
John Carson

Jul 23 '05 #3
Hi

Friend classes are used to define iterators through containers. IAnd
you can do that with a static member funct.

Nadina

Jul 23 '05 #4
*** You can NOT define iterators with static member functions.

Jul 23 '05 #5
"Nadina" <Na*******@gmai l.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com
Hi

Friend classes are used to define iterators through containers. IAnd
you can do that with a static member funct.

Nadina

No indeed, but the question was about static *functions* vs friend
*functions*, not about static functions vs friend *classes*.

--
John Carson

Jul 23 '05 #6

"Samee Zahur" <sa*********@gm ail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
Question: How do friend functions and static member functions differ in
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and either has access
only to static members of the class (ie. assuming no object of the
class is in scope - neither by arguments recieved nor by local
declarations).
The two concepts are not related. A friend function is a function that has
been granted access to the private and protected members of the class that
declares it a friend. A static function is a member of the class, but one
which is restricted to using the static members of the same class.
Any static member function like this:

//accessing static member i
static void cls::func(int funny){i=funny; }

would perform functions very similar to this function, assuming it
is a friend of cls:

//accessing static member i
void func(int funny){cls::i=f unny;}
This shows a friend function that is similar to a static member function,
but it's certainly not the only way to use a friend function. The friend
function *may* have access to a specific instance of the class.

So I'd like to know more about the differences between the two uses.
Differences that I have come to think of:
statics can be hidden behind access priveleges, while anyone may call a
friend.
Anyone may call *any* free function, regardless of whether it's a friend of
any specific class.
Statics access members with a slightly cleaner syntax :)
Many classes may grant friendship to the same function, not possible
with statics.
Does this matter? They're unrelated concepts. If you want another class to
be able to access your members, you can grant friendship to that class.
Certainly, many classes can grant friendship to a given class, and thus to
its static member functions (as well as non-static member functions).
Friendship is not inherited to derived classes - statics are.
And probably (I'm not sure though) static members may become virtual,
supporting run-time polymorphism.
No, a static function cannot also be virtual. A virtual function requires
an instance, in order to be able to determine its specific run-time type. A
static function, by definition, carries no such run-time information.

Now I just jotted these points down from five-minutes' thought, so it
would be nice if someone went through it to see if they are right, and
if some could be added to the list.

And another question ...
I've had numerous functions being declared static in my code while
their purpose really is being friends - reason: they needed to be
hidden by a protected privilege!
What needed to be hidden? The function itself? If it's a member function,
then why do you say its purpose was to be a friend? Conversely, if it was
intended to be a friend function, why was any access protection needed for
it?
This even happenned when my class
didn't even have a static member ... quite bewildering for someone
trying to read my codes!
Again, access to static members is unrelated to friendship. Granting
friendship grants access to protected and private members. Anyone who can
access a public non-static member can also access a public static member,
regardless of friendship. There is no difference between access to static
and non-static members, as far as friendship goes. Granting friendship
merely bypasses the protected/private access restrictions.
Can anyone tell me what is a nice way of
getting around this? (I have even hoped that one day static and friend
would become synonymous ... LOL)


I'd suggest you read up more on both subjects, as it seems you're confusing
two quite distinct concepts.

-Howard

Jul 23 '05 #7
John Carson's post was specially helpful ... I'd like to know if anyone
can add to this list if possible. As for Howard's post, I think you
misunderstood my question:

I know that the original *concepts* behind the two keywords are
different - but my question was about the similarity/differences
of the ultimate *effects* these keywords have ... hence the example
I gave on func ... neither has a this pointer or any instance
associated with the function call, but they both can access private
members of any associated class object in their scopes (introduced
by auto variables or arguements). In practical life, we often need to
use features of a language in ways very different from the original
concepts behind those features ... e.g. in a recent post in
comp.std.c++
I used inheritence to achieve the effect of a typedef!! I didn't have
a choice. So I'm more concerned here about what the differences of the
effects of the keywords are - I already know that their original
concepts are different.
What needed to be hidden? The function itself? If it's a member function, then why do you say its purpose was to be a friend? Conversely, if it was intended to be a friend function, why was any access protection needed for it?


I believe an example would clear this up:
class base{
int i;
protected:
static void addto(base& a,const base& b){a.i+=b.i;}
};

Now let's say that
I want addto to be accessible to any class derived from base, and
no one else. With my limited skills, I really couldn't find a simpler
way to do something like this. I can't turn it into a member function,
since that will either pass a redundant this pointer to it, OR make
it useless. I cannot make it a simple "outside" function and declare
it as a friend, since that would make it publicly visible. Now the
function addto serves suits the concept behind the keyword friend, but
I just can't use it here ... not simply, anyway. So I have to declare
it a static, even though there is no static member here. I'd like it
if you can clean up this code to something less confusing - but I have
so far failed in that.

Although it may not be much in comparision with your expertise, but
after almost 6 years since my first introduction to C++, I doubt if
further reading can help me - with my limited patience, I'm more likely
to skim through the matter *thinking* that I already know this stuff.
The best I can do is learn from this group here :(

Samee

Jul 23 '05 #8
Samee Zahur wrote:

I believe an example would clear this up:
class base{
int i;
protected:
static void addto(base& a,const base& b){a.i+=b.i;}
};

Now let's say that
I want addto to be accessible to any class derived from base, and
no one else. With my limited skills, I really couldn't find a simpler
way to do something like this. I can't turn it into a member function,
since that will either pass a redundant this pointer to it, OR make
it useless.
Why do you think the "this" pointer is any more or less redundant than
passing a reference to "a"? You want to operate on two objects--
somehow you need to specify them.

I cannot make it a simple "outside" function and declare it as a friend, since that would make it publicly visible. Now the
function addto serves suits the concept behind the keyword friend, but
I just can't use it here ... not simply, anyway. So I have to declare
it a static, even though there is no static member here. I'd like it
if you can clean up this code to something less confusing - but I have
so far failed in that.


Write a member function which is invoked as a.add(b):
void add(const base& b) {i += b.i;}
Jul 23 '05 #9
Ok ... you are stepping into the exactly SAME trap I did ...hehehe

If I write it up like a member function like that, I would be able to
call obj1.add(obj2)
from a derived member if and only if obj1 is of the derived type -
while I would be able to call add(obj1,obj2) no matter what the types
are ... derived or base!

See the difference?

Samee

Jul 23 '05 #10

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

Similar topics

8
17883
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
1
3784
by: Mike - EMAIL IGNORED | last post by:
Are static friend functions allowed? I tried this today with gcc-3.2 without success. The function was not a class member. The friend statement was interpreted as meaning "extern", and this was reported incompatible with a previous static declaration. Chapter and verse would also be helpful. I looked around but didn't find anything.
8
1901
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a (factory) class Fa. I wanted to make that only F can call new A
12
3259
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always private. I have placed "friend void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to execute CA::Run_A(), but compiler shows an error saying that it is the violation to access private function. I don't understand why because friend...
5
1645
by: Gianni Mariani | last post by:
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1 dies complaining about somthing <unknown>. Is this valid ? More to the point, is there any way of doing this that is supported across all 3 compilers ? I want a template to take a parameter and make it it's friend. <code>
9
5524
by: Ivan Mascovich | last post by:
Previous posts (and compilers) confirm that class X { friend void Y() ; } ; does not match namespace
8
8938
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
19
2727
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use public data members. Don't use friends, except to avoid or . Consider the following scenarios: Scenario 1:
4
1370
by: Mike Peretz | last post by:
I am wondering if anyone out there is trying to use friend with C#, simular to the way C++ handles it. I wrote a blog about it and I wonder if someone can give me some feedback. http://mikeperetz.blogspot.com/2008/05/using-friends-with-c-or-should-i-say.html
0
9851
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
10583
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
10639
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
10286
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
9419
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
7824
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
7012
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();...
2
4059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
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.