Solved

What do u mean by static int f(x)

Posted on 2001-06-22
5
264 Views
Last Modified: 2010-04-02
I want to know why do we declare a function in the following format static int f(int x);

static int f(int x)
{
...

....
}

why do we give the return type as static?






0
Comment
Question by:Masterdumbo
5 Comments
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6217575
Two possibilities, i) a standalone, normal function:

static in this instance, means, effectively that the function is private to the translation unit (.cpp file) in which it is defined.  That is only code in the same file unit may call it.

It's name will also not conflict with a similar function in any other translation unit, so you could have another function with the same name and signature in another file without multiply defined symbols.

Possibility ii) a class member function:

a completely different meaning for static (helpful!), in this case it means that there is one instance of the function per class (not per object), i.e. it is a class method.  The method may be called without an instance of the class (Class::method()).

0
 

Accepted Solution

by:
RanganathanVenkatakrishnan earned 100 total points
ID: 6218066
static int F(int x) means as foolows

1. A function is declared static so that it can be accessed without creating the instance of the Class.
2. To access only static data members and functions.

3. The return type is not static.

eg
 class A
{
static int x;
int z;
public:
A();
static F(int y)
{
x = y;
z = y // not allowed
}
};

void main()
{
A a; // not required

A::F(20);

}

0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6218335
Ranga_etc, how do you know Masterdumbo is asking about static class members?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6218406
RanganathanVenkatakrishnan,

please read the guidelines for comments and answers.  Your contribution does not constitute a proper answer.

> To access only static data members and functions.

oh, really, how do you account for this then:

class X
{
    int x;
public:
    static f(X* obj) { obj->x = 2; }
};

> 3. The return type is not static.

what exactly do you mean by this?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6218740
Hi RanganathanVenkatakrishnan , welcome to Experts-Exchange.
All of the experts here, for the most part have learn from other experts as to the proper etiquette for posting answer.

Proper etiquette for posting Answers:
1.  An answer should not be posted as an answer, if other experts have previously posted possible answers as comments, and/or have already made contributions to the question.

2.  An answer should not be posted as an answer, if it has a question.
Example: (Is this for windows? If so, here?s the answer)

3.  An answer should not be posted as an answer, if it has the phrase ?I think?.
Example:  (I think this should work)

4.  Full complete schoolwork answers must not be posted. (EE Policy)

5.  If you inadvertently post an answer after anther expert just post the same answer as a comment, you should promptly notify the questioner of your accidental post, and ask the questioner to fairly award the question to the expert whom fully/best answered the question first.  You might consider also apologizing to the other expert.

If you feel you have an answer, you can post it as a comment, and the questioner can convert your comment to an answer if he/she thinks it?s correct or deserving of the awarded points.

There are many experts who never post answers as answer.  Instead, they post their answers as comments.
If you read the following link, you?ll see why this is the preferred method for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp


Hi Masterdumbo:
Feel free to click the [Reject Answer] button near (Answer-poster's) response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other experts.  Later, you can click the [Select Comment as Answer] button on any response.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question