Page 2 of 2

Re: Sorting the code && Storing the files.

Posted: Tue Oct 27, 2015 11:19 am
by K-Bal
I don't have time to address all questions, so I just leave some points about the static keyword ;)

In general, static means that something has a lifetime that extends across the lifetime of the entire run of the program. Static variables occupy memory addresses that lie next to the program code, in contrast to stack and heap variables.

Static Free Functions

Static free functions are functions which have "internal linkage". That means, that you get one copy of the static function for each .cpp file, where you're using it (actually for each translation unit, but that often boils down to the indiviual .cpp files). It gives the compiler more freedom for optimization.

Static Class Functions

Static class functions behave like free functions in the namespace of the encompassing class (there are subtle differences, but they are not relevant for you at the moment). They don't have an implicit "this" parameter, so they don't need an instanced object of the class in order to be called. Like SuperLED already said, you call them like this: ClassName::FunctionName(/*arguments...*/);

Re: Just questions.

Posted: Tue Oct 27, 2015 1:43 pm
by dandymcgee
superLED wrote: Public: Everything can access and change these variables. If you include the class with these public variables, you can just grab them an do whatever with them. Try to avoid this. Only methods should be public (the ones that the outside world should be able to access)
I'm not a fan of the OO-whore approach of wrapping *everything* the light touches with completely meaningless get / set methods. If you have a public set method that does nothing other than set the property it's effectively a public property, so just make it public and save yourself the overhead of a function call. The *only* reason to use an empty get / set wrapper is if you need to change them later (e.g. add validation, side effects, etc.) This would allow you to add validation to setter without changing the syntax in all of the places it's called.

In C/++, if a class has only public properties it's common to use the "struct" keyword instead. The only difference being that a struct's members are public by default, rather than private (like a class).

Just questions.

Posted: Wed Oct 28, 2015 8:08 am
by AronE.
K-Bal wrote:I don't have time to address all questions, so I just leave some points about the static keyword ;)

In general, static means that something has a lifetime that extends across the lifetime of the entire run of the program. Static variables occupy memory addresses that lie next to the program code, in contrast to stack and heap variables.

Static Free Functions

Static free functions are functions which have "internal linkage". That means, that you get one copy of the static function for each .cpp file, where you're using it (actually for each translation unit, but that often boils down to the indiviual .cpp files). It gives the compiler more freedom for optimization.

Static Class Functions

Static class functions behave like free functions in the namespace of the encompassing class (there are subtle differences, but they are not relevant for you at the moment). They don't have an implicit "this" parameter, so they don't need an instanced object of the class in order to be called. Like SuperLED already said, you call them like this: ClassName::FunctionName(/*arguments...*/);
Okay, so I'm honestly confused, I read your post 5 times, watched a video on youtube and got this :

A static method : doesn't need an instance, can be called randomly and runs constantly, for the whole lifespan of the program.(That's the reason why the main method is static)
A non-static method : needs an instance, it's better for the compiler.

Also, I don't think I understand the static variables thing.. What's the point of it and when we set one variable to ,,static" what are the differences from a non-static variable, aside from "being alive" until the program is closed.

I'm not a C/C++ developer, so I tried my best to replace these terms with the ones used in Java.. Please, correct me if my statements above are wrong.

Re: Sorting the code && Storing the files.

Posted: Thu Oct 29, 2015 6:33 am
by bbguimaraes
edit: by the way, what I describe here are static methods. Static members and the static qualifier for variables are entirely different (but somewhat related) things.

Let me give you my take: in c++, static methods are not bound to an instance of a class. Usually, if you have:
class Foo {
    public:
        int x;
        void g() {...}
        static int h() {...}
};
Here Foo::g is not static, so you'd need an instance to call it:
Foo foo;
foo.g();
// This would not work:
Foo::g();
On the other hand, Foo::h is static, so you don't have that restriction:
// This works fine.
Foo::h();
However, since you don't have an instance (i.e. you don't have a "this"), you can't access members of the class.
static int Foo::h() {
    // This is ilegal:
    return this->x;
}
One example of static methods is the "fake constructor", where instead of using a standard constructor, you use a static method with a better name:
class Square {
    // ...
    public:
        // Instead of this:
        Square(Point p0, Point p1);
        // this:
        static Square from_corners(Point bettom_left, Point upper_right);
};
from_points is a static method because it doesn't need an instance (it actually can't have an instance, since that's what it creates).

Re: Sorting the code && Storing the files.

Posted: Fri Oct 30, 2015 5:49 pm
by AronE.
Is Foo::h(); supposed to be an instance? It's C++ so idk... I tried to make a static method and run it, but idk... I'm honestly confused as hell... The access modifiers are difficult to understand, the static thing is even twice as difficult.......

Edit: Is this the whole point of the static variables/methods????

Static method :

Code: Select all

package just_Package;

public class Class2 {

      public static void methodName(){
         String name = "I'm a string!";
    	  System.out.println(name);
      }
}

Code: Select all

package just_Package;

public class Class1 {
      public static void main(String[] args){
    	 Class2.methodName();
      }
}
Static variables :

Code: Select all

package just_Package;

public class Class2 {
      static String name = "I'm a static variable";
}

Code: Select all

package just_Package;

public class Class1 {
      public static void main(String[] args){
    	 System.out.println(Class2.name);
      }
}
Edit2: Also, how is this useful? Have you guys ever needed it?

Re: Sorting the code && Storing the files.

Posted: Mon Nov 02, 2015 8:01 am
by bbguimaraes
Sorry, I didn't realize you were talking about java. Let me try another example of static members. Say you want to track how many objects of a particular class have been created. One way to do it is using a static variable on the class that gets updated on the constructor:
public class Foo {
    public static int n = 0;
    public Foo() {
        n += 1;
        // construct the object
    }
};
The variable n is declared static because to the class itself, so shared by all instances (sorry if the syntax is not quite right, I'm a bit rusty on java, but I think the concept is clear). At any point, you can get the number of instances created (note how we're accessing the variable through the class, not any instance):
new Foo(); new Foo(); new Foo(); new Foo(); new Foo();
System.out.println(Foo.n); // 5