Initializing a static class member that depends on a private template type (C++)
I have the following situation:
class Test
{
private:
class SubType
{
//...
};
static std::vector<SubType> v;
};
Because v is static, I initia开发者_JS百科lize it in the cpp file with
std::vector<Test::SubType> Test::v;
But this does not work, the compiler tells me that "Test::SubType" is private. What can I do about this?
Thanks!
This works for me:
#include <vector>
using namespace std;
class A {
class B {
};
static B b;
static vector <B> vb;
};
A::B A::b;
vector <A::B> A::vb;
I guess you forgot to #include <vector>
. Because the following compiles on comeau
#include <vector>
class Test {
class SubType
{
//...
};
static std::vector<SubType> v;
};
std::vector<Test::SubType> Test::v;
Others reported the code compiles fine. I want to supply the Standard wording for backing it up. At 11/5
All access controls in clause 11 affect the ability to access a class member name from a particular scope. The access control for names used in the definition of a class member that appears outside of the member’s class definition is done as if the entire member definition appeared in the scope of the member’s class.
[...]
[Example:
class A { typedef int I; // private member I f(); friend I g(I); static I x; }; A::I A::f() { return 0; } A::I g(A::I p = A::x); A::I g(A::I p) { return 0; } A::I A::x = 0;
Here, all the uses of
A::I
are well-formed becauseA::f
andA::x
are members of class A and g is a friend of class A. This implies, for example, that access checking on the first use ofA::I
must be deferred until it is determined that this use ofA::I
is as the return type of a member of class A. ]
精彩评论