fonzi337
23rd March 2012, 01:40
Hi,
I am trying to implement the D-pointer pattern for some of my classes. I've followed the tutorials http://zchydem.enume.net/2010/01/19/qt-howto-private-classes-and-d-pointers/ and http://techbase.kde.org/Policies/Library_Code_Policy/Shared_D-Pointer_Example but whenever I access a private member of my d-pointer, a compiler error results stating that the member is private in the given context. Q_DECLARE_PRIVATE is supposed to make my XYZPrivate class a friend of my XYZ class, so why is there a problem accessing its private data members?
Here's the relevant code:
Employer.h:
class EmployerPrivate;
class Employer : public Loggable
{
public:
Employer(QString name, QString market);
~Employer();
QList<Position> findJobs() const;
QString toString() const;
bool hire(Person& newHire, Position pos);
protected:
const QScopedPointer<EmployerPrivate> d_ptr;
private:
Q_DECLARE_PRIVATE(Employer)
};
Employer.cpp:
Employer::Employer(QString name, QString market) :
d_ptr(new EmployerPrivate(name, market))
{
}
Employer::~Employer()
{
}
QList<Position> Employer::findJobs() const
{
Q_D(const Employer);
return d->m_positions; // Compiler error: m_positions private within context
}
QString Employer::toString() const
{
Q_D(const Employer);
return QString("***Employer information***\nName: %1\nMarket: %2").arg(d->m_name).arg(d->m_market); // Compiler error
}
EmployerPrivate.h:
class EmployerPrivate
{
public:
EmployerPrivate(QString name, QString market);
private:
QList<Position> m_positions;
QString m_name;
QString m_market;
};
EmployerPrivate.cpp:
EmployerPrivate::EmployerPrivate(QString name, QString market) :
m_name(name),
m_market(market)
{
}
I've tried making both classes derive from QObject and added the Q_OBJECT macro just in case (even though, from my understanding, these aren't needed), but that also did not work.
I am trying to implement the D-pointer pattern for some of my classes. I've followed the tutorials http://zchydem.enume.net/2010/01/19/qt-howto-private-classes-and-d-pointers/ and http://techbase.kde.org/Policies/Library_Code_Policy/Shared_D-Pointer_Example but whenever I access a private member of my d-pointer, a compiler error results stating that the member is private in the given context. Q_DECLARE_PRIVATE is supposed to make my XYZPrivate class a friend of my XYZ class, so why is there a problem accessing its private data members?
Here's the relevant code:
Employer.h:
class EmployerPrivate;
class Employer : public Loggable
{
public:
Employer(QString name, QString market);
~Employer();
QList<Position> findJobs() const;
QString toString() const;
bool hire(Person& newHire, Position pos);
protected:
const QScopedPointer<EmployerPrivate> d_ptr;
private:
Q_DECLARE_PRIVATE(Employer)
};
Employer.cpp:
Employer::Employer(QString name, QString market) :
d_ptr(new EmployerPrivate(name, market))
{
}
Employer::~Employer()
{
}
QList<Position> Employer::findJobs() const
{
Q_D(const Employer);
return d->m_positions; // Compiler error: m_positions private within context
}
QString Employer::toString() const
{
Q_D(const Employer);
return QString("***Employer information***\nName: %1\nMarket: %2").arg(d->m_name).arg(d->m_market); // Compiler error
}
EmployerPrivate.h:
class EmployerPrivate
{
public:
EmployerPrivate(QString name, QString market);
private:
QList<Position> m_positions;
QString m_name;
QString m_market;
};
EmployerPrivate.cpp:
EmployerPrivate::EmployerPrivate(QString name, QString market) :
m_name(name),
m_market(market)
{
}
I've tried making both classes derive from QObject and added the Q_OBJECT macro just in case (even though, from my understanding, these aren't needed), but that also did not work.