声明主要的朋友被认为有害? [英] Declaring main as friend considered harmful?

查看:133
本文介绍了声明主要的朋友被认为有害?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 main 可以是 / code>:

I know that main can be a friend of a class:

#include <iostream>

class foo {
  friend int main();
  int i = 4;
};

int main() {
  foo obj;
  std::cout << obj.i << std::endl;
}

LIVE DEMO

但是,我觉得这是完美的允许它隐藏许多危险。

However, I feel that although this is perfectably allowable it conceals many dangers.


  1. c> c c c c>作为类的朋友应该被认为有害吗?

  1. Are there any valuable uses in making main a friend of a class?
  2. Are there any reasons that declaring main as friend of a class should be considered harmful?


推荐答案

如果功能不可用,选择是否使用或避免法律功能,其实,法律。我相信对此有严重怀疑,因为标准说

The choice whether to use or avoid a legal feature becomes moot if the feature is not, in fact, legal. I believe there's serious doubt surrounding this, because the Standard says


函数 main

已经有一个关于是否结束 :: main()的问题实际上已被允许,您可以在我的答案中找到更多详细信息。

There's already a question regarding whether befriending ::main() is in fact allowed, and you'll find more details in my answer there.

这篇关于声明主要的朋友被认为有害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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