Solved

What do u mean by static int f(x)

Posted on 2001-06-22
5
268 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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