referenzen & klassen
-
class Secret { private: char wepkey[100]; public: Secret::Secret(const char *set_wepkey) { memset(&wepkey,0,wepkey); strcpy(wepkey,set_wepkey); } }; class Foo { private: Secret *ptr_Secret; // Pointer auf Secret public: void setInstance(const Secret ptr) { ptr_Secret = ptr; } char &getwepkeyInstance() { return ptr_Secret->wepkey; } }; int main() { // Referenz auf Instanz ptr_s Secret &ptr_s = new Secret("codename_matrix"); // Referenz auf Instanz ptr_f Foo &ptr_f = new Foo; ptr_f->setInstance(*ptr_s); cout << ptr_f->getwepkeyInstance() << endl; return 0; }
ho
Eine Referenz wird ja einmal bei der Initialsierung an ein Objekt gebunden und kann dann nie mehr geändert werden.
Eine Referenz auf eine Instanz macht ja Sinn wenn ich quasi einen kostanten Zeiger haben will?
die übergabe des objektes an die andere klasse müsste ok sein?greets
-
sicher das du
public: Secret::Secret(const char *set_wepkey)
und nicht
public: Secret(const char *set_wepkey)
meinst
-
[cpp]class Secret
{private:
char wepkey[100];public:
Secret(const char set_wepkey)
{
memset(&wepkey, 0, /!!!*/sizeof(wepkey));
strcpy(wepkey,set_wepkey);
}/!!!/
const char *get_wepkey() const
{
return wepkey;
}};class Foo
{
private:
*/!!!/const Secret ptr_Secret; // ConstPointer auf Secretpublic:
void setInstance(const Secret */!!!/ptr)
{
ptr_Secret = ptr;
}const char *getwepkeyInstance() /*!!!/const
{
/!!!*/
return ptr_Secret->get_wepkey();
}};int main()
{
// Pointer auf Instanz ptr_s
/!!!/
*Secret ptr_s = new Secret("codename_matrix");// Referenz auf Instanz ptr_f
/!!!/
*Foo ptr_f = new Foo;/!!!/
ptr_f->setInstance(ptr_s);/!!!/
std::cout << ptr_f->getwepkeyInstance() << std::endl;/!!!/
delete ptr_s;/!!!/
delete ptr_f;return 0;
} [/cpp]oder einfacher:
#include <memory> #include <string> #include <iostream> class Secret { private: std::string wepkey; public: Secret(const char *set_wepkey) : wepkey(set_wepkey) { } const char *get_wepkey() const { return wepkey.c_str(); } }; class Foo { private: const Secret *ptr_Secret; // Pointer auf Secret public: void setInstance(const Secret *ptr) { ptr_Secret = ptr; } const char *getwepkeyInstance() /*!!!*/const { return ptr_Secret->get_wepkey(); } }; int main() { std::auto_ptr<Secret> ptr_s(new Secret("codename_matrix")); std::auto_ptr<Foo> ptr_f(new Foo); ptr_f->setInstance(ptr_s.get()); std::cout << ptr_f->getwepkeyInstance() << std::endl; return 0; }
-
ho!
es geht mir jetzt nicht um den const pointer, sondern wollte da:Secret *ptr_s = new Secret("codename_matrix"); oo *ptr_f = new Foo;
mal mit referenzen auf ein objekt arbeiten...manchmal kann man ja sowas auch gebrauchen? könntest dann mal zeigen was man ändern muss?
cu
-
nachwuchscoder schrieb:
mal mit referenzen auf ein objekt arbeiten...manchmal kann man ja sowas auch gebrauchen? könntest dann mal zeigen was man ändern muss?
bestimmt
#include <memory> #include <string> #include <iostream> class Secret { private: std::string wepkey; public: Secret(const char *set_wepkey) : wepkey(set_wepkey) { } const char *get_wepkey() const { return wepkey.c_str(); } }; class Foo { private: const Secret &refSecret; // ConstReference auf Secret public: Foo(const Secret &refSecret) : refSecret(refSecret) { } const char *getwepkeyInstance() /*!!!*/const { return refSecret.get_wepkey(); } }; int main() { Secret s("codename_matrix"); Foo f(s); std::cout << f.getwepkeyInstance() << std::endl; return 0; }
-
nachwuchscoder schrieb:
ho!
es geht mir jetzt nicht um den const pointer, sondern wollte da:Secret *ptr_s = new Secret("codename_matrix"); oo *ptr_f = new Foo;
mal mit referenzen auf ein objekt arbeiten...
new liefert einen Zeiger, und Zeiger!=Referenz
lösung mit auto_ptr wurde schon genannt
-
nach deiner aussage, dürfte das ja nie funktionieren?
Secret &ref_s = new Secret("codename_matrix"); oder Secret &ref_s = Secret("codename_matrix");
cu
-
Secret &ref_s = *new Secret("codename_matrix");
gehört wohl so! macht sowas denn sinn?
cu
-
Secret &ref_s = new Secret("codename_matrix");
Erzeugt augenscheinlich ne Speicherleiche.
-
class Secret { private: char wepkey[100]; public: Secret(const char *set_wepkey) { memset(&wepkey, 0, sizeof(wepkey)); strcpy(wepkey,set_wepkey); } const char *get_wepkey() const { return wepkey; } }; class Foo { private: const Secret *ptr_Secret; // ConstPointer auf Secret public: void setInstance(const Secret *ptr) { ptr_Secret = ptr; } const char *getwepkeyInstance() const { return ptr_Secret->get_wepkey(); } }; int main() { // Referenz auf Instanz ptr_s Secret &ptr_s = Secret("codename_matrix"); // Referenz auf Instanz ptr_f Foo &ptr_f = Foo(); ptr_f.setInstance(&ptr_s); std::cout << ptr_f.getwepkeyInstance() << std::endl; std::cin.get(); return 0; }
was sagen die gurus dazu??
cu
-
nachwuchscoder schrieb:
//UB!!! initial value of reference to non-const must be an lvalue Secret &ptr_s = Secret("codename_matrix"); //UB!!! initial value of reference to non-const must be an lvalue Foo &ptr_f = Foo(); //UB!!! ptr_f.setInstance(&ptr_s); //UB!!! std::cout << ptr_f.getwepkeyInstance() << std::endl;
was sagen die gurus dazu??
undefined behavior
cu
-
ja....aber der code läuft...keinen error, keine exception!! bist nun platt was?
cu
-
nachwuchscoder schrieb:
ja....aber der code läuft...keinen error, keine exception!! bist nun platt was?
cu
Undefined behavior indicates that an implementation may behave unpredictably when a program reaches a certain state, which almost without exception is a result of a bug. Undefined behavior can be manifested as a run time crash, unstable and unreliable program state, or--in rare cases--it may even pass unnoticed. Examples of undefined behaviors include an attempt to write to a buffer past its boundary, accessing an out of range array subscript, de-referencing a dangling pointer or deleting a non-NULL pointer more than once.
Undefined behavior means that ANYTHING can happen! If you write i=i++, your computer can teleport itself to 16th century Wittenberg and change Western history by nailing sections 1.6 and 3.3 of ANSI X3.159 to the church door!