Sorting the code && Storing the files.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Re: Sorting the code && Storing the files.

Postby K-Bal on Tue Oct 27, 2015 12:19 pm

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...*/);
User avatar
K-Bal
ES Beta Backer
ES Beta Backer
 
Posts: 719
Joined: Sun Mar 15, 2009 4:21 pm
Location: Germany, Aachen

Re: Just questions.

Postby dandymcgee on Tue Oct 27, 2015 2:43 pm

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).
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Just questions.

Postby AronE. on Wed Oct 28, 2015 9:08 am

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.
AronE.
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 31
Joined: Mon Sep 14, 2015 9:04 am

Re: Sorting the code && Storing the files.

Postby bbguimaraes on Thu Oct 29, 2015 7:33 am

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).
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
 
Posts: 324
Joined: Wed Apr 11, 2012 5:34 pm
Location: Brazil

Re: Sorting the code && Storing the files.

Postby AronE. on Fri Oct 30, 2015 6:49 pm

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?
AronE.
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 31
Joined: Mon Sep 14, 2015 9:04 am

Re: Sorting the code && Storing the files.

Postby bbguimaraes on Mon Nov 02, 2015 10:01 am

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
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
 
Posts: 324
Joined: Wed Apr 11, 2012 5:34 pm
Location: Brazil

Previous

Return to Programming Discussion

Who is online

Users browsing this forum: Bing [Bot] and 4 guests

cron