Are static variables in a base class shared by all derived classes?
They will each share the same instance of staticVar
.
In order for each derived class to get their own static variable, you'll need to declare another static variable with a different name.
You could then use a virtual pair of functions in your base class to get and set the value of the variable, and override that pair in each of your derived classes to get and set the "local" static variable for that class. Alternatively you could use a single function that returns a reference:
class Base {
static int staticVarInst;
public:
virtual int &staticVar() { return staticVarInst; }
}
class Derived: public Base {
static int derivedStaticVarInst;
public:
virtual int &staticVar() { return derivedStaticVarInst; }
}
You would then use this as:
staticVar() = 5;
cout << staticVar();
static variable for each derived class
You can use the Curiously recurring template pattern.
// This is the real base class, preserving the polymorphic structure
class Base
{
};
// This is an intermediate base class to define the static variable
template<class Derived>
class BaseX : public Base
{
// The example function in the original question
bool foo(int y)
{
return x > y;
}
static int x;
};
class Derived1 : public BaseX<Derived1>
{
};
class Derived2 : public BaseX<Derived2>
{
};
Now classes Derived1
and Derived2
will each have a static int x
available via the intermediate base class! Also, Derived1
and Derived2
will both share common functionality via the absolute base class Base
.
Are static variables inherited
Please have a look into the documentation of oracle: http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e12110
Static variables are inherited as long as they're are not hidden by another static variable with the same identifier.
Are static members in base classes shared across its derived classes
Could you show some code?
public class A
{
private static string test;
public string Test { get { return test; } set { test = value; } }
public override string ToString()
{
return Test;
}
}
public class B : A
{
public override string ToString()
{
return base.ToString();
}
}
public class C:A
{
public override string ToString()
{
return base.ToString();
}
}
Test console code
private static void Test()
{
Console.Clear();
A a = new A();
B b = new B();
C c = new C();
a.Test = "Test A";
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
b.Test = "Test B";
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
c.Test = "Test C";
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.ReadKey();
}
Test console result
Test A
Test A
Test A
Test B
Test B
Test B
Test C
Test C
Test C
C++ : Initializing base class constant static variable with different value in derived class?
You can't. There is one instance of the static variable that is shared by all derived classes.
C# Same variables, static and not, on every derived class
Is there a reason that you need sides
to be static? For example, do you need to know the number of sides for a shape without having an instance?
If so, I can't see any reason for inheritance, just do Circle.sides
or Hexagon.sides
.
The only way to work with types and not instances is through reflection, so inheritance is not a concern.
If you are working with instances, define an abstract property then polymorphism can be used. For example:
public abstract class Shape
{
public abstract int Sides { get; }
}
public class Circle : Shape
{
public override int Sides => 1;
}
public class Hexagon : Shape
{
public override int Sides => 6;
}
Shape[] shapes = new Shape[] { new Circle(), new Hexagon() };
foreach (Shape shape in shapes)
{
Console.WriteLine($"Shape has {shape.Sides} sides");
}
I can't see any reason why you would make draw()
static, unless it is a factory method, as it would require instance variables.
Related Topics
How to Initialise a Dynamic Array in C++
What Do C and Assembler Actually Compile To
M_Pi Works with Math.H But Not with Cmath in Visual Studio
How to Scale Down Numbers from Rand()
What Happens to Global Variables Declared in a Dll
How to Create a Pause/Wait Function Using Qt
How to Catch Python Stdout in C++ Code
Lambda Implicit Capture Fails with Variable Declared from Structured Binding
Copy an Cv::Mat Inside a Roi of Another One
Reorder Vector Using a Vector of Indices
Why Is My Log in the Std Namespace
Avoiding Copy of Objects with the "Return" Statement