2012-06-02 33 views
5

Şu anda C++ 'da aşırı yükleme operatörlerini çalıştırıyorum ve bir sorunum var. Dize sınıfı oluşturdum, sadece alanlara sahip biri var, diğeri ise karakterdir. Bir String "Alice bir kedisi var" veC++ ve dereference'de aşırı yükleme operatörü

cout<<moj[2]; 

demenden 'i' almak istiyorum, ama Adalet Bakanlığı şimdi alıyorum moj + 16U'ya adresi + 2 sizeof (String) zaman Ben buna Şaud'un ama Aşırı yüklü operatör tanımında buna KQUEUE istediğiniz şekilde çalışır

cout<<(*moj)[2]; 

diyoruz. Birçok şey denedim ama çözüm bulamıyorum. Lütfen düzelt beni. Tüm kod, önemli şeyler sayfa aşağıdır. Derleme ve çalışıyor. yapılamaz

#include <iostream> 
    #include <cstdio> 
    #include <stdio.h> 
    #include <cstring> 
    using namespace std; 

class String{ 
public: 

//THIS IS UNIMPORTANT------------------------------------------------------------------------------ 
char* napis; 
int dlugosc; 
    String(char* napis){ 
    this->napis = new char[20]; 
    //this->napis = napis; 
    memcpy(this->napis,napis,12); 
    this->dlugosc = this->length(); 
} 

    String(const String& obiekt){ 
    int wrt = obiekt.dlugosc*sizeof(char); 
    //cout<<"before memcpy"<<endl; 
    this->napis = new char[wrt]; 
    memcpy(this->napis,obiekt.napis,wrt); 

    //cout<<"after memcpy"<<endl; 
    this->dlugosc = wrt/sizeof(char); 
    } 

    ~String(){ 
    delete[] this->napis; 
    } 

    int length(){ 
    int i = 0; 
    while(napis[i] != '\0'){ 
     i++; 
    } 
    return i; 
    } 
     void show(){ 
     cout<<napis<<" dlugosc = "<<dlugosc<<endl; 
} 


//THIS IS IMPORTANT 
    char & operator[](int el) {return napis[el];} 
    const char & operator[](int el) const {return napis[el];} 
}; 


    int main() 
    { 

    String* moj = new String("Alice has a cat"); 
    cout<<(*moj)[2]; // IT WORKS BUI 
// cout<<moj[2]; //I WOULD LIKE TO USE THIS ONE 


    return 0; 
    } 
+0

Sen' olmalıdır Moj silin:

String moj = String("Alice has a cat"); // note lack of * and new cout << moj[2]; 

Ayrıca new ihtiyaçları olan sen tahsis herhangi bir şeyin not sonra silinecek. – Matt

+0

En az kodu bu yüzden sildim. – Yoda

cevap

8
String* moj = new String("Alice has a cat"); 
cout<<(*moj)[2]; // IT WORKS BUI 
// cout<<moj[2]; //I WOULD LIKE TO USE THIS ONE 

, sonraki durumda simge operatörün bir işaretçi uygulanır. Yalnızca argümanlardan en az birinin kullanıcı tanımlı tipte (veya buna bir referans, ancak bir işaretçi değilken) yüklenmesi; Bu durumda, argümanlar her iki temel tipte String* ve 2'dur. Eğer String kendisi sizi bir şey yapmak istiyorsanız,

String moj("Alice has a cat"); 
// cout<<(*moj)[2]; <-- now this doesn't work 
cout<<moj[2]; // <-- but this does 
3

String * bir String bir işaretçi anlamı: Eğer yapabilir Ne

, bunu neden ihtiyaç görmüyorum tamamen işaretçi damla olduğunu *moj ile dereferans etmek zorunda. Bunun yerine ne yapabilirsiniz şudur:; dönmeden önce `

String *x = new String("foo"); 

// code 

delete x; 
İlgili konular