Accessing Static Class Variables in C++

Accessing static class variables in C++?

You must add the following line in the implementation file:

int Foo::bar = you_initial_value_here;

This is required so the compiler has a place for the static variable.

How to access static members of a class?

You've declared the static members fine, but not defined them anywhere.

Basically what you've said "there exists some static member", but never set aside some memory for it, you need:

int A::x = 100;

Somewhere outside the class and not inside main.

Explicitly access static member variable in static member method - in C++

Is there something like self:: in C++ ?

No there is no such feature, but you can use a class local typedef:

class MyClass {
typedef MyClass self;
static int testValue;
static int getTestValue1(){
return self::testValue;
}
};

See a working demo.

Access static class variables in Objective-C

There is no static class variables in Objective-C, thus you can't expect something like Class.var_class. This is why your last proposition is a correct one, and commonly adopted. See, Does Objective-C support class variables?.

----EDIT----

Some new constructions were added to Objective-C, see Objective-C Class Properties. Thus syntax is admitted, while there is no automatic synthesis.

Access static class members from dynamic library in c++

$(CXX) $(CXX_FLAGS) -I$(INC) -L$(LIB) -shared $^ -o $@ -lMain

You are linking Main.cpp directly into the shared library.

And you are running Main.cpp as part of your executable.

As a result, there are duplicate instantions of the static class member. This violates the One Definition Rule, and results in undefined behavior. This is the explanation for the behavior you observed.

You most likely did that because without linking -lMain the shown program segfaults. The reason this happens is completely unrelated -- the inlined C-linkage function never gets instantiated in the shared library, because the compiler had no reason to instantiate it, when compiling the shared library. You probably could not figure out why, but discovered that linking Main resolved the segfault, but resulted in this behavior. This is a classical XY problem. Your question should've been why you were getting a segfault, originally.

Multiple classes need access to a static variable

I'm not sure if I need an instance of D in main.

You don't. static variables are global, they are shared by all the instances of the same class and you don't need to instantiate a class to be able to use such variables so they must be initialized regardless of class instantiation. That being the case they need to be initialized outside the class.

How do I implement this error-free?

Something like:

class B{};

class C{};

class A{
public:
B instanceB;
C instanceC;
};

class D{
public:
D(){};
static int staticVariable;
};

int D::staticVariable = 10; //<-- here, outside the class

int main()
{
//no instance of D needed
std::cout << D::staticVariable; // prints 10
D::staticVariable++;
std::cout << D::staticVariable; // prints 11
}


And how do you do it for static objects that call a function for initialization?
I meant if the function is of the same class as the object you're trying to initialize, and is non-static, I can't seem to be able to call staticVariable.init();

It's the same principle, you declare the object static within the class and initialize it outside:

class A{
public:
void init(){std::cout << "Do Something";} //non-static method
};

class D{
public:
static A obj; //declare
};

A D::obj{}; //<-- initialize outside the class

int main()
{
D::obj.init(); // prints 'Do Something'
}

Is it possible to access static members class in C++ like in Java?

In C++, you need to use Var::jf1. The . syntax is used when you have an object on the left side.

Also, you will need to define the member in your .cpp file:

JFrame Var::jf1;

Same for the other members.

What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?

A static variable shares the value of it among all instances of the class.

Example without declaring it static:

public class Variable
{
public int i = 5;
public void test()
{
i = i + 5;
Console.WriteLine(i);
}
}

public class Exercise
{
static void Main()
{
Variable var = new Variable();
var.test();
Variable var1 = new Variable();
var1.test();
Console.ReadKey();
}
}

Explanation: If you look at the above example, I just declare the int variable. When I run this code the output will be 10 and 10. Its simple.

Now let's look at the static variable here; I am declaring the variable as a static.

Example with static variable:

public class Variable
{
public static int i = 5;
public void test()
{
i = i + 5;
Console.WriteLine(i);
}
}

public class Exercise
{
static void Main()
{
Variable var = new Variable();
var.test();
Variable var1 = new Variable();
var1.test();
Console.ReadKey();
}
}

Now when I run above code, the output will be 10 and 15. So the static variable value is shared among all instances of that class.

Accessing private member variables of a class from a static method

static myClassPtr create(unsigned int val) {

create() is a static method of myClass, it is a member of this class. As such it it entitled to access all private members and methods of its class. This right extends not only to its own class instance, but any instance of this class.

As per my understanding private members should not be accessible.

... except by members of their class.

Let's create a completely pointless copy constructor for your class, the same copy constructor you would get by default:

myClass(const myClass &o) : m_val{o.m_val} {}

This copy constructor has no issues, whatsoever, of accessing m_val of the passed-in object. Exactly the same thing happens here. m_val is a private member of its class. It doesn't mean that only an instance of the same exact object can access its private members, it means that any instance of the class, or a static class method, can access the private class members.



Related Topics



Leave a reply



Submit