声明不带名称的C ++类有什么用 [英] what is use of declaring C++ class without name

查看:98
本文介绍了声明不带名称的C ++类有什么用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,您可以声明一个没有名称的类.

就像

  class 
{

}; 



以上声明在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 ; 


in C++ , you can declare a class without name .

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屋!

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