- fchao-Sinus-Wechselrichter AliExpress         
Ergebnis 1 bis 2 von 2

Thema: Change member of a class from another class

  1. #1

    Beitrag Change member of a class from another class

    Anzeige

    Praxistest und DIY Projekte
    Hey there,
    I have a problem: I want to change the member of a class A from class B. So I made class B to a friend of class A. There is a function in class B that changes the variable "state". And in class A there is a function that checks if the variable "state" is 1. If true it will turn on an Led. I wrote this code:
    Code:
    class A {
      friend class B;
      
      private:
      int state;
      int Led = 13;
      A(){
      pinMode(Led, OUTPUT);
      }
    
      void update(){
      if (state == 1){
      digitalWrite (Led, HIGH);
      }
     }
    };
    
    class B{
      public:
      void update(){
        state = 1;
      }
    
    };
    
    A first;
    B second;
    
    void setup() {
      // put your setup code here, to run once:
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      first.update();
      second.update();
    }
    But it doesnt work. Where is the mistake? I hope somebody can help me.

    Best regards

    Maurice W.

  2. #2
    Erfahrener Benutzer Roboter Experte
    Registriert seit
    04.09.2011
    Ort
    Hessen
    Beiträge
    707
    Hi,

    the friend declaration does only say that B has access to A's private and protected elements.

    But an instance of B does not know in which instance of A it should change the value of state. You could have something like this
    Code:
    A a, b, c;
    there are three objects of type A, which one do you want to change ?

    One possible solution would be giving B's update a parameter
    Code:
    class B{
      public:
      void update(A& a){
        a.state = 1;
      }
    
    };
    and using it this way
    Code:
    void loop() {
      // put your main code here, to run repeatedly:
      first.update();
      second.update(first);
    }
    Much better C++ would be giving A a changeState method, instead of using friend.
    Geändert von Mxt (27.08.2016 um 10:01 Uhr) Grund: removed const

Ähnliche Themen

  1. Probleme mit Class-D-Audioverstärker / Digitalendstufe
    Von altekischt im Forum Elektronik
    Antworten: 0
    Letzter Beitrag: 27.11.2012, 18:44
  2. EAGLE Forward Backward Net Class
    Von magic33 im Forum Konstruktion/CAD/3D-Druck/Sketchup und Platinenlayout Eagle & Fritzing u.a.
    Antworten: 2
    Letzter Beitrag: 19.11.2007, 12:06
  3. Class AB Verstärker
    Von Johannes G. im Forum Elektronik
    Antworten: 25
    Letzter Beitrag: 03.02.2007, 12:44
  4. Class Wizard
    Von blizzard im Forum PC-, Pocket PC, Tablet PC, Smartphone oder Notebook
    Antworten: 3
    Letzter Beitrag: 21.08.2005, 09:54

Stichworte

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

LiFePO4 Speicher Test