C++ 内部类可以访问私有变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/486099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:37:30  来源:igfitidea点击:

Can inner classes access private variables?

c++inner-classes

提问by kal

class Outer {

    class Inner {
    public:
        Inner() {}
        void func() ;
    };

private:
    static const char* const MYCONST;
    int var;
};

void Outer::Inner::func() {
    var = 1;
}

const char* const Outer::MYCONST = "myconst";

This errors out when I compile with class Outer::Inner' has no member named `var'

当我使用类 Outer::Inner' 进行编译时出现此错误,其中没有名为“var”的成员

回答by Martin York

An inner class is a friend of the class it is defined within.
So, yes; an object of type Outer::Innercan access the member variable varof an object of type Outer.

内部类是它在其中定义的类的朋友。
所以,是的;类型的对象Outer::Inner可以访问成员变量var类型的对象的Outer

Unlike Java though, there is no correlation between an object of type Outer::Innerand an object of the parent class. You have to make the parent child relationship manually.

但是,与 Java 不同的是,类型Outer::Inner对象和父类的对象之间没有相关性。您必须手动建立父子关系。

#include <string>
#include <iostream>

class Outer
{
    class Inner
    {
        public:
            Inner(Outer& x): parent(x) {}
            void func()
            {
                std::string a = "myconst1";
                std::cout << parent.var << std::endl;

                if (a == MYCONST)
                {   std::cout << "string same" << std::endl;
                }
                else
                {   std::cout << "string not same" << std::endl;
                }
            }
        private:
            Outer&  parent;
    };

    public:
        Outer()
            :i(*this)
            ,var(4)
        {}
        Outer(Outer& other)
            :i(other)
            ,var(22)
        {}
        void func()
        {
            i.func();
        }
    private:
        static const char* const MYCONST;
        Inner i;
        int var;
};

const char* const Outer::MYCONST = "myconst";

int main()
{

    Outer           o1;
    Outer           o2(o1);
    o1.func();
    o2.func();
}

回答by MSN

An inner class has access to all members of the outer class, but it does not have an implicit reference to a parent class instance (unlike some weirdness with Java). So if you pass a reference to the outer class to the inner class, it can reference anything in the outer class instance.

内部类可以访问外部类的所有成员,但它没有对父类实例的隐式引用(与 Java 的一些奇怪之处不同)。因此,如果将外部类的引用传递给内部类,则它可以引用外部类实例中的任何内容。

回答by Mark Ransom

Anything that is partof Outer should have access to all of Outer's members, public or private.

任何属于Outer 的东西都应该可以访问所有 Outer 的成员,无论是公共的还是私有的。

Edit: your compiler is correct, var is not a member of Inner. But if you have a reference or pointer to an instance of Outer, it could access that.

编辑:您的编译器是正确的,var 不是 Inner 的成员。但是如果你有一个指向 Outer 实例的引用或指针,它可以访问它。

回答by xiaochuanQ

var is not a member of inner class.

var 不是内部类的成员。

To access var, a pointer or reference to an outer class instance should be used. e.g. pOuter->var will work if the inner class is a friend of outer, or, var is public, if one follows C++ standard strictly.

要访问 var,应该使用指向外部类实例的指针或引用。例如,如果内部类是外部类的朋友,则 pOuter->var 将起作用,或者,如果严格遵循 C++ 标准,则 var 是公共的。

Some compilers treat inner classes as the friend of the outer, but some may not. See this document for IBM compiler:

一些编译器将内部类视为外部的朋友,但有些可能不是。有关IBM 编译器,请参阅此文档

"A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.

“嵌套类是在另一个类的范围内声明的。嵌套类的名称是其封闭类的局部名称。除非使用显式指针、引用或对象名称,否则嵌套类中的声明只能使用可见结构,包括来自封闭类和全局变量的类型名称、静态成员和枚举数。

Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class."

嵌套类的成员函数遵循常规访问规则,对其封闭类的成员没有特殊的访问权限。封闭类的成员函数对嵌套类的成员没有特殊访问权限。”

回答by Adarsh Kumar

First of all, you are trying to access non-static member varoutside the class which is not allowed in C++.

首先,您试图访问varC++ 中不允许的类之外的非静态成员。

Mark's answer is correct.

马克的回答是正确的。

Anything that is part of Outer should have access to all of Outer's members, public or private.

任何属于 Outer 的东西都应该可以访问所有 Outer 的成员,无论是公共的还是私有的。

So you can do two things, either declare varas staticor use a reference of an instance of the outer class to access 'var' (because a friend class or functionalso needs reference to access private data).

所以你可以做两件事,要么声明var为,要么static使用外部类实例的引用来访问“var”(因为朋友类或函数也需要引用来访问私有数据)。

Static var

静态变量

Change varto staticIf you don't want varto be associated with the instances of the class.

更改varstatic如果您不想var与类的实例相关联。

#include <iostream>

class Outer {

private:
    static const char* const MYCONST;
    static int var;

public:
   class Inner {
    public:
        Inner() {
          Outer::var = 1;
        }
        void func() ;
    };
};

int Outer::var = 0;

void Outer::Inner::func() {
    std::cout << "var: "<< Outer::var;
}

int main() {
  Outer outer;
  Outer::Inner inner;
  inner.func();

}

Output- var: 1

输出变量:1

Non-static var

非静态变量

An object's reference is must access any non-static member variables.

对象的引用必须访问任何非静态成员变量。

#include <iostream>

class Outer {

private:
    static const char* const MYCONST;
    int var;

public:
   class Inner {
    public:
        Inner(Outer &outer) {
          outer.var = 1;
        }
        void func(const Outer &outer) ;
    };
};

void Outer::Inner::func(const Outer &outer) {
    std::cout << "var: "<< outer.var;
}

int main() {
  Outer outer;
  Outer::Inner inner(outer);
  inner.func(outer);

}

Output- var: 1

输出变量:1

Edit - External links are links to my Blog.

编辑 - 外部链接是指向我的博客的链接。