Solved

What do u mean by static int f(x)

Posted on 2001-06-22
5
236 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ranga_etc, how do you know Masterdumbo is asking about static class members?
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now