防止返回私有内部类实例 [英] Preventing return of private inner class instance

查看:56
本文介绍了防止返回私有内部类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩 C ++ 中的内部类,现在我有点困惑.

I have been playing around with inner classes in C++, and I'm bit confused right now.

#include <iostream>

class outer{
private: 
    class inner{
    private:
        int something;
    public:
        void print(){
            std::cout<< "i am inner"<<std::endl;
        }   
    };
public:
    inner returnInner(){
        inner i;
        return i;
    }  
};

int main(){
    outer o;
    //outer::inner i = o.returnInner(); (1)
    //auto i = o.returnInner();         (2)
    //i.print();                  
    o.returnInner().print();          //(3)
    return 0;
}

这是在Linux上使用 clang ++-3.5 -std = c ++ 14 编译的.

This is compiled on Linux with clang++-3.5 and -std=c++14.

  • (1)中,我收到了预期的编译器错误,因为 inner outer 的私有内部类.

  • In (1) I'm getting a compiler error as expected because inner is a private inner class of outer.

但是,在(2)中,当使用 auto 关键字时,编译成功并且程序运行.

However, in (2) when the auto keyword is used, compilation is successful and the program runs.

所有内容都适用于(3).

为什么会这样?我可以阻止从外部方法返回私有内部类的实例,同时保留在外部类中移动和/或复制它们的能力吗?

Why is this so? Can I prevent returning an instance of private inner class from outer methods while retaining the ability to move and/or copy them within the outer class?

编译器是否应该像(1)一样在(2)(3)中引发错误?

Shouldn't the compiler elicit an error in (2) and (3) like it does in (1)?

推荐答案

为什么会这样编译器不应该像(1)一样在(2)和(3)中返回错误吗?

why is it so Shouldn't compiler return error in (2) and (3) as in (1)?

这是一个有趣的问题.对此的答案就更有趣了.

It is an interesting question.. and answer to this is even more interesting.

在您发布的代码中,内部是一种类型的名称.它是名称,被声明为私有,而不是 type 本身 1 —在C ++中, type 的可访问性没有任何意义.可访问性仅适用于名称—是类型的 name ,函数的 name 还是data的 name .因此,当您听到类似类型X是公共类" 之类的消息时,它几乎总是意味着"名称X被声明为公共,它指向一个类型,而该类型为无所不在,它无处不在" .

In the code you posted, inneris a name of a type. It is the name which is declared to be private, not the type itself1 — accessibility of type doesn't make sense in the context of C++; accessibilty applies to name only — be it name of type, name of function, name of data . So when you hear something like "the type X is a public class", it almost always means "the name X is declared to be public, and it refers to a type and the type is omnipresent, it exists everywhere".

回到为什么使用 auto 不会给出错误的原因,好吧,因为当您使用 auto 时,您将访问 type 而不用使用它的名称,这就是为什么它不会给出错误的原因.

Coming back to why using auto doesn't give error, well because when you use auto, you're accessing the type without using its name, which is why it doesn't give error.

如何防止外部方法返回私有内部类的实例,

how can i prevent return of an instance of private inner class from outer methods,

没有办法.

1. struct {int数据;} type ,没有要引用的 name ,但请注意,这不是有效的声明,因为它缺少 name .您必须声明未命名类型的对象,如 struct {int data;} obj; 或以 struct data_type {int data;};

1. struct { int data; } is a type, without a name to refer to, though note that is not a valid declaration, because it lacks name. Either you've to declare an object of unnamed type as in struct { int data; } obj; Or give it a name as struct data_type { int data; };

这篇关于防止返回私有内部类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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