LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-16-2006, 09:10 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
C++: difference between static method variable and static class variable?


hiho@ll

what i have:
on one hand a static method variable within a class
on the other a static class variable within a class

what i thought it should be:
the static class variable depends on the class not on a object instance, this means i could do something like a "instance" counter (i think everybody knows such examples)
the static local variable in my method can only be accessed through my method and it doesn't "die" between 2 function calls, BUT it has nothing to do with static class variables so a instance counter isn't possible because if i create another object the static method variable is initialized newly
so the static method variable is instance bound

code:
Code:
#include <iostream>
using namespace std;

class StaticTest
{
public:
    void SetValue(int Value);
    int GetValue(void);
private:
    int summe;
};

void StaticTest::SetValue(int Value)
{
    static int counter = 0;
    counter += Value;
    summe = counter;
}

int StaticTest::GetValue(void)
{
    return summe;
}

int main()
{
//3 Objekte:
StaticTest *a, *b, *c;
    a = new StaticTest();
    b = new StaticTest();
    c = new StaticTest();
    //1. Object
    a->SetValue(1);
    a->SetValue(1);
    a->SetValue(1);
    cout << "Value of a is " << a->GetValue() << "\n";
    //3
    delete a;
    //2. Object
    b->SetValue(1);
    //3. Object
    c->SetValue(1);

    cout << "Value of b is " << b->GetValue() << "\n";
    //4
    cout << "Value of c is " << c->GetValue() << "\n";
    //5
    return 0;
}
the output of the above code is:
Value of a is 3
Value of b is 4
Value of c is 5

what i thought it should be:
Value of a is 3
Value of b is 3
Value of c is 3

question:
what is the difference between a local static variable and static class member?
i mean the only difference is (as i see for now), that i can't access the local variable directly
but:
Code:
class{
 public:
 void mymethod(){
   static int counter;
 }
 private:
  static int othercounter;
};
what's the difference in the above code? does it make sense that there is no difference? (well i think there is no difference)
or maybe a local static variable within a method is only a "relict" from C?
 
Old 01-16-2006, 09:43 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
If you compile that code using the -S option it'll show you why it works the way it works. When the static is declared it creates the variable as some named mangled form of StaticTest->SetValue->counter. Any subsiquent attempt to declare a static variable called counter from SetValue in a StaticTest will look to see if it is already there, and use the one present. This is a result of the way statics have always worked. The solution is to declare counter as a private class member variable (non-static).
 
Old 01-16-2006, 09:45 AM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Can you access the local variable (*any* local variable, static or automatic) from outside the function?

Can you access a public member variable from outside the method? (Hint, the answer is "yes")
 
Old 01-16-2006, 10:08 AM   #4
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
so there is no difference
but why does it exist then?
this would mean static method variables are useless (for c++), not?
[EDIT]
note: java has no static method variables! so the question is: why should it make sense to have them in c++?
[/EDIT]

Last edited by Thinking; 01-17-2006 at 04:31 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
extern static variable alaios Programming 9 09-11-2005 04:11 AM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
What is a static variable? saiz66 Programming 7 09-30-2004 05:17 PM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM


All times are GMT -5. The time now is 05:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration