2

How would I go about making static methods that are used like the static methods in java when I have multiple .cpp files?

In java you can reference another class by using MyClass name = new MyClass(); and every variable in this class will be the same when called in any other file that you have.

I seems like a pain to have to be able to pass by reference the MyClass& name in every function you want to use that class in.

Is there a way to define MyClass name at the top of a .cpp file without it making a brand new instance of that object in memory and wiping away all of your progress, for example if you were trying to make a simple counter, and the main class would tell the counting class to increment, and the visibility class would get the number of counts from the counting class and print it to the screen. Now this would be simple in that you could pass the counting class to the visibility, but what if you had 20+ methods, then it would be difficult to keep track of.

I'm just not that familiar with static, or references, and I have looked around and saw "namespace", but I am not sure where I would define this, or if it is what I even need.

Thanks in advance

Example Java Code:

public class Foo{
private static int counter = 0;

public int getCounter(){
    return counter;
}

public void incrCounter(){
    counter+=1;
}
}

public class MainProg{
public static void main(String[] args){
    Foo foo = new Foo();
    Display disp = new Display();
    foo.incrCounter();
    foo.incrCounter();
    disp.displayCounter();
}
}

public class Display{
Foo foo = new Foo();
public void displayCounter(){
    foo.incrCounter();
    System.out.println(foo.getCounter());
}
}

This should print out "3"

Brink
  • 31
  • 1
  • 4
  • What does [the book you're learning C++ from](http://stackoverflow.com/q/388242/46642) say? – R. Martinho Fernandes Mar 07 '12 at 01:06
  • I think you would use a header file. All methods are by default "static" in C++, unless they are contained within an object. Each class does not represent one object, but can contain multiple objects or zero objects. – Ryan Amos Mar 07 '12 at 01:07
  • My classes are already using header files and .cpp's, but I am trying to avoid constantly passing around the Object. – Brink Mar 07 '12 at 01:12
  • 2
    You should really state what your problem to solve is, rather than the problem with the language. It is hard to determine what you are worrying about, do you need a global variable? A static member of a class? What is it that you want to access? Also note that the Java code you present makes no sense with the description... – David Rodríguez - dribeas Mar 07 '12 at 01:15
  • In Java, you can access a class in as many other classes as you like and the values in the class you are accessing will always be the same as long as the variables are static. I am looking for a way to do this in C++. – Brink Mar 07 '12 at 01:17
  • Is it the case that in some of those instances where you used 'class' you really meant 'object'? Perhaps an example of actual Java code could help here. – Luc Danton Mar 07 '12 at 01:24
  • That could be the case, I will try and make an example in the top. – Brink Mar 07 '12 at 01:25
  • Way late to the show. You do not need to instantiate a object in Java to use a static field. So no you would not need to create a java object to use the static field. Same capabilities are in c++. In this sample you create a Foo object which is unnecessary for this use case. – Mr00Anderson Sep 26 '18 at 00:59

2 Answers2

2

In C++, scope resolution operator :: is used instead of . in Java. You call static methods like this:

In the header:

class MyClass {
public:
    static int myPrivateStaticVar;
    static void myStaticMethod();
};

In the cpp file:

#include "myfile.h"
#include <iostream>

using namespace std;

int MyClass::myPrivateStaticVar = 123;

void MyClass::myStaticMethod() {
    cerr << "hello, world! " << myPrivateStaticVar  << endl;
}

int main() {
    MyClass::myStaticMethod();
}

Your static method myStaticMethod needs to be public in the MyClass class.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • So you do not need to define a whole new object and if you have initalized one MyClass somewhere throughout all of your classes, using MyClass:: will be able to use updated values? – Brink Mar 07 '12 at 01:11
  • @Brink Static methods in C++ have the same limitations as static methods in Java: they can access only static variables and call only static member functions of the class. You cannot access *instance methods* or *instance variables* of the class from your static method, unless an instance of the object is available statically (e.g. as in a singleton implementation). – Sergey Kalinichenko Mar 07 '12 at 01:15
  • When I used your example you provided, I get "Error: Cannot call member function 'Myclass::myMethod(int)' without object", I changed the method to static and that did not work either. I don't want to declare an object though... – Brink Mar 07 '12 at 01:19
  • It kind of works, except it is saying that there is an undefined reference to the static variable that I am using. – Brink Mar 07 '12 at 01:30
  • It is saying that I have an undefined reference to MyClass::(variable), but I think that this is a completely different issue now. – Brink Mar 07 '12 at 01:39
  • @Brink I just tried it locally: I put the header in "myfile.h", and everything compiled fine. – Sergey Kalinichenko Mar 07 '12 at 01:42
  • what would it look like if you introduced a variable into it, for example an integer? My main compiles fine as well when calling the method, but it is hitting a problem when trying to use variables. – Brink Mar 07 '12 at 01:46
  • @Brink: The solution to your new problem is answered in [this question](http://stackoverflow.com/questions/5601051/static-member-variable-in-a-class). – Ken Wayne VanderLinde Mar 07 '12 at 05:01
  • @Brink I added a static variable to the example. – Sergey Kalinichenko Mar 07 '12 at 15:32
0

In Java, you can access a class in as many other classes as you like and the values in the class you are accessing will always be the same as long as the variables are static.

You mean, like a global variable? You can declare a global variable like this:

extern int foo;

You will need one definition of it, which goes like this:

int foo = 42;

The declaration will work well if put in a header file that is included anywhere it is need, and the definition should go in one source file.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • But won't this be declaring a new instance of foo, and it will go back to the default variables that have been set up in the class? I am looking to declare an object of foo, but have it be able to use variables that have been changed from another class. – Brink Mar 07 '12 at 01:24
  • What class? There's no class in my example. – R. Martinho Fernandes Mar 07 '12 at 01:30
  • If you could look at my example code at the top in Java, then maybe you can see what I am trying to get at. – Brink Mar 07 '12 at 01:35