[ fromfile: constmembers.xml id: constmembers ]
Example 2.21 shows how member functions might be seen by a linker after a translation into C.
Example 2.21. src/const/constmembers/constmembers.cpp
#include <QTextStream> #include <QString> class Point { public: Point(int px, int py) : m_X(px), m_Y(py) {} void set(int nx, int ny) { m_X = nx; m_Y = ny; } QString toString() const { // m_X = 5; m_Count++; return QString("[%1,%2]").arg(m_X).arg(m_Y); } private: int m_X, m_Y; mutable int m_Count; }; int main() { QTextStream cout(stdout); Point p(1,1); const Point q(2,2); p.set(4,4); cout << p.toString() << endl; //q.set(4,4); return 0; }
Suppose that you need to work with a project that contains classes that do not use const
correctly. When you start to add const
to member functions, parameters, and pointers that need it, you may find that those changes generare a cascade of compiler errors that prevent you from building the project until const
has been correctly added throughout the project. When const has finally been added to all the correct places, you can say that your classes are const correct.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |