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

Home Posts Topics Members FAQ

static function, not a member?

Hi,

I came across some sample code for using a third-party API, and the code
in it had several free-standing (i.e., non-member) functions, but strangely,
these were declared as 'static'. My compiler tells me that the function has
no prototype, which is weird, because there *is* a prototype in the header
file. I was not able to find in my books any reference to a static function
that was not a member function. The closest info I could find (via google)
was something about making the symbol "local", and a suggestion that this
was deprecated in C++.

Can someone tell me what it means to have the static keywoard in front
of a non-member function, and whether it can be safely removed for my use?

Thanks,
Howard

Jul 22 '05 #1
4 1877
"Howard" <al*****@hotmai l.com> wrote in message
news:S0******** **********@bgtn sc05-news.ops.worldn et.att.net
Hi,

I came across some sample code for using a third-party API, and
the code in it had several free-standing (i.e., non-member)
functions, but strangely, these were declared as 'static'. My
compiler tells me that the function has no prototype, which is weird,
because there *is* a prototype in the header file. I was not able to
find in my books any reference to a static function that was not a
member function. The closest info I could find (via google) was
something about making the symbol "local", and a suggestion that this
was deprecated in C++.

Can someone tell me what it means to have the static keywoard in
front of a non-member function, and whether it can be safely removed
for my use?

It means that the function is only available within the translation unit
within which it is defined.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
Howard posted:
Hi,

I came across some sample code for using a third-party API, and the
code
in it had several free-standing (i.e., non-member) functions, but
strangely, these were declared as 'static'. My compiler tells me that
the function has no prototype, which is weird, because there *is* a
prototype in the header file. I was not able to find in my books any
reference to a static function that was not a member function. The
closest info I could find (via google) was something about making the
symbol "local", and a suggestion that this was deprecated in C++.

Can someone tell me what it means to have the static keywoard in
front
of a non-member function, and whether it can be safely removed for my
use?

Thanks,
Howard


EXAMPLE 1

void Horse(void)
{
static int j = 7;

j +=2 ;
}

int main(void)
{
Horse();
Horse();
Horse();
Horse();
Horse();

// The variable j is global, but can only be accessed from
// within the Horse function. Right now, it's value is 17.
}
EXAMPLE 2

static int hello = 45;

int main(void)
{
;
}

The global variable hello cannot be accessed from outside the current CPP
file.

Jul 22 '05 #3
On Mon, 21 Jun 2004 15:29:03 GMT, JKop <NU**@NULL.NULL > wrote in
comp.lang.c++:
Howard posted:
Hi,

I came across some sample code for using a third-party API, and the
code
in it had several free-standing (i.e., non-member) functions, but
strangely, these were declared as 'static'. My compiler tells me that
the function has no prototype, which is weird, because there *is* a
prototype in the header file. I was not able to find in my books any
reference to a static function that was not a member function. The
closest info I could find (via google) was something about making the
symbol "local", and a suggestion that this was deprecated in C++.

Can someone tell me what it means to have the static keywoard in
front
of a non-member function, and whether it can be safely removed for my
use?

Thanks,
Howard


EXAMPLE 1

void Horse(void)
{
static int j = 7;

j +=2 ;
}

int main(void)
{
Horse();
Horse();
Horse();
Horse();
Horse();

// The variable j is global, but can only be accessed from
// within the Horse function. Right now, it's value is 17.
}
EXAMPLE 2

static int hello = 45;

int main(void)
{
;
}

The global variable hello cannot be accessed from outside the current CPP
file.


You misread the question. It dealt with static _functions_, not
static objects.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #4
"Howard" <al*****@hotmai l.com> wrote in message news:<S0******* ***********@bgt nsc05-news.ops.worldn et.att.net>...
Hi,

I came across some sample code for using a third-party API, and the code
in it had several free-standing (i.e., non-member) functions, but strangely,
these were declared as 'static'. My compiler tells me that the function has
no prototype, which is weird, because there *is* a prototype in the header
file. I was not able to find in my books any reference to a static function
that was not a member function. The closest info I could find (via google)
was something about making the symbol "local", and a suggestion that this
was deprecated in C++.

Can someone tell me what it means to have the static keywoard in front
of a non-member function, and whether it can be safely removed for my use?

Thanks,
Howard

When you add a static keyword in front of a function definition, it
means that the definition of the function would be limited to that
very particular translation unit or that very particular file.
e.g. if your header file includes a lot of functions that you want
your client to use, it is but natural that you do not want to limit
your coding to those very particular functions so you bring in static
functions whose 'usablility' would be reserved to that very particular
translation-unit / file itself only.

--
ISA
isingh AT acm DOT org
Jul 22 '05 #5

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

Similar topics

5
9387
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. what does a private static member function mean? Thaanx in advance for any advice Rgds,
29
4413
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
11
4620
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
4
1960
by: raghavendra | last post by:
Hi , A static member can be accessed only by another static method....but the vice-versa is not true....Can anyone pls explain me the logic behind this... Also in a project, if we have too many static members and methods, will it cause a problem?? thanx for ur comments regards, Raghavendra Mahuli
8
1838
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances' handles) instances? Thanks in advance for any help
15
6596
by: Samee Zahur | last post by:
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...
5
651
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
7
3041
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class is a helper Singleton class, and is created & deleted as and when required) - Where does the static pointer point to when every single instance of the class is deleted. I presume it'll be dangling pointer- as the code seg to which it's
11
3846
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
5
2507
by: Michael Oswald | last post by:
Hello all, I'm working on a project where I came across some situations, where the GUI library works with normal C callbacks. The code has been implemented by many people, so I came across different versions of callbacks (see the code below for 3 variants). Variant1: a normal static member function is used to redirect the callback to a member function Variant2: a static member function is declared, but at the definition the
0
9687
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
10482
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...
0
10251
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...
0
10027
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
9072
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
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
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
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.