Results 1 to 2 of 2

Thread: Static field and public method

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Static field and public method

    For some reason this code doesn't work

    class Media
    {
    private:
    static int number;

    public:
    void addMediaType();
    };

    void Media :: addMediaType()
    {
    number=1;
    }

    How can I refer to my static number from my public method ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Static field and public method

    Quote Originally Posted by probine
    How can I refer to my static number from my public method ?
    Media::number = 1;

    But you have to initialise it somewhere first. The initialisation should take place outside of any class member and should be done only once, something like that:

    Qt Code:
    1. //header
    2. class X{
    3. static int var;
    4. };
    5.  
    6. //implementation
    7. int X::var = 1;
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.