Results 1 to 4 of 4

Thread: const function parameter problems

  1. #1
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default const function parameter problems

    Hi,

    I've been having some issues with the const keyword on preceding parameters so functions.
    I'm writing some Qt code using QList. The code I've written was producing errors asking for me to implement a copy constructor and operator= with a const parameter for my classes.

    Here's the operator overload code for my ShootVersion class:
    Qt Code:
    1. ShootVersion& ShootVersion::operator=(const ShootVersion& other)
    2. {
    3. if (this != &other)
    4. {
    5.  
    6. }
    7.  
    8. return *this;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Stock standard stuff.
    When I try to implement the copying of members from other I run into a snag.
    simply calling any of other's functions produces errors, for example:
    Qt Code:
    1. otherVersion.Filename();
    2.  
    3. ...produces...
    4.  
    5. error C2662: 'ShootVersion::Filename' : cannot convert 'this' pointer from 'const ShootVersion' to 'ShootVersion &'
    To copy to clipboard, switch view to plain text mode 
    I figured that the const was causing an issue seeing as it prevents modification to the parameter (although I'm only reading from it so I'm confused about this), so I tried the following before the call:
    Qt Code:
    1. ShootVersion otherVersion( other );
    To copy to clipboard, switch view to plain text mode 
    That compiles fine, but the problem here is that I run into the same issue as the convention is to declare the parameter to the copy constructor as const as well, so I can't do the copy I'm after.

    All the examples I've found thus far online suggest that what I'm doing is legal, but maybe there's something I'm missing.

    Can anyone point out what I need to be doing?
    I'm using Visual Studio 2k3 for this project (maybe it's a compiler issue?)

    Thanks,

    Stephen York



    Just for convenience here's the class I'm implementing:
    Qt Code:
    1. #ifndef SHOOTVERSION_H
    2. #define SHOOTVERSION_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QStringList>
    7.  
    8.  
    9. class ShootVersion : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. ShootVersion(const ShootVersion& other);
    15. ShootVersion(QObject *parent = 0);
    16. ~ShootVersion();
    17.  
    18. QString Filename();
    19. void SetFilename(const QString& filename);
    20.  
    21. bool Download();
    22. void SetDownload(bool download);
    23.  
    24. QStringList Images();
    25. void SetImages(const QStringList& images);
    26. void AddImage(const QString& filename);
    27.  
    28.  
    29. ShootVersion& operator=(const ShootVersion& other);
    30.  
    31.  
    32. protected:
    33. QString _filename;
    34. bool _download;
    35. QStringList _images;
    36.  
    37. };
    38.  
    39. #endif // SHOOTVERSION_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "shootversion.h"
    2.  
    3. ShootVersion::ShootVersion(const ShootVersion& other)
    4. {
    5. _filename = other.Filename();
    6. }
    7.  
    8. ShootVersion::ShootVersion(QObject *parent)
    9. : QObject(parent)
    10. {
    11.  
    12. }
    13.  
    14. ShootVersion::~ShootVersion()
    15. {
    16.  
    17. }
    18.  
    19. QString ShootVersion::Filename()
    20. {
    21. return _filename;
    22. }
    23.  
    24. void ShootVersion::SetFilename(const QString& filename)
    25. {
    26. _filename = filename;
    27. }
    28.  
    29. bool ShootVersion::Download()
    30. {
    31. return _download;
    32. }
    33.  
    34. void ShootVersion::SetDownload(bool download)
    35. {
    36. _download = download;
    37. }
    38.  
    39. QStringList ShootVersion::Images()
    40. {
    41. return _images;
    42. }
    43.  
    44. void ShootVersion::SetImages(const QStringList& images)
    45. {
    46. _images = images;
    47. }
    48.  
    49. void ShootVersion::AddImage(const QString& filename)
    50. {
    51. _images.append(filename);
    52. }
    53.  
    54. ShootVersion& ShootVersion::operator=(const ShootVersion& other)
    55. {
    56. if (this != &other)
    57. {
    58. ShootVersion otherVersion( other );
    59. _filename = otherVersion.Filename();
    60. _download = otherVersion.Download();
    61. _images = otherVersion.Images();
    62. }
    63.  
    64. return *this;
    65. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: const function parameter problems

    The coping of QObjects is disabled by declaring it private. AFAIK even if you override the = operators it wouldn't help. This topic has been discussed earlier. So please search the forum
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    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: const function parameter problems

    Sunil already pointed out the main problem, but I'll add a few words about it why it doesn't compile. Your problem is that your getter methods are not marked as const, hence the compiler thinks they will modify the object which violates the const keyword in the copy constructor. Your getter methods need to look like this (note the const keyword):

    Qt Code:
    1. QStringList ShootVersion::Images() const {
    2. return _images;
    3. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to wysota for this useful post:

    stevey (18th December 2006)

  5. #4
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: const function parameter problems

    Thanks Wysota,

    I was hoping it was a simple fix like that. "It's all very clear to me now" in the words of David Bowman.
    I never really understood why you'd need to declare a member function as const ( other than for our own safety ), but it makes sense to me now that the compiler wouldn't know if modification would occur.

    I just figured the const parameter would be guarding reassignment of the object or public variables and it would care about the methods being called.


    Thanks again,

    Stephen York

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  4. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52

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.