static default parameter



  • class Color
    {
    public:
    	Color(int red,int blue = 255);
    	void set(int red,int blue = bl);
    
    private:
    	static int bl;
            int blue;
            int red;
    
    };
    
    Color::Color(int r,int b):
      red(r),
      blue(b)
    {bl = blue}
    
    Color::set(int r,int b){
      red = r;
      blue = b;
      bl = blue;
    }
    

    ich hab hier probleme mit der static variable,
    aber ich braucht die static variable für den default parameter



  • Hallo

    class Color
    {
    public:
        Color(int red,int blue = 255);
        void set(int red,int blue = bl);
    
    private:
        static int bl;
            int blue;
            int red;
    
    };
    
    Color::Color(int r,int b):
      red(r),
      blue(b)
    {bl = blue;} // Fehlendes Semikolon
    
    void Color::set(int r,int b){ // void fehlt
      red = r;
      blue = b;
      bl = blue;
    }
    
    int Color::bl; // Deklarierung fehlt
    

    Beim nächsten Mal bitte konkrete Fehlermeldung mit angeben

    bis bald
    akari



  • Was heißt, du brauchst die Variable? So, wie du dir das gedacht hast, geht das nicht - vor allem weil die Default-Parameter bereits vom Compiler eingesetzt werden und deshalb (afaik) nur konstante Werte erlaubt sind.

    Als Alternative könntest du einen ungültigen Wert übergeben für den Fall, daß der blau-Wert nicht geändert werden soll. Oder du verzichtest auf Default-Parameter und nutzt stattdessen Überladung.


  • Mod

    user91 schrieb:

    class Color
    {
    public:
    	Color(int red,int blue = 255);
    	void set(int red,int blue = bl);
    	
    
    private:
    	static int bl;
            int blue;
            int red;
    
    };
    
    Color::Color(int r,int b):
      red(r),
      blue(b)
    {bl = blue}
    
    Color::set(int r,int b){
      red = r;
      blue = b;
      bl = blue;
    }
    

    ich hab hier probleme mit der static variable,
    aber ich braucht die static variable für den default parameter

    welche Probleme? Abgesehen von den offensichtlichen Syntaxfehlern ist das alles erlaubt.



  • upps...
    das mit den semekolon hatte ich :p

    aber die deklaration nicht

    thx



  • AFAIK kann man als Default Parameter sogar Funktionen aufrufen und solche Sachen.



  • Nagut, wenn du das sagst, will ich dir mal glauben. Die Konstruktion finde ich trotzdem fragwürdig.

    @user: Ich hoffe, dir ist klar, wie weit derArm einer static-Variable reicht:

    Color cRot(255,0),cBlau(0,255);//am Ende ist bl==255
    cRot.set(128);                 //setzt cRot.red=128 UND cRot.blue=255
    

Anmelden zum Antworten