C++ Změnit soukromý člen třídy přes jen jeden konkrétní třídy?

0

Otázka

Jak mohu změnit int 'a' uvnitř třídy, ale omezit funkce, které změní int POUZE třídy B? Já ani nechci být schopen to změnit.

Snažil jsem se vytvořit funkce příteli, ale dostanu chybu, jsem zahrnuty v komentářích kódu. Pak jsem zkoušel jen dopředu deklarovat funkci jako friend funkce v soukromých a pak vlastně vytváření funkcí ve veřejné, ale třídy C přístup k veřejné funkci. To i přes to, že mě psaní (v private sekci), že funkce je funkce příteli. Můžete mi "přítel", vlastní hodnoty? Já jsem si nejste jisti, co dělat tady.

#include <stdio.h>
#include <iostream>

int main() {

    // forward declaration
    class B;
    class C;

    class A {
        private:
            int a; // private member I want to edit, but only via class B.
            int a2; // I would like to NOT be able to access this in class B. I only want to access and modify int a from class B, no other variable. If possible.

            // invalid use of non-static data member 'a'
            friend void test(int new_value) {
                a = 5;
            }

            friend B;
        public:
    };

    class B {
        private:
            int b;
        public:
            change_a_value(A a_class, int new_int) {
                a_class.test(new_int); // I want this to be possible.
            }
    };

    class C {
    private:
        int c;
    
    public:
        change_a_value(A a_class, int new_int) {
            a_class.test(new_int); // I want this to be impossible
        }
    };

    return 0;
}


c++ class scope
2021-11-23 17:56:05
1

Nejlepší odpověď

1

Přidat "void", než prototypy change_a_value, a změnit

friend B

k

friend class B

a potlačit přítel v

friend void test(int new_value) {

kompletní opravené program :

int main() {
  // forward declaration
  class B;
  class C;
  class A {
  private:
    int a; // private member I want to edit, but only via class B.
    int a2; // I would like to NOT be able to access this in class B. I only want to access and modify int a from class B, no other variable. If possible.
    void test(int new_value) {
      a = new_value;
    }
    friend class B;
  public:
  };
  class B {
  private:
    int b;
  public:
    void change_a_value(A a_class, int new_int) {
      a_class.test(new_int); // I want this to be possible.
    }
  };
  class C {
  private:
    int c;
  public:
    void change_a_value(A a_class, int new_int) {
      a_class.test(new_int); // I want this to be impossible
    }
  };
  return 0;
}

Výsledky kompilace jsou, co se očekávalo :

TestCpp2.cpp:9:14: error: ‘void main()::A::test(int)’ is private
     void test(int new_value) {
          ^
TestCpp2.cpp:28:31: error: within this context
       a_class.test(new_int); // I want this to be impossible
                           ^
Makefile:510: recipe for target 'TestCpp2.o' failed
make: *** [TestCpp2.o] Error 1
2021-11-25 06:39:24

Pokud moje odpověď se zdá v pořádku pro vás, můžete ji přijmout ? Díky
Saint-Martin

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................