CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2001
    Posts
    31

    [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

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  3. #3
    Join Date
    Dec 2001
    Posts
    31

    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.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured