statischen member in der klasse ansprechen



  • ist es möglich einen statischen member inerhalb der klasse zu ändern?



  • Hä?
    Einfach sowas oder was?

    struct c
    {
        static int x;
        c() { ++x; }
    };
    
    int c::x = 0;
    

    natürlich geht das 😕
    Warum sollte das nicht gehn?
    Oder wie meinst du das?



  • nein ich mein

    class test{
    static int i;
    void func(){
    i=9;
    }

    }
    klappt bei mir irgendwie nicht.



  • Du musst die statische Membervariable natürlich auch initialisieren.
    Und "geht nicht" heißt nix, das hier geht bei mir:

    #include <iostream>
    
    class c
    {
        public:
        static int i;
        void func()
        {
            i = 3;
        }
    };
    
    int c::i = 2;
    
    int main()
    {
        c x;
        x.func();
        std::cout << c::i; //3
    }
    


  • dein beispiel geths, aber meins mekert immer wegen Nicht aufgelöstes externes Symbol ""public: static int control::number" (?number@control@@2HA)".

    class control{
    public:
    	static int number;
    	int adress;
    	bool visible;
    	bool active;
    
    	control();
    	virtual void render();
    	virtual void process();
    
    };
    	control::control(){
    		number=0;
    
    	}
    	void  control::render(){
    		//printf("fail/n");
    
    	}
    	void control::process(){
    	}
    

    ich seh den fehler nich



  • Wo initialisierst du number?

    Du musst irgendwo außerhalb der Klasse so wie ich Datentyp Klassenname::Variablenname = Wert; schreiben.
    Das machste genau einmal.

    Also einfach

    class control{
    public:
        static int number;
        int adress;
        bool visible;
        bool active;
    
        control();
        virtual void render();
        virtual void process();
    
    };
    
        int control::number = 0;
        control::control(){
            number=0;
    
        }
        void  control::render(){
            //printf("fail/n");
    
        }
        void control::process(){
        }
    


  • super, danke.


Anmelden zum Antworten