为什么友元类在静态函数调用时崩溃?

发布于 2024-11-19 09:47:35 字数 497 浏览 2 评论 0原文

#include <iostream>

using namespace std;

class CClass
{
private:
   friend class CFriend;
   static void privateFunc(){std::cout << "privateFunc" << std::endl;};
};

class CFriend
{
public:

   void privateFunc(){privateFunc();};
};

int main(int argc, char* argv[])
{
   CFriend b;
   b.privateFunc();
   return 0;
}

此代码可以编译,但使用 gcc 编译器或 http://www.ideone.com/ 程序崩溃。这是编译器错误还是我需要了解有关友元类的更多信息?

#include <iostream>

using namespace std;

class CClass
{
private:
   friend class CFriend;
   static void privateFunc(){std::cout << "privateFunc" << std::endl;};
};

class CFriend
{
public:

   void privateFunc(){privateFunc();};
};

int main(int argc, char* argv[])
{
   CFriend b;
   b.privateFunc();
   return 0;
}

This code compiles, but using the gcc-compiler or http://www.ideone.com/ the program crashes. Is that a compiler error or do I need to understand more about friend classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧时光的容颜 2024-11-26 09:47:35

您已经创建了无限递归:

void privateFunc(){privateFunc();};

请改为使用:

void privateFunc(){CClass::privateFunc();};

友元声明没有任何问题。

You've created infinite recursion:

void privateFunc(){privateFunc();};

Use instead:

void privateFunc(){CClass::privateFunc();};

There's nothing wrong with friend declaration.

画▽骨i 2024-11-26 09:47:35

对象中的无限递归,创建堆栈溢出!!!

您必须明确调用您的朋友类:

void privateFunc(){CClass::privateFunc();};

Infinite recursion in your object, creating a Stack Overflow !!!

You must explicitely call your friend class :

void privateFunc(){CClass::privateFunc();};
π浅易 2024-11-26 09:47:35

CFriend 类中也有名为 privateFunc() 的函数。这意味着当您在该函数内部调用 privateFunc() 时,它将调用自身(它如何知道您指的是另一个类),从而进入递归无限循环。

您的意思是

void privateFunc()
{
  CClass::privateFunc();
}

使用 CClass:: 来完全指定您所指的函数的名称。

You have the function called privateFunc() in CFriend class as well. This means when inside that function you call privateFunc() it will call itself (how should it know you mean the other class) thus entering a recursive infinite loop.

You mean

void privateFunc()
{
  CClass::privateFunc();
}

using CClass:: to specify completely the name of the function you mean.

橘味果▽酱 2024-11-26 09:47:35

由于堆栈溢出而崩溃,您将通过范围解析来调用静态函数

class CFriend
{
public:

   void privateFunc(){CClass::privateFunc();};
};

It is crashing because of stack overflow you would beed scope resolution to call static function

class CFriend
{
public:

   void privateFunc(){CClass::privateFunc();};
};
世界等同你 2024-11-26 09:47:35

无限递归函数导致堆栈溢出。 CFriend::privateFunc 正在调用自身。将其更改为 void privateFunc() {CClass::privateFunc();}

范围区别,公共、私有、受保护和友元,根本没有运行时后果。它们严格由编译器来决定什么是合法的或不合法的。您可以#define private public并且生成的可执行文件不会改变。

You have a stack overflow from an infinitely recursive function. CFriend::privateFunc is calling itself. Change it to void privateFunc() {CClass::privateFunc();}

Scope distinctions, public, private, protected and friend, have not runtime consequences at all. They are strictly for the compilier to decide what is legal or not. You could #define private public and the resulting executable wouldn't change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文