Is the output is determined? Here is the code snippet:

#include <iostream>

class Outer {
public:
    class Inner {
    public:
        Inner() {}
        void func(Outer& outer) ;
        static const int thrd;
    };

private:
    static const int num;
    int var;
};

void Outer::Inner::func(Outer& outer) {
    outer.var = 1;
}


const int Outer::Inner::thrd = Outer::num;
const int Outer::num = 5;

int main(){

    std::cout << "Outer::Inner::thrd=" << Outer::Inner::thrd << std::endl; //is the output is determined?
}
1

There are 1 best solutions below

2
Pepijn Kramer On

Avoid static (global) variables. You might run into the static initialization order fiasco at some point when you want to do this kind of things across multiple translation units.

So use constexpr whenever you can. That way you cannot initialize constants the wrong way. (in header files replace static constexpr, with inline constexpr)

#include <iostream>

class Outer 
{
private:
    static constexpr int num{5};

public:
    class Inner {
    public:
        Inner() {}
        void func(Outer& outer) ;
        static constexpr int thrd{num};
    };

private:
    int var;
};

void Outer::Inner::func(Outer& outer) 
{
    outer.var = 1;
}

int main(){

    std::cout << "Outer::Inner::thrd=" << Outer::Inner::thrd << std::endl; //is the output is determined?
}