PDA

View Full Version : const function parameter problems



stevey
18th December 2006, 13: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;
}

sunil.thaha
18th December 2006, 13:47
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

wysota
18th December 2006, 17:12
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):


QStringList ShootVersion::Images() const {
return _images;
}

stevey
18th December 2006, 22:22
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