小编Shu*_*ngh的帖子

C++ 中的本地类

我正在阅读 Balagurusamy 的 C++ 面向对象编程中的“本地类”概念(http://highered.mcgraw-hill.com/sites/0070593620/information_center_view0/)。

最后一行说“封闭函数不能访问本地类的私有成员。但是,我们可以通过将封闭函数声明为友元来实现这一点。

现在我想知道如何完成突出显示的部分?

这是我正在尝试但没有运气的代码,

#include<iostream>
using namespace std;

class abc;

int pqr(abc t)
{
    class abc
    {
        int x;
    public:
        int xyz()
        {
            return x=4;
        }
        friend int pqr(abc);
    };
    t.xyz();
    return t.x;
}

int main()
{
    abc t;
    cout<<"Return "<<pqr(t)<<endl;
}
Run Code Online (Sandbox Code Playgroud)

我知道代码看起来是错误的,任何帮助都是可观的。

c++ local-class

5
推荐指数
1
解决办法
5555
查看次数

标签 统计

c++ ×1

local-class ×1