- Labornetzteil AliExpress         
Ergebnis 1 bis 2 von 2

Thema: Change member of a class from another class

Baum-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #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
  •  

Labornetzteil AliExpress