this->
-
hi
ich verstehe nicht ganz den sinn davon.
kann mir jemand das erkläre ?
thx schonmal
-
es gibt keinen
-
gibt es keinen sinn ?
-
höchstens um Namenskonflikte zu beseitigen:
void Foo::bar(int baz) { this->baz = baz; // do something more }
-
this-Zeiger:
http://www.mathematik.uni-marburg.de/~cpp/operatoren/this_1.html
http://www.mi.uni-koeln.de/c/mirror/f7alpha1.informatik.fh-muenchen.de/~schieder/programmieren-2-ss97/ptr.html#section_12_10
http://home.arcor.de/cpp_kurs/cpp/le10/k3_08_01.htm
http://fb1.hdm-stuttgart.de/cpp/node13.html
http://www.schornboeck.net/ckurs/this.htm
-
*gähn*
-
Hallo,
für abhängige Namen in Template-Klassen-Vererbungs-Situationen finde es auch ganz schick:template <class T> struct Base { void func() {} }; template <class T> struct Foo : Base<T> { void gunc() { func(); // Fehler! func() nicht deklariert this->func(); // ok Base<T>::func(); // auch ok } };
-
*gähn*
-
Beispiele für this:
http://www.tutorialpage.de/Cpp2/chap3.phpHier noch ein Beispiel für die Verwendung von this (Klasse rational bei den Funktionen anders implementiert als bei Volkard):
s.u.
-
dieses beispiel ist doch voellig verbloeded. du markierst membervariablen einerseits durch _so und andererseits durch this->? was soll das? stehst du auf kryptischen code?
und im konstruktor verwendest du es nicht. warum so inkonsequent? wenn du wenigstens eine initialisierungsliste genommen haettest.
das einzig sinnvolle an dem beispiel ist return*this; da muss man hier aber gesondert drauf hinweisen, sonst sieht man es gar nicht.
-
Sorry, hatte einen unfertigen Zwischenschritt von this->z (nur zu Demonstrationszwecken üblich) nach _z erwischt, wurde gelöscht.
"return *this;" ist das Entscheidende.#include <iostream> #include <conio.h> class rational { private: int _z; int _n; public: //Ctor rational(); rational( int z, int n ); //Dtor ~rational(); //IO rational& eingabe(); void ausgabe(); //math rational& add( const rational& r1, const rational& r2); rational& sub( const rational& r1, const rational& r2); rational& mul( const rational& r1, const rational& r2); rational& div( const rational& r1, const rational& r2); }; rational::rational(){} rational::rational( int z, int n ) { _z = z; _n = n; } rational& rational::eingabe() { std::cin >> _z; std::cin >> _n; return *this; } rational::~rational() {} void rational::ausgabe() { std::cout << _z << "/" << _n << std::flush; } rational& rational::add( const rational& r1, const rational& r2 ) { _z = r1._z * r2._n + r1._n * r2._z; _n = r1._n * r2._n; return *this; } rational& rational::sub( const rational& r1, const rational& r2 ) { _z = r1._z * r2._n - r1._n * r2._z; _n = r1._n * r2._n; return *this; } rational& rational::mul( const rational& r1, const rational& r2) { _z = r1._z * r2._z; _n = r1._n * r2._n; return *this; } rational& rational::div( const rational& r1, const rational& r2) { _z = r1._z * r2._n; _n = r1._n * r2._z; return *this; } int main() { rational a(5,9); a.ausgabe(); std::cout << std::endl; rational b; std::cout << "Bitte Rationale Zahl eingeben (Zaehler (Space) Nenner): "; b.eingabe(); b.ausgabe(); std::cout << std::endl; rational c; c.add(a,b); c.ausgabe(); std::cout << std::endl; rational d; d.sub(a,b); d.ausgabe(); std::cout << std::endl; rational e; e.mul(a,b); e.ausgabe(); std::cout << std::endl; rational f; f.div(a,b); f.ausgabe(); std::cout << std::endl; getch(); return 0; }