-
November 22nd, 2010, 01:48 AM
#1
[RESOLVED] Replacement for Java Class (static) methods and variables
Hello,
May I ask how we can create something like java's class methods and variables? These variables and methods do not require us to create an instance of the class and we can directly call them using class name.
public class Geometry
{
public static int status;
private static Geometry geo;
public static synchronized Geometry getGeometry()
{
...
return geo;
}
}
...
Geometry geo= Geometry.getgeometry();
int a=Geometry.status;
Regards,
Mac
-
November 22nd, 2010, 02:29 AM
#2
Re: Replacement for Java Class (static) methods and variables
Code:
public class Geometry
{
public:
static int status;
static Geometry geo;
static synchronized Geometry getGeometry()
{
...
return geo;
}
}
...
Geometry geo = Geometry::getgeometry();
int a=Geometry::status;
For starters, please put your code in CODE tags, not QUOTE tags. At least you put it in something, which is better than nothing, but CODE is even better.
Except for the syntax, it is pretty much the same thing.
In c++, you can "block" public or private, so you just put "public:" at the top, and list all your public members
In C++, static members aren't called like "members" of the class, but as objects "scoped" inside the class. Basically, it just means you have to use "class::myStatic", and not "class.myStatic".
Finally, avoid static public attributes. Those are nothing more than glorified globals, and C++ coders hate globals.
As for private static attributes, depending on your design, they are either good solutions (like for reference counters), or sometimes globals in disguise.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 22nd, 2010, 02:55 AM
#3
Re: Replacement for Java Class (static) methods and variables
Thank you very much.
I used to know C++ before 2002 but now after 8 years it is unfortunate that I have forgotten very basic things. It's a shame.
Using quote has been a mistake.
-
November 22nd, 2010, 09:01 AM
#4
Re: Replacement for Java Class (static) methods and variables
C++ does not have a "synchronized" keyword. The very latest compilers may support the new <thread> header, though, which includes a mutex type that you can construct on the stack at the start of a function; this is effectively the same thing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|