In FORTRAN, we can have a common data block in which all exchanging
data are stored. When a subroutine/function needs data from the data block,
it can automatically get them from the data block if the program found
data with the same variable name.

In C++, I am wondering how we use a class doing the same
thing like what we do in FORTRAN?

A simple practical example will be highly appreciated.

Recommended Answers

All 3 Replies

The good news is C++ has what is called global data objects -- objects that are declared outside any function are considered visible (scope) and usable to all functions/classes within the entire program. The bad news is global data is very frowned upon because they make the program more difficult to understand and debug. A better solution is to declare the objects local to a function then pass them around as arguments to the other functions/classes that need them.

A little more restricted than global objects, but still worth considering in the right context, are class members declared with the keyword static as part of the declaration. These are variables that can be used by any instance of the class, but only by instances of that class. For example, I've seen static member variables used when you wanted to keep track of data irrespective of which object of the class has changed it, or if you wanted to keep track of how many objects of a given type are being used at any given point in the program for whatever reason. Declaring a container object using keyword static in the class declaration will declare a single copy of the container for all objects of that type, and only objects of that type, to use. Using static objects isn't something I've typically seen in programs written by novices in C++, however.

>>Declaring a container object using keyword static in the class declaration will declare a single copy of the container for all objects of that type
true

>> and only objects of that type, to use

No. static objects have the same scope as non-static, that is public, protected and private. And static objects are identical to globals with the class and access scope restrictions. A public static can be accessed anywhere in the program, but a private static can only be accessed by a member of that same class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.