声明不带名称的C ++类有什么用 [英] what is use of declaring C++ class without name
问题描述
在C ++中,您可以声明一个没有名称的类.
就像
class
{
};
in C++ , you can declare a class without name .
以上声明在C ++中完全合法.它将编译而没有任何错误.
我的问题是
尽管允许,但是它有什么具体用途吗?
还是一个漏洞????????????????? msdn.microsoft.com/en-us/library/a3bbz53t(v=vs.110).aspx">MSDN [ class { public : int x;} xaaa1,xbbb1,xccc1;
类 {公共: int y;} xaaa2,xbbb2,xccc2;
int main()
{
xaaa1.x = 1 ;
xaaa2.y = 2 ;
xbbb2.y = 3 ;
cout<< " << xaaa1.x< " << xaaa2.y
<< " << xbbb2.y< "
<< xaaa1.x + xaaa2.y + xbbb2.y
您没有义务去上课.
例如,这不是问题:
class bazz {} a;
int main()
{
爵士乐b;
爵士乐
int ;
like
class
{
};
Above declarations is perfectlly legal in C++. It will compile without any error.
My question is ,
Though it is allowed, is ther any a specific use of this ?
or it is a loophole ??Anonymous classes are allowed (see, for instance, MSDN[^]).
I think they are not a loophole.You may declare instances of that class.
class{public: int x;} xaaa1, xbbb1, xccc1;
class{public: int y;} xaaa2, xbbb2, xccc2;
int main()
{
xaaa1.x = 1;
xaaa2.y = 2;
xbbb2.y = 3;
cout<< "xaaa1.x = "<< xaaa1.x<< "; xaaa2.y = "<< xaaa2.y
<< "; xbbb2.y = "<< xbbb2.y<< "; xaaa1.x + xaaa2.y + xbbb2.y = "
<< xaaa1.x + xaaa2.y + xbbb2.y<< endl;
You are not obligated to instanciate the class.
For instance, this is not a problem:
class bazz{} a;
int main()
{
bazz b;
bazz;
int;
这篇关于声明不带名称的C ++类有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!