stevey
18th December 2006, 14:36
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:
ShootVersion& ShootVersion::operator=(const ShootVersion& other)
{
if (this != &other)
{
}
return *this;
}
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:
otherVersion.Filename();
...produces...
error C2662: 'ShootVersion::Filename' : cannot convert 'this' pointer from 'const ShootVersion' to 'ShootVersion &'
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:
ShootVersion otherVersion( other );
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:
#ifndef SHOOTVERSION_H
#define SHOOTVERSION_H
#include <QObject>
#include <QString>
#include <QStringList>
class ShootVersion : public QObject
{
Q_OBJECT
public:
ShootVersion(const ShootVersion& other);
ShootVersion(QObject *parent = 0);
~ShootVersion();
QString Filename();
void SetFilename(const QString& filename);
bool Download();
void SetDownload(bool download);
QStringList Images();
void SetImages(const QStringList& images);
void AddImage(const QString& filename);
ShootVersion& operator=(const ShootVersion& other);
protected:
QString _filename;
bool _download;
QStringList _images;
};
#endif // SHOOTVERSION_H
#include "shootversion.h"
ShootVersion::ShootVersion(const ShootVersion& other)
{
_filename = other.Filename();
}
ShootVersion::ShootVersion(QObject *parent)
: QObject(parent)
{
}
ShootVersion::~ShootVersion()
{
}
QString ShootVersion::Filename()
{
return _filename;
}
void ShootVersion::SetFilename(const QString& filename)
{
_filename = filename;
}
bool ShootVersion::Download()
{
return _download;
}
void ShootVersion::SetDownload(bool download)
{
_download = download;
}
QStringList ShootVersion::Images()
{
return _images;
}
void ShootVersion::SetImages(const QStringList& images)
{
_images = images;
}
void ShootVersion::AddImage(const QString& filename)
{
_images.append(filename);
}
ShootVersion& ShootVersion::operator=(const ShootVersion& other)
{
if (this != &other)
{
ShootVersion otherVersion( other );
_filename = otherVersion.Filename();
_download = otherVersion.Download();
_images = otherVersion.Images();
}
return *this;
}
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:
ShootVersion& ShootVersion::operator=(const ShootVersion& other)
{
if (this != &other)
{
}
return *this;
}
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:
otherVersion.Filename();
...produces...
error C2662: 'ShootVersion::Filename' : cannot convert 'this' pointer from 'const ShootVersion' to 'ShootVersion &'
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:
ShootVersion otherVersion( other );
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:
#ifndef SHOOTVERSION_H
#define SHOOTVERSION_H
#include <QObject>
#include <QString>
#include <QStringList>
class ShootVersion : public QObject
{
Q_OBJECT
public:
ShootVersion(const ShootVersion& other);
ShootVersion(QObject *parent = 0);
~ShootVersion();
QString Filename();
void SetFilename(const QString& filename);
bool Download();
void SetDownload(bool download);
QStringList Images();
void SetImages(const QStringList& images);
void AddImage(const QString& filename);
ShootVersion& operator=(const ShootVersion& other);
protected:
QString _filename;
bool _download;
QStringList _images;
};
#endif // SHOOTVERSION_H
#include "shootversion.h"
ShootVersion::ShootVersion(const ShootVersion& other)
{
_filename = other.Filename();
}
ShootVersion::ShootVersion(QObject *parent)
: QObject(parent)
{
}
ShootVersion::~ShootVersion()
{
}
QString ShootVersion::Filename()
{
return _filename;
}
void ShootVersion::SetFilename(const QString& filename)
{
_filename = filename;
}
bool ShootVersion::Download()
{
return _download;
}
void ShootVersion::SetDownload(bool download)
{
_download = download;
}
QStringList ShootVersion::Images()
{
return _images;
}
void ShootVersion::SetImages(const QStringList& images)
{
_images = images;
}
void ShootVersion::AddImage(const QString& filename)
{
_images.append(filename);
}
ShootVersion& ShootVersion::operator=(const ShootVersion& other)
{
if (this != &other)
{
ShootVersion otherVersion( other );
_filename = otherVersion.Filename();
_download = otherVersion.Download();
_images = otherVersion.Images();
}
return *this;
}