访问和名称查找,这个代码感到惊讶 [英] access and name lookup, surprised by this code

查看:44
本文介绍了访问和名称查找,这个代码感到惊讶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc 3.3.1和VC ++ 7.1都编译以下代码。


struct Outer

{

struct Inner

{

int f(){return c; }

};

私人:

static const int c;

};


const int Outer :: c = 123;


int main()

{

外部: :内部o;

std :: cout<< o.f()<< ''\\ n';;

}


显然我错了,但我认为以上打破了两个不同的规则

C ++。首先不是私人,其次如何内部访问

Outer的成员而没有限定名称(即返回Outer :: c)?


解释我将不胜感激。


john

Both gcc 3.3.1 and VC++ 7.1 compile the following code.

struct Outer
{
struct Inner
{
int f() { return c; }
};
private:
static const int c;
};

const int Outer::c = 123;

int main()
{
Outer::Inner o;
std::cout << o.f() << ''\n'';
}

Obviously I''m wrong but I thought the above breaks two different rules of
C++. Firstly isn''t c private, secondly how can Inner access a member of
Outer without qualifying the name (i.e. return Outer::c)?

Explanations and references to the standard would be appreciated.

john

推荐答案



John Harrison <乔************* @ hotmail.com>在消息中写道

news:c1 ************* @ ID-196037.news.uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
gcc 3.3.1和VC ++ 7.1都编译了以下代码。

struct Outer
{
struct Inner
{
int f(){return c ;私有:
static const int c;
};

const int Outer :: c = 123;

int main()
{Outer :: Inner o;
std :: cout<< o.f()<< ''\ n'';
}

显然我错了,但我认为上面打破了两个不同的C ++规则。首先不是私人的,其次如何内部访问外部的成员而不限定名称(即返回外部:: c)?

对标准的解释和参考将不胜感激。
Both gcc 3.3.1 and VC++ 7.1 compile the following code.

struct Outer
{
struct Inner
{
int f() { return c; }
};
private:
static const int c;
};

const int Outer::c = 123;

int main()
{
Outer::Inner o;
std::cout << o.f() << ''\n'';
}

Obviously I''m wrong but I thought the above breaks two different rules of
C++. Firstly isn''t c private, secondly how can Inner access a member of
Outer without qualifying the name (i.e. return Outer::c)?

Explanations and references to the standard would be appreciated.




我对你的第一个问题感到困惑。

但至于你的第二个问题 -

第9.7节/ 1


"除了使用显式指针,引用和对象名称外,嵌套类中的声明

只能使用类型名称,静态成员和调查员来自

封闭类。 [例如:

int x;

int y;


类附上{

public:

int x;

static int s;


class inner {


void f(int i)

{

int a = sizeof(x); //错误:指的是封闭:: x

x = i; //错误:指定封闭:: x

s = i; //好的:指定封闭:: s

:: x = i; // OK:分配给全局x

y = i; // OK:分配给全局y

}


void g(附上* p,int i)

{

p-> x = i; // OK:指定封闭:: x

}


};

};



Am perplexed about your 1st question.
But as for your second question -
Section 9.7/1

"Except by using explicit pointers, references, and object names, declarations
in a nested class can use only type names, static members, and enumerators from
the enclosing class". [Example:
int x;
int y;

class enclose {
public:
int x;
static int s;

class inner {

void f(int i)
{
int a = sizeof(x); // error: refers to enclose::x
x = i; // error: assign to enclose::x
s = i; // OK: assign to enclose::s
::x = i; // OK: assign to global x
y = i; // OK: assign to global y
}

void g(enclose* p, int i)
{
p->x = i; // OK: assign to enclose::x
}

};
};




" Sharad Kala" <无***************** @ yahoo.com>在消息中写道

news:c1 ************* @ ID-221354.news.uni-berlin.de ...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

John Harrison <乔************* @ hotmail.com>在消息中写道
新闻:c1 ************* @ ID-196037.news.uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
两者都是gcc 3.3。 1和VC ++ 7.1编译以下代码。

struct Outer
结构内部
{
int f(){return c;私有:
static const int c;
};

const int Outer :: c = 123;

int main()
{Outer :: Inner o;
std :: cout<< o.f()<< ''\ n'';
}

显然我错了,但我认为上面打破了两个不同的C ++规则。首先不是私人的,其次如何内部访问外部的成员而不限定名称(即返回外部:: c)?

对标准的解释和参考将不胜感激。
Both gcc 3.3.1 and VC++ 7.1 compile the following code.

struct Outer
{
struct Inner
{
int f() { return c; }
};
private:
static const int c;
};

const int Outer::c = 123;

int main()
{
Outer::Inner o;
std::cout << o.f() << ''\n'';
}

Obviously I''m wrong but I thought the above breaks two different rules of
C++. Firstly isn''t c private, secondly how can Inner access a member of
Outer without qualifying the name (i.e. return Outer::c)?

Explanations and references to the standard would be appreciated.



对你的第一个问题感到困惑。



Am perplexed about your 1st question.




这不仅仅是静态成员的定义: - )?



Isn''t it just simply a definition for the static member :-) ?




" Sharad Kala" <无***************** @ yahoo.com>在消息中写道

news:c1 ************* @ ID-221354.news.uni-berlin.de ...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

Sharad Kala <无***************** @ yahoo.com>在消息中写道
新闻:c1 ************* @ ID-221354.news.uni-berlin.de ...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

约翰哈里森 <乔************* @ hotmail.com>在消息中写道
新闻:c1 ************* @ ID-196037.news.uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
两者都是gcc 3.3。 1和VC ++ 7.1编译以下代码。

struct Outer
结构内部
{
int f(){return c;私有:
static const int c;
};

const int Outer :: c = 123;

int main()
{Outer :: Inner o;
std :: cout<< o.f()<< ''\ n'';
}

显然我错了,但我认为以上打破了两个不同规则
的C ++。首先不是私有的,其次如何在没有限定名称的情况下内部访问成员
的外部(即返回Outer :: c)?

对标准的解释和参考将不胜感激。
Both gcc 3.3.1 and VC++ 7.1 compile the following code.

struct Outer
{
struct Inner
{
int f() { return c; }
};
private:
static const int c;
};

const int Outer::c = 123;

int main()
{
Outer::Inner o;
std::cout << o.f() << ''\n'';
}

Obviously I''m wrong but I thought the above breaks two different rules of C++. Firstly isn''t c private, secondly how can Inner access a member of Outer without qualifying the name (i.e. return Outer::c)?

Explanations and references to the standard would be appreciated.



我对你的第一个问题感到困惑。



Am perplexed about your 1st question.



这不仅仅是静态成员的定义:-)?



Isn''t it just simply a definition for the static member :-) ?




我认为有一条规则,内部类没有特殊的

访问它们的封闭类。所以Inner应该被宣布为

Outer的朋友才能访问Outer :: c,至少那是我的想法。


john



I thought there was a rule to the effect that inner classes have no special
access to their enclosing class. So Inner should be declared a friend of
Outer in order to access Outer::c, at least that''s what I thought.

john


这篇关于访问和名称查找,这个代码感到惊讶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆