Advertisement

"static" stuff in C++

Started by April 12, 2002 03:22 PM
12 comments, last by Tac-Tics 22 years, 5 months ago
I''m a little confused on how to translate static class fields from Java into C++. I think it has to do with the fact you can''t initialize them to a default state like you can in Java. For example, how would you port this code snip in Java to C++?
  
public
class Manager
{
     static private Manager staticObj = new Manager(3, 4);
     
     private int dataOne;
     private int dataTwo;

     public Manager(int dataOne, int dataTwo)
     {
         this.dataOne = dataOne;
         this.dataTwo = dataTwo;
     }
     
     static Manager getTheStaticObj ()
     {
         return (staticObj);
     }
}
  
Obviously, this is just an example class. I just need some help on how to automatically initialize the staticObj field (and how do I handle its disposal since pooey C++ makes me take out the garbage myself)? Any help = many/much thnx

      class Manager {    private:        int dataOne, dataTwo;    public:        inline Manager(int _dataOne, int _dataTwo) :            dataOne(_dataOne), dataTwo(_dataTwo) {        }        static inline Manager &getTheStaticObj(void) {            static Manager staticObj(3,4);            return staticObj;        }};  

You may want to make the constructor private/protected too, since it doesn't look like you want people creating their own Manager's. Edit: Made it look nicer and added the missing semicolon on the end of the class declaration. I decided that returning a reference is better.


[edited by - Null and Void on April 12, 2002 4:48:08 PM]
Advertisement
You can have your garbage collection exactly like in your Java programs by doing one thing. Never use delete or free. The specs for Java say that garbage collection is done automatically, but I have yet to see a gar. coll. scheme that actually works in all cases (you can almost always find a way to make it not work). Most Java implementations do absolutely nothing with new variable until the program ends.
So pooey on Java.

Ah, thanks. I still like it better in Java tho =-) Much clearer syntax. Also, a few more questions:

1) What exactly does the "inline" keyword do for you? I know it is benificial for shorter methods, but that''s it.

2) Why do some people use (void) instead of (void)? Is it just a habit from old-skool C or something?

3) Why is that evil :var(initVal) syntax legal? They do that alot in the book we use in my CompSci class and it bugs me! It''s hard to read, inconsistent, and it misleads you by forcing you to include an empty set of brakets. Does it have any real effect on the program preformance? Sry, it''s just one of those seemingly redundant things that drives me nuts =-)

Thanks again for the help
Hehe. Actually, I really don''t care if GC works at all as long as I don''t have to think about it =-) It''s very similar to real life GC. The garbage men come and take it away (after I set it to ''null'') and then god knows where it ends up (my guess is they shoot it into space). But the point is, I don''t have to deal with it =-P That''s the beauty of abstraction.
with inlining, the function is placed directly into code, which removes overhead from calling the function.

if you mean using (void) instead of (), its just a habit from C. in C, a function that used () could take any parameters, while a void one could not. in c++ this is done by (...). however, using void might make your code a bit clearer

Advertisement
quote: Original post by Tac-Tics
1) What exactly does the "inline" keyword do for you? I know it is benificial for shorter methods, but that''s it.

It places the code from the function in the place where it''s called. Since those functions are actually doing next-to-nothing, this results in less overhead and smaller executables.
quote: Original post by Tac-Tics
2) Why do some people use (void) instead of (void)? Is it just a habit from old-skool C or something?

In C++ is doesn''t matter. I like putting void there, since I learned C first, and I still write C code often (I don''t want to start forgetting to do it ). It''s a personal preference and a habit, in other words.
quote: Original post by Tac-Tics
3) Why is that evil :var(initVal) syntax legal? They do that alot in the book we use in my CompSci class and it bugs me! It''s hard to read, inconsistent, and it misleads you by forcing you to include an empty set of brakets. Does it have any real effect on the program preformance? Sry, it''s just one of those seemingly redundant things that drives me nuts =-)

It makes the relationship between the parameters and the member variables more explicit. It also allows you to mess with base class constructors (if you have any), so it''s best to have a uniform interface for initialization.

quote: Original post by Tac-Tics
1) What exactly does the "inline" keyword do for you? I know it is benificial for shorter methods, but that''s it.

In the example given, and in many cases, it allows you to use 7 extra keystrokes.
quote:
3) Why is that evil :var(initVal) syntax legal?

It''s called an initialiser list. Sometimes it''s the only syntax available for what you want to do (initialising base via non-default ctor, for instance).
quote:
It''s hard to read, inconsistent, and it misleads you by forcing you to include an empty set of brakets.

If you are including an empty set of brackets, you can omit it as the default ctor will be run anyway.
When you define a member function in a class (as opposed to just providing a prototype and define it later outside of the class), that function automatically becomes inline no matter how big it is. Therefore, in above examples, the inline keyword is redundant.
quote: Original post by Tac-Tics
I'm a little confused on how to translate static class fields from Java into C++. I think it has to do with the fact you can't initialize them to a default state like you can in Java.

struct A{  static int classA_int;};int A::classA_int = 0; // initialization to default state 

Any further questions? I realize that I provide a trivial example, but the point is that static variables are initialized at file scope. I've heard that the Java-style initialization is also valid, but never bothered to try it out.

[Edit: Closed tag.]

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!

[edited by - Oluseyi on April 12, 2002 8:32:36 PM]

This topic is closed to new replies.

Advertisement