Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A static object

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
0
0
SG
Can someone tell me the effects of the static object on the statements that are numbered?

SomeCode.cpp
Code:
#include "../RSS/RSSInfo.h"

using RSS::RSSInfo ;

static RSSInfo fxnOne(void) ; // ??? (1)
static RSSInfo& fxnTwo(void) ; // ??? (2)

static RSSInfo rss_1 ; // ??? (3)

int main()
{
    static RSSInfo rss_2 ; // ??? (4)

    return 0 ;
}

Or in a more general question, what are the effects of the static keyword on an object, object reference and object pointer. Can someone enumerate the key differences of static between C and C++ (excluding static member functions). What is the effect of static in a global function?

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
1. Not really any effect
2. Same
3. Global Data to this file only
4. Not really any effect.

For # 4, if declared somewhere else then main, the value remains the same every time you come back to the function.

Ex.

int myFxn()
{
static int number_of_calls = 0;
number_of_calls++;
cout<<"Number of calls to myFxn:"<<number_of_calls<<endl;
}

Matt
 
Making a global function static (cases #1 and #2) prevents the compiler from exporting the symbol in the object file. So the function cannot be called from a function in another source file, without calling it indirectly (i.e. pointer to function).
 
1. fxnOne() function has internal linkage (known in this module only w/o conflicts with other definitions). It's a declaration only, fxnOne() must be defined in this module (if used)...
2. The same as above...
3. Declaration (and definition) of a variable rss_2 denotes an object of class RSSInfo with default constructor; known only in this module. In this context (var dcl) static keyword defines static storage duration (not a linkage as for functions). This object will be created logically before main() execution.
4. The same as case 3 but rss_2 is known only in the main() body.
 
Also, static functions mean each call to the function will use the same activation record, which means that it is possible for the values of local variables to change. The return by reference on the second one is returning the object in the function, so any changes made to the object passed out will have an effect on subsequent calls to the functions.

Code:
#include <iostream>
using namespace std;

static int f1()
{
   static int i = 0;
   i += 1;
   return i;
}

static int& f2()
{
  static int i = 0;
  i += 1;
  return i;
}

int main()
{
   for(int i = 0; i < 5; ++i)
   {
      int x = f1();
      cout << "x: " << x << " ";
      int y = f2() += 5;
      cout << "y: " << y << endl;
   }
   return 0;
}

yeilds the output:
Code:
x: 1 y: 6
x: 2 y: 12
x: 3 y: 18
x: 4 y: 24
x: 5 y: 30
 
Also, static functions mean each call to the function will use the same activation record
Alas, it's a wrong statement.
Any call of any function uses a different activation record (or a stack frame). You must declare locals as static if you want to save its values between the function calls (in static functions too, as in this snippet)...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top