Dear all,
Here is the continuation of my post on compiler warnings, this time regarding adding default ctors and initializing all of the members in the initializer list. In general that’s a good thing, everybody would agree! My problem is with adding default ctors to private Data classes (e.g. Protocols::Generics::Packet::Data). There were some previous discussions on the topic, which you could check:
http://www.calitko.org/source-talk/90
http://www.calitko.org/source-talk/92
http://www.calitko.org/source-talk/357
Let’s look at an example:
(warning)
Protocols/Gnutella/Packets/QueryHits.h: In constructor ‘Protocols::Gnutella::Packets::QueryHits::Data::Data()’:
Protocols/Gnutella/Packets/QueryHits.h:76: note: synthesized method ‘Protocols::Gnutella::Packets::PacketBase::Data::Data()’ first required here
Protocols/Gnutella/Packets/QueryHits.h:76: warning: ‘Protocols::Gnutella::Packets::QueryHits::Data::port’ should be initialized in the member initialization list
Protocols/Gnutella/Packets/QueryHits.h:76: warning: ‘Protocols::Gnutella::Packets::QueryHits::Data::ipAddress’ should be initialized in the member initialization list
...
(before)
QueryHits (quint16 port = 0,
const QHostAddress &ipAddress = QHostAddress(),
quint32 speed = 0,
const ResultSet &resultSet = ResultSet(),
const QueryHitsData &queryHitsData = QueryHitsData());
...
class Data: public PacketBase::Data
{
Q_SHARED_DATA_COPY (Data);
public:
quint16 port;
QHostAddress ipAddress;
quint32 speed;
ResultSet resultSet;
QueryHitsData queryHitsData;
};
...
QueryHits::QueryHits (
quint16 port,
const QHostAddress &ipAddress,
quint32 speed,
const ResultSet &resultSet,
const QueryHitsData &queryHitsData)
: PacketBase (new Data, QueryHitsPacket)
{
Q_SD (Data);
d->port = port;
d->ipAddress = ipAddress;
d->speed = speed;
d->resultSet = resultSet;
d->queryHitsData = queryHitsData;
}
after
QueryHits (quint16 port = 0,
const QHostAddress &ipAddress = QHostAddress(),
quint32 speed = 0,
const ResultSet &resultSet = ResultSet(),
const QueryHitsData &queryHitsData = QueryHitsData());
...
class Data: public PacketBase::Data
{
Q_SHARED_DATA_COPY (Data);
public:
QueryHits (quint16 port,
const QHostAddress &ipAddress,
quint32 speed,
const ResultSet &resultSet,
const QueryHitsData &queryHitsData);
quint16 port;
QHostAddress ipAddress;
quint32 speed;
ResultSet resultSet;
QueryHitsData queryHitsData;
};
...
QueryHits::QueryHits (
quint16 port,
const QHostAddress &ipAddress,
quint32 speed,
const ResultSet &resultSet,
const QueryHitsData &queryHitsData)
: PacketBase (new Data (port, ipAddress, speed, resultSet, queryHitsData), QueryHitsPacket)
{
}
QueryHits::Data::Data (
quint16 port_,
const QHostAddress &ipAddress_,
quint32 speed_,
const ResultSet &resultSet_,
const QueryHitsData &queryHitsData_)
: port (port_), ipAddress (ipAddress_), speed (speed), resultSet (resultSet), queryHitsData (queryHitsData)
{
}
I don’t think that the (after) example is of higher quality. It’s more code and what’s worse, it’s duplicated code in my opinion.
That is not to say I’ll never fix that kind of warnings! Quite on the contrary, I’ll fix most but I’d add warning regarding private Data classes on the ignore list.
What are other opinions? I have to say I have a good will to fix the warnings, my problem is just that I don’t always “believe” them to be right, so I’d be quite happy if someone convinces me to follow suite and not go against the coding standards C++ experts have set.
Thanks,
Peter
