Class scope question

Hi. When I defined some type names in the outer scope of a class and used them, can I redefine them in the class using the same names? It is said some compliers will allow it while some don't.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>

typedef string::size_type Si;

class Type {
public:
	char get(const string &s, Si index) { return s[index]; }
	//use type definition in the outer scope
	typedef char *Si;  //redefine using same name
	char put() { return p[0]; }
private:
	Si p = "hello!";
};

int main(){
	Type str;
	cout << str.get("hello!", 0) << endl;
	cout << str.put() << endl;
  //both print 'h' 
Yes you can, but it's not really "redefining" the name. It's defining a different type name with the same name but in a different scope. You can still use the typedef of the outer scope by qualifying the full name ::Si.

1
2
Si p = "hello!";
::Si q = 5;
That's the same as object names. it seems some say type names are special. I got it, thanks.
Last edited on
Topic archived. No new replies allowed.