answersLogoWhite

0


Best Answer

When a function is declared static at file scope, this means the function has internal linkage only. That is, the function is only accessible to the translation unit in which it is declared. This applies to both C and C++. However, we can achieve the same thing in C++ by declaring a (non-static) function in an un-named (anonymous) namespace. Whether this better represents internal linkage or not is merely a matter of taste.

In C++ we can also declare static functions inside a class. Static member functions differ from non-static member functions in that they do not have access to a 'this' pointer; they are local to the class, not to objects (instances) of the class. As such, they can be invoked without having to instantiate an object from the class.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Static functions in C are not the same as static functions in C++. In C, a static function is local (and therefore only visible) to the translation unit in which it is declared. In C++, a static function is better known as a static member function which can be called even if no instance of the class in which it is declared exists, and is visible to any code with access to the class. Static member functions differ from ordinary member functions in that they have no access to the implicit this pointer, since they are not associated with any instance of the class. They are typically used to provide general functionality that is closely associated with the class, but that cannot be applied to any one instance of the class. However, as members of the class, they have private access to the class, which includes access to other static members of the class as well as any instances of the class, which can either be instantiated by the function itself, or passed into the function arguments. However, if a static member function accepts only one instance of a class, then it would probably be better implemented as an ordinary member function (with access to the implicit this pointer) rather than as a static member function.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There is no real distinction required since C is not an object-oriented programming environment and therefore has no such concept as a member function. C++ does, but the only distinguishing factor is that a member function has access to the implicit this pointer, which is never NULL and therefore refers to the current instance of the class. The only exceptions are static member functions, which are scoped to the class as a whole, not to any instance of the class, and therefore have no access to the implicit this pointer (although as members of the class they still have private access to the class).

There are more fundamental differences within the calling convention, of course, but since C has no concept of class, you cannot call C++ member functions in C code. Normal C++ functions, including overloaded functions, can be declared 'extern "C"' to ensure they follow the C calling conventions, but member functions cannot. For the same reason, C++ structs must not have any member methods associated with them if they are to be utilised by C.

Aside from access to the implicit this pointer, functions in C are, by and large, no different to functions in C++, including member functions. However, the type-safety of C++ is much more strictly enforced than in C, thus there are some differences in this respect. Moreover, in C it is not always necessary to declare functions with forward declarations before they are used, but in C++ this rule is strictly enforced.

However, conversely, in some areas the rules are relaxed. For instance, the main function is typically declared as 'int main()' and must therefore return a value, usually via a 'return 0;' statement (typically used to indicate to the caller that the program completed successfully). But in C++, the 'return 0;' is implicit.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A static function in C++ is the same as a static function C. Unlike standard, non-static functions which are accessible outside of the file in which they are declared, static functions are local to the file in which they are declared.

C++ also allows static functions to be declared inside a class. These static member functions are members of the class and have unrestricted access to all other members of the class, including other static members and instance members, but they are not associated with any instance of the class (they can be called even when there is no instance of the class) and therefore they have no access to an implicit this pointer. They are used to provide general functionality that is closely associated with the class, and often make use of the class' static member variables.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A macro function is not a function per se, it is an inline text replacement. Since all macros are preprocessed, the compiler has no access to them and therefore cannot help you ensure they are type safe, let alone bug free; you are completely on your own. So when you have the option to use a macro function or a template function, go with the template function every time. If you need a specific overload that the compiler cannot generate for you from a template function, roll your own overload to cater for those specific instances. It's a small price for piece of mind.

Macros have their uses, of course, but they should only be employed when there is no other option, or where the equivalent C++ code would be deemed overly complex compared to a simpler macro definition.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A static function is a class method that is declared static. Static methods are not associated with any instance of the class and can therefore be called without instantiating the class. As a result, static methods do not inherit a 'this' pointer. However, they have unrestricted access to all static member variables of the class, as well as all other static methods of the class. Instances of the class can also be passed to static methods of the class, by reference or by pointer variable, just as they can be passed to functions that are external to the class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A static class function can only access static members of the class, because there is no implicit "this" parameter. A static non-class function, at file scope, only has visibility at file scope, i.e. it has no external linkage.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Static variables always retain their values even when they fall from scope. That is, a static variable declared local to a function is only accessible to that function, but it will retain its value between calls. Non-static variables are not guaranteed to retain their value when they fall from scope, and are expected to be initialised before each use. Static variables are initialised once and once only, every time the program executes.

For example:

void foo()

{

static int x=0;

++x;

int y = 0;

++y;

}

In the above example, x and y both have local scope (local to the function). However, x is declared static thus it is initialised to 0 as soon as the program starts and is subsequently incremented every time the function foo is called. Thus x effectively maintains a count of all calls to foo. By contrast, y is instantiated anew every time foo is called, is initialised to 0 on each call, and is incremented before returning from the function. Thus y can never be more than 1 in this example. Although both x and y will fall from scope when the function returns (and are therefore inaccessible outwith the function), x will maintain its value between calls because it is declared static. It therefore exists for the lifetime of the program.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A "method" is what Java calls its procedures. C++ has only functions (which return a value) and subroutines (which do not return a value).

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A static int (or a static anything) has file scope, and will thus retain its value even if the function in which it is declared goes out of scope.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between normal function and static function in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between static function and global static function?

'global static'?! There is no such thing.


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


What is the difference between complex permittivity and static dielectric conatant?

What is the difference between complex permittivity and static dielectric conatant?


What is the difference between method and function in java?

A function is a piece of code that can be reused by calling its name while a method is a function that is associated with a class. In Java, functions are usually referred to as static methods.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


What is the Difference between public static void and static public void?

There is no difference between public static void and static public void


What is the difference between static charge and static electricity?

in my own explanation static charge is a reaction between the (+)protons and (-)electrons an the effect of this reaction is called static electricity.


What is the difference between static electricity and static discharge?

i love v a g i n a


Difference between static and dynamic hazards?

lauda


What are the difference between versions and functions?

Version: static reference to a specific entity of a specific instance. Function: dynamic reference to existing algorithm to perform or execute.


What is the difference between static and dynamic fighting?

Static stays the same and dynamic is always different.