Eigene String Klasse



  • Hey liebes Forum,

    wenn ich meine eigene String-Klasse programmieren möchte, wie kann ich dann eine Membervariable (nämlich den String) zurückgeben, ohne eine Funktion aufzurufen?
    Bei der std::string Klasse geht es ja so:

    std::string AnyString = "Bla Bla Bla";
    if (/* hier wird halt der String zurückgegeben */ AnyString == "Bla Bla Bla")
     TuDiesDas();
    

    Ich hoffe, Ihr habt meine Frage verstanden 😃

    MFG DerCoder



  • Du musst die Operatoren überladen. In deinem Beispiel den operator==

    http://www.c-plusplus.net/forum/232010



  • Hier ein kleines Beispiel, welches einen std::string in einer Klasse kapselt und die Vergleichsoperatoren überlädt, damit ein Vergleich möglich ist.

    Es handelt sich dabei natürlich nur um ein Beispiel, welches zeigen soll, wie es gemacht wird.

    #include <string>
    #include <iostream>
    
    class MyString
    {
    	private:
    		std::string str_;
    
    	public:
    		MyString(const std::string& str) : str_(str)
    		{
    		}
    
    		friend bool operator==(const MyString& lhs, const MyString& rhs);
    		friend bool operator==(const MyString& lhs, const char * rhs);
    		friend bool operator==(const char *lhs, const MyString& rhs);
    
    		friend bool operator!=(const MyString& lhs, const MyString& rhs);
    		friend bool operator!=(const MyString& lhs, const char * rhs);
    		friend bool operator!=(const char *lhs, const MyString& rhs);
    };
    
    bool operator==(const MyString& lhs, const MyString& rhs)
    {
    	return lhs.str_ == rhs.str_;
    }
    
    bool operator==(const MyString& lhs, const char * rhs)
    {
    	return lhs.str_ == rhs;
    }
    
    bool operator==(const char *lhs, const MyString& rhs)
    {
    	return rhs.str_ == lhs;
    }
    
    bool operator!=(const MyString& lhs, const MyString& rhs)
    {
    	return !(lhs == rhs);
    }
    
    bool operator!=(const MyString& lhs, const char * rhs)
    {
    	return !(lhs == rhs);
    }
    
    bool operator!=(const char *lhs, const MyString& rhs)
    {
    	return !(rhs == lhs);
    }
    
    int main()
    {
    	MyString test("Test");
    
    	if (test == "Test")
    	{
    		std::cout << "Ja" << std::endl;
    	}
    
    	return 0;
    }
    


  • Ganz schön unhandlich. Warum muss op!= ein friend sein, wenn er nur auf op== zugreift?

    #include <string>
    #include <iostream>
    
    class MyString
    {
    	friend bool operator==(const MyString& lhs, const MyString& rhs);
        std::string str_;
    
    public:
        MyString(const char* str) : str_(str)
        {
        }
    };
    
    bool operator==(const MyString& lhs, const MyString& rhs)
    {
        return lhs.str_ == rhs.str_;
    }
    
    bool operator!=(const MyString& lhs, const MyString& rhs)
    {
        return !(lhs == rhs);
    }
    
    int main()
    {
        MyString test("Test");
    
        if (test == "Test")
            std::cout << "Ja" << std::endl;
    }
    


  • Okay danke schonmal für die vielen Beiträge!

    Habe die Frage falsch formuliert 😕
    Wenn ich z. B. meinen String in der Konsole ausgeben möchte (bei std::string reicht: )

    cout << MeinString;
    

    Wie kann ich das bei meiner String-Klasse machen?



  • << überladen...

    friend ostream& operator << (ostream&, const MyString&);
    

Anmelden zum Antworten