The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
c++ equivalent to java's static class members
Discuss c++ equivalent to java's static class members in the C Programming forum on Dev Shed. c++ equivalent to java's static class members C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 2nd, 2002, 02:35 PM
|
|
Junior Member
|
|
Join Date: Feb 2002
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
c++ equivalent to java's static class members
Hi,
in java, it's possible to declare a class member - be it a function or a variable - as static, thus creating a member that, e.g, holds the same value regardless of instantiation. It is not a constant though, as it stays accessible for all instances of the class which, in case it is a variable, may change it.
I am learning c++ and bought a book which doesn't mention anything like static class members.
Is there or is there not an equivalent construct in c++?
any answers appreciated
costas
|

March 2nd, 2002, 04:07 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Yes there is and it's declared using the keyword static as well. Here's some sample code using a static variable.
Code:
#include <iostream>
class test {
static int counter;
public:
int getcount() { return counter;}
test();
};
int test::counter = 0;
test::test() {
counter++;
}
int main(void) {
test foo, bar;
cout << foo.getcount() << "\n";
}
|

March 2nd, 2002, 04:51 PM
|
|
Junior Member
|
|
Join Date: Feb 2002
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
hey, thanks a lot.
so, it's more or less identical to the java syntax. I guess, it's the same with functions?
BTW, this makes me feel bad about the quality of the book I bought. I mean, it does focus mainly on developement under Linux with c++, but it explains the syntax in detail...or so I thought.
one more time, thanks, however
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|