Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
OT: C++ Membervariable als Referenz [solved]
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
manuels
Advocate
Advocate


Joined: 22 Nov 2003
Posts: 2146
Location: Europe

PostPosted: Fri Sep 18, 2009 9:57 am    Post subject: OT: C++ Membervariable als Referenz [solved] Reply with quote

Hi,

mal etwas anderes: Ein C++-Problem.

Ich habe folgenden Code:
Code:
using namespace std;

class Klasse1 {
  int wert1;

public:
  Klasse1() {
    wert1 = 9999;
  }
 
  int& getWert() { return wert1; }
};

class Klasse2 {
  int wert2;

public:
  Klasse2(Klasse1* k1) {
    wert2 = k1->getWert();
  }
};

int main() {
  Klasse1* k1 = new Klasse1();

  Klasse2 k2(k1);
}


Welcher auch wunderbar funkioniert.

Nun soll allerdings, wenn sich der Wert von Klasse1::wert1 aendert "automatisch" auch der Wert von Klasse2::wert2 aendern.
Das macht man naetuerlich mit einer Referenz.
Also wird Klasse2 so abgeandert:
Code:
class Klasse2 {
  int &wert2; // <- Hieraus wird eine Referenz

public:
  Klasse2(Klasse1* k1) {
    wert2 = k1->getWert();
  }
};


Doch leider mag das der Gnu-C++-Kompiler (und wohl auch jeder andere Kompiler) nicht:
Code:
test.cpp:18: error: uninitialized reference member `Klasse2::wert2'


Er maekert, dass Klasse2::wert2 nicht initialisiert wurde. Das kann ich aber erst im Konstruktor machen, oder?
Er gibt mir keine Chance das Ding zu initialisieren!

Folgender Code in der main() funkioniert:
Code:
  int &wert3 = k1->getWert();


Ich moechte aber keine Referenz in der main()-Funkion, sondern in der Klasse2.
Gibt es irgendeine Moeglichkeit dies zu realisieren?
_________________
Build your own live cd with catalyst 2.0!


Last edited by manuels on Fri Sep 18, 2009 2:11 pm; edited 1 time in total
Back to top
View user's profile Send private message
69719
l33t
l33t


Joined: 20 Sep 2004
Posts: 865

PostPosted: Fri Sep 18, 2009 11:43 am    Post subject: Reply with quote

Code:

#include <stdio.h>

using namespace std;

class Klasse1 {
   public:
      Klasse1( int value ) {
         wert1 = value;
      }

      int *getPointer( void ) {
         return &wert1;
      }

      int getWert( void ) {
         return wert1;
      }

   private:
      int wert1;
};
 
class Klasse2 {
   public:
      Klasse2( Klasse1* k1 ) {
         wert2 = k1->getPointer();
      }

      int getWert( void ) {
         return *wert2;
      }

   private:
      int *wert2;
};
 
int main( int argc, char *argv[] ) {
   Klasse1 k1( 9999 );
   Klasse2 k2( &k1 );
   
   printf( "k1=%d\n", k1.getWert() );
   printf( "k2=%d\n", k2.getWert() );

   return 0;
}
Back to top
View user's profile Send private message
mrsteven
Veteran
Veteran


Joined: 04 Jul 2003
Posts: 1938

PostPosted: Fri Sep 18, 2009 12:57 pm    Post subject: Reply with quote

Oder du benutzt Initialisierungslisten:
Code:
using namespace std;

class Klasse1 {
        int wert1;

        public:
        Klasse1() {
                wert1 = 9999;
        }

        int& getWert() { return wert1; }
};

class Klasse2 {
        int &wert2;

        public:
        Klasse2(Klasse1* k1) :
        wert2(k1->getWert())          //Das hier!
        {
        }
};

int main() {
        Klasse1* k1 = new Klasse1();

        Klasse2 k2(k1);
}

_________________
Unix philosophy: "Do one thing and do it well."
systemd: "Do everything and do it wrong."
Back to top
View user's profile Send private message
manuels
Advocate
Advocate


Joined: 22 Nov 2003
Posts: 2146
Location: Europe

PostPosted: Fri Sep 18, 2009 2:11 pm    Post subject: Reply with quote

Ah, so klappt das.
Danke!
_________________
Build your own live cd with catalyst 2.0!
Back to top
View user's profile Send private message
Earthwings
Bodhisattva
Bodhisattva


Joined: 14 Apr 2003
Posts: 7753
Location: Germany

PostPosted: Sun Sep 20, 2009 7:31 am    Post subject: Reply with quote

Eigentlich kannst Du k1 auch auf dem Stack anlegen, dann musst Du den Speicher nicht selbst freigeben.
Code:
Klasse1 k1;
Klasse2 k2(&k1);

_________________
KDE
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum