Home > Backend Development > C++ > Can Inner Classes in C Access Private Members of Their Outer Class?

Can Inner Classes in C Access Private Members of Their Outer Class?

Patricia Arquette
Release: 2024-11-27 13:36:13
Original
460 people have browsed it

Can Inner Classes in C   Access Private Members of Their Outer Class?

Can Inner Classes Access Private Variables?

In this code example, an inner class, Inner, is defined within an outer class, Outer. When an instance of Inner is created, it is expected to have access to the private member variable var of the Outer class. However, when compiled, an error occurs, stating that Inner has no member named var.

Inner Classes and Member Access

An inner class in C is considered a friend of its outer class. As a friend, an inner class has access to all members of the outer class, including private members. Therefore, instances of Inner should be able to access the private variable var of Outer.

The Problem and the Solution

The error stems from the lack of a connection between instances of Inner and instances of Outer. In Java, there is an implied parent-child relationship between inner classes and their enclosing classes. However, in C , this relationship must be explicitly defined.

To fix the issue, a reference to the outer class must be passed to the constructor of the inner class. This provides the inner class with the necessary context to access the members of the outer class.

Here is an example that demonstrates how to establish the parent-child connection and allow Inner to access the private variable var of Outer:

#include <string>
#include <iostream>

class Outer {
public:
    class Inner {
    public:
        Inner(Outer& x): parent(x) {}  // Pass a reference to the outer class

        void func() {
            std::string a = "myconst1";
            std::cout << parent.var << std::endl;

            if (a == Outer::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();

    return 0;
}
Copy after login

This revised code allows Inner to access var because the constructor of Inner establishes a parent-child relationship between Inner and the Outer instance that created it.

The above is the detailed content of Can Inner Classes in C Access Private Members of Their Outer Class?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template