473,378 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

static and non-static functions

What is the difference between those two. Is there gonna
be some difference in app execution for static functions?
What difference does it make?

UF
Jul 23 '05 #1
4 1663
Ufit wrote:
What is the difference between those two. Is there gonna
be some difference in app execution for static functions?
What difference does it make?


Static functions do not require (nor do they have, internally)
an instance of the class to be called. Non-static, vice versa,
do require and provide (through 'this' pointer) an instance of
the class for which they are called.

I am not sure how to answer the second question. The third
question is too vague.

V
Jul 23 '05 #2
(Hopefully this won't show up twice; if it does, I apologize. I'm
reasonably sure Google dropped my first post though, so here goes:)
What is the difference between those two. Is there gonna
be some difference in app execution for static functions?
What difference does it make?


Victor answered your questions for member functions in classes. (If
not, consult Google or your C++ book; anything that talks about classes
should talk about this forn of static-ness.) However, there can be free
(non-member) functions that are static too, and static in this context
means something entirely different.

Normally you can use a function defined in another compilation unit by
using the extern keyword:

File1.cpp
int foo() { ... }

File2.cpp
extern int foo() { ... }

int main() { foo(); }
However, if you have functions that you don't want to be able have this
done to (sorta similar to the idea of private member functions), you
can make them static:

File1.cpp
static int foo() { .. }

Now a linker error will be generated because extern int foo() can't
pull it in.

This use has been superceded in C++ by the unnamed namespace. File1
(the second version) should be refactored as:

File1.cpp
namespace
{
int foo() { ... }
}

Jul 23 '05 #3
Victor Barzarov answered your question for functions are class members,
but there can also be non-member static functions. (If you have other
questions consult Google or your favorite C++ book.)

These, along with any global static variables, restrict the scope of
the function to just that file. In other words, other compilation units
cannot use extern to get to those functions.

File1.cpp:
static void foo() { ... }

File2.cpp
void bar() { ... }

File3.cpp
extern void foo() { ... } // illegal
extern void bar() { ... } //okay
This use has been superceded in C++ by the unnamed namespace. File1.cpp
should be refactored into:

File1.cpp
namespace
{
void foo() { ... }
}

Jul 23 '05 #4
I assume that you are talking about standalone functions and not class
member functions.
Is there gonna be some difference in app execution for static functions?


Can be. One compiler I know of for Intel "16-bit large code models" will do
a certain optimisation knowing that a function is static to the translation
unit. That makes it fractionally faster.

Stephen Howe
Jul 23 '05 #5

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.