Results 1 to 17 of 17

Thread: C++ readonly property

  1. #1
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Question C++ readonly property

    Hi, I want to implement a read only property for a class, can anyone show me how to do this?

    Currently I am using a function, is this the correct way? Can anyone explain what is this "const" means for function return? I just put there, it compiles, but have no clue what it will do? I guess it will stop user from changing the return value?
    Class A
    {
    private:
    QString myID;

    public QString ID() const {return myID;}
    }
    Last edited by yyiu002; 20th June 2010 at 22:05.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: C++ readonly property

    1) make the data you want to protect protected, or private.

    2) create accessor functions that allow you to read the data, but not write it or change it in any way.

  3. The following user says thank you to SixDegrees for this useful post:

    yyiu002 (21st June 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    I just updated showed my code, is it the correct way?

  5. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: C++ readonly property

    Yes, that will work.

  6. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ readonly property

    Yes, it will indeed work, but now you have a class A, with a empty QString as a member, and a function that allways returns an empty QString... this doesn't sound very usefull

    So i guess you will need the constructor to initialize the QString member, or another method to create a usefull string in there.

  7. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    const functions just means that the function will not modify any class variables.

  8. The following user says thank you to squidge for this useful post:

    yyiu002 (21st June 2010)

  9. #7
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    Quote Originally Posted by fatjuicymole View Post
    const functions just means that the function will not modify any class variables.
    What is the difference between following 2 lines?

    Thanks.

    public QString ID() const {return myID;}

    public const QString ID() {return myID;}

  10. #8
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    I did this in my constructor: A::A(QString comPort, QString baudRate):myID(comPort+":"+baudRate)

    But still a worry that myID might not be unique, I have create a list, each time adding an instance to check my ID whether is unique? Is there a better way to do this?

    At C#, I might have a static constructor, generate my own ID each time when class get instantialized.

    At C#:
    Class A{

    int myID;

    static A(){
    myID++;
    }
    }

    How can I do similar thing in C++?

  11. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ readonly property

    public: QString ID() const {return myID;} // this is a function declared const fatjuicymole allready explained what this is

    public: const QString ID() {return myID;} // this is a non-const function that returns a const QString

    In c++ you can do something like:
    Qt Code:
    1. class A{
    2. static int myID;
    3. public:
    4. A(){
    5. myID++;
    6. }
    7. //...
    8. };
    To copy to clipboard, switch view to plain text mode 
    and outside of class you will need to initialize the static variable, like this:
    Qt Code:
    1. int A::myID = 0;
    To copy to clipboard, switch view to plain text mode 
    Last edited by Zlatomir; 21st June 2010 at 00:54.

  12. The following user says thank you to Zlatomir for this useful post:

    yyiu002 (21st June 2010)

  13. #10
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    Quote Originally Posted by Zlatomir View Post
    public: QString ID() const {return myID;} // this is a function declared const fatjuicymole allready explained what this is

    public: const QString ID() {return myID;} // this is a non-const function that returns a const QString

    In c++ you can do something like:
    Qt Code:
    1. class A{
    2. static int myID;
    3. public:
    4. A(){
    5. myID++;
    6. }
    7. //...
    8. };
    To copy to clipboard, switch view to plain text mode 
    and outside of class you will need to initialize the static variable, like this:
    Qt Code:
    1. int A::myID = 0;
    To copy to clipboard, switch view to plain text mode 
    Is this empty constructor gets called every time when I initialize it with a different constructor with parameters like following:

    static int myID;
    public:
    A(QString myName){
    }
    //...
    };

    Thank you for your reply.

  14. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ readonly property

    No, only one constructor it's called (for each created object)

    So you will need to increase that variable in each of them (and maybe you will want to define a copy constructor to increase the ID in case someone create a object by making a copy of one that already exist, because the default generated copy constructor won't increase that variable)

  15. #12
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    Quote Originally Posted by Zlatomir View Post
    No, only one constructor it's called (for each created object)

    So you will need to increase that variable in each of them (and maybe you will want to define a copy constructor to increase the ID in case someone create a object by making a copy of one that already exist, because the default generated copy constructor won't increase that variable)
    Sorry I am little dummy on copy constructor? Please show me some code on how to do this.
    Many thanks.

  16. #13
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    with line : int A::myID = 0;
    When I put it inside header file where class A was defiined, I get error like: multiple definition of `A::myID', why is that? I am sure there are "#ifndef A_H
    #define A_H " to stop class A was included more than once.

  17. #14
    Join Date
    Jan 2010
    Location
    Perth, Australia
    Posts
    37
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ readonly property

    Quote Originally Posted by yyiu002 View Post
    with line : int A::myID = 0;
    When I put it inside header file where class A was defiined, I get error like: multiple definition of `A::myID', why is that? I am sure there are "#ifndef A_H
    #define A_H " to stop class A was included more than once.
    The #ifndef makes sure that each .cpp file only includes one copy of the header. But if you have 5 .cpp files that have "#include a.h", then the header will be included in 5 files, so A::myID will be defined 5 times. To fix that, move "int A::myID = 0" from the header into one of your .cpp files (but put it outside a function, just like how you define a global variable).

    Also, here is a comprehensive article that describes all the ways to use "const" in C++: http://duramecho.com/ComputerInforma...wCppConst.html

    You only need a copy constructor if you want to make a copy of an existing object. If you do, have a look at http://171.64.64.250/class/cs193d/ha...nstructors.pdf

  18. #15
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ readonly property

    Quote Originally Posted by yyiu002 View Post
    with line : int A::myID = 0;
    When I put it inside header file where class A was defiined, I get error like: multiple definition of `A::myID', why is that? I am sure there are "#ifndef A_H
    #define A_H " to stop class A was included more than once.
    You got to learn about c++ way of doing this things, so you create a header file (a.h) for the class definition (here you don't write the implementation of the functions, just the definition) and you write a second file (a.cpp) that include the a.h and here you write the implementation (it's not like in c#) and don't forget about the include guards (they are not useless)
    Example:
    Qt Code:
    1. //a.h file
    2. #ifndef A_H
    3. #define A_H
    4. class A{
    5. static int myID;
    6. int member;
    7. public:
    8. A();
    9. A(A&); //copy c-tor
    10. int id(); // function that returns the id
    11. // rest of the class definition
    12. //...
    13. };
    14. #endif // end of define A_H
    To copy to clipboard, switch view to plain text mode 
    and the cpp file:
    Qt Code:
    1. //a.cpp
    2. #include "a.h"
    3.  
    4. int A::myID = 0;
    5.  
    6. A::A(){
    7. myID++;
    8. }
    9.  
    10. A::A(A& ref){ //implementation of copy constructor
    11. // here you copy the members from the ref object passed to the constructor
    12. member = ref.member; //just an example here member is uninitialized... you need to take care of all the data members
    13.  
    14. myID++; // and increment the static variable...
    15. }
    16. int A::id() {
    17. return myID;}
    To copy to clipboard, switch view to plain text mode 

  19. #16
    Join Date
    Jun 2010
    Posts
    32
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: C++ readonly property

    At article http://www.codersource.net/c/c-misce...structors.aspx
    it says following

    There are 3 important places where a copy constructor is called.

    1. When an object is created from another object of the same type
    2. When an object is passed by value as a parameter to a function
    3. When an object is returned from a function

    case 2 and 3 could be a problem.

  20. #17
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: C++ readonly property

    Quote Originally Posted by yyiu002 View Post
    case 2 and 3 could be a problem.
    Why is case 2 and 3 problem?

Similar Threads

  1. Replies: 3
    Last Post: 3rd September 2012, 07:32
  2. ReadOnly problem for QTableView
    By omprakash in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2008, 16:02
  3. Replies: 4
    Last Post: 6th February 2008, 14:15
  4. Readonly Model implementation requirements for QComboBox
    By DeepDiver in forum Qt Programming
    Replies: 6
    Last Post: 8th November 2007, 17:10
  5. How to set a column readonly in qtablewidgetitem
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th November 2007, 13:23

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.