Possible Duplicate:
Scoped using-directive within a struct/class declaration?
Why “using namespace X;” is not allowed inside class/struct level?
I would like to introduce only std::string into structure. Why is the below considered illegal?
#include <iostream>
#include <string>
struct Father
{
using std::string;
string sons[20];
string daughters[20];
};
But oddly enough, i can do the following the in a function
int main()
{
using std::string;
}
Update: C++ uses the same keyword with different semantics.
c++ uses the keyword "using" to introduce a data member or function from the base class into the current class. Thus when i wrote using std::string inside a struct declaration, compiler is assuming that i am trying to introduce a member from a base class std. But std is not a base class, rather it is a namespace. Thus
struct A
{
int i;
}
struct B:A
{
using A::i; // legal
using std::string// illegal, because ::std is not a class
}
the same "using" keyword is also used to access a member of a particular namespace.
So, i am guessing compiler decides the semantics of "using" based on where it is declared.