Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A {
- private:
- static const int a = 4;
- /*...*/
- };
- class A{
- private:
- static const int a = 4; // valid
- static const std::string t ; // can't be initialized here
- ...
- ...
- };
- // in a cpp file where the static variable will exist
- const std::string A::t = "this way it works";
- class X
- {
- public:
- static int i;
- };
- int X::i = 0; // definition outside class declaration
- class C {
- static int i;
- static int j;
- static int k;
- static int l;
- static int m;
- static int n;
- static int p;
- static int q;
- static int r;
- static int s;
- static int f() { return 0; }
- int a;
- public:
- C() { a = 0; }
- };
- C c;
- int C::i = C::f(); // initialize with static member function
- int C::j = C::i; // initialize with another static data member
- int C::k = c.f(); // initialize with member function from an object
- int C::l = c.j; // initialize with data member from an object
- int C::s = c.a; // initialize with nonstatic data member
- int C::r = 1; // initialize with a constant value
- class Y : private C {} y;
- int C::m = Y::f();
- int C::n = Y::r;
- int C::p = y.r; // error
- int C::q = y.f(); // error
- #include <iostream>
- using namespace std;
- struct X {
- static const int a = 76;
- };
- const int X::a;
- int main() {
- cout << X::a << endl;
- }
- template<class T> struct X{
- static T x;
- };
- template<class T> T X<T>::x = T();
- int main(){
- X<int> x;
- }
- // .h
- class A{
- private:
- static const int a = 4;
- static const foo bar;
- ...
- ...
- };
- // .cpp
- const foo A::bar = ...;
- class Entry {
- public:
- int x;
- Entry(v):x(v){}
- }
- struct Type {
- static constexpr Entry GetEntry() { return Entry(10); }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement