对象的名称可以是任何其他类的名称吗? [英] Can a name of a object be name of any other class?

查看:88
本文介绍了对象的名称可以是任何其他类的名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我有两个A类和B类。是否错误地声明一个名为B的对象是
类型A?

A类{}

B级{}

AB;

为什么会出错?

解决方案



< vi ******************** @ gmail.com>在消息中写道

news:11 ********************** @ 38g2000cwa.googlegro ups.com ...

考虑我有两个A类和B类。声明一个名为B的A类对象是错误的吗?
A类{}
B类{}
AB;
为什么会出错?




令人惊讶的是,这个编译,并且在我的有限测试中确实有效。我会说这是错的,但我不能说为什么。这实际上编译并产生输出10的输出。这是我不希望的。


并不令人惊讶,你不能说

A A;

然而。但如果你能说A B;为什么你不能说AA;?


#include< iostream>

#include< string>


A级{

公开:

int X;

朋友std :: ostream&运营商LT;< (std :: ostream& os,const A& Instance);

};


std :: ostream&运营商LT;< (std :: ostream& os,const A& Instance)

{

os<< Instance.X;

返回操作系统;

}


B级{};


int main()

{

AB;

BX = 10;


std :: cout<< B<< std :: endl;


std :: string wait;

std :: cin>>等等;


返回0;

}


它不会给出一个错误,但它不允许实例化类型B的

对象。它不识别calss B的声明。可以

任何人解释?


vi ** ******************@gmail.com 写道:

考虑我有两个A和B类。错误地声明一个名为B的A型对象?
A类{}
B类{}
AB;
为什么会出错?



不是。


[3.3 / 4]


给定一套声明在一个声明区域中,每个声明区域都是

指定相同的不合格名称,

?它们都应引用同一个实体,或全部引用函数和

函数模板;或者

?只有一个声明应声明一个类名或枚举名

不是typedef名称而其他声明都应引用

相同的对象或枚举,或者所有引用功能和功能

模板;在这种情况下,类名或枚举名称是隐藏的。

所以你所做的并不违法,但你隐藏了B类。作为一个编码风格的问题但是,我会远离这些东西。

最好


Kai-Uwe Bux


consider i have two classes A and B. Is it wrong declaring a object of
type A with name B?
class A{}
class B{}
A B;
Why is it wrong?

解决方案


<vi********************@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...

consider i have two classes A and B. Is it wrong declaring a object of
type A with name B?
class A{}
class B{}
A B;
Why is it wrong?



Suprisingly, this compiles, and in my limited test actually works. I would
say it''s wrong, but I can''t say why. This actually compiles and produces
the output "10" which I would not expect.

Not suprisingly, you can''t say
A A;
however. But if you could say A B; why couldn''t you say A A;?

#include <iostream>
#include <string>

class A {
public:
int X;
friend std::ostream& operator<< (std::ostream& os, const A& Instance );
};

std::ostream& operator<< (std::ostream& os, const A& Instance )
{
os << Instance.X;
return os;
}

class B { };

int main()
{
A B;
B.X = 10;

std::cout << B << std::endl;

std::string wait;
std::cin >> wait;

return 0;
}


It will not give an error but it will not allow to instantiate an
object of type B. It does not identify the declaration of calss B. Can
anyone explain?


vi********************@gmail.com wrote:

consider i have two classes A and B. Is it wrong declaring a object of
type A with name B?
class A{}
class B{}
A B;
Why is it wrong?



It is not.

[3.3/4]

Given a set of declarations in a single declarative region, each of which
specifies the same unqualified name,
? they shall all refer to the same entity, or all refer to functions and
function templates; or
? exactly one declaration shall declare a class name or enumeration name
that is not a typedef name and the other declarations shall all refer to
the same object or enumerator, or all refer to functions and function
templates; in this case the class name or enumeration name is hidden.
So what you are doing is not illegal, but you are hiding class B. As a
matter of coding style, however, I would stay away from such things.
Best

Kai-Uwe Bux


这篇关于对象的名称可以是任何其他类的名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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