Note that there are some explanatory texts on larger screens.

plurals
  1. PORule of thumb for C++ constructor overloads?
    primarykey
    data
    text
    <p>In C++, I often (almost <em>always</em>) run into problems with constructors; I'm never sure how to use the parameters, and eventually I'll end up using only parameterless constructor for each class. Then I'll use setters after each instance definition. For example:</p> <pre><code>// Obviously better Point p(5, 3); // ...and yet I end up using this Point p; p.setX(5); p.setY(3); </code></pre> <p>The reason I end up using the "worse" method is because some classes take so many parameters and there are so many different possibilities to costruct them, so my thoughts get messed up and I no longer know what I should do. Here's an example of a recent class I made (it uses Qt):</p> <pre><code>// implements position and pixmap (image shown when painted) class Block : QObject { public: Block(const QPixmap &amp;img = Pixmap(), QObject *parent = 0); Block(const QPoint &amp;pos, const QPixmap &amp;img = Pixmap(), QObject *parent = 0); Block(int x, int y, const QPixmap &amp;img = Pixmap(), QObject *parent = 0); Block(const Block &amp;other); }; </code></pre> <p>Having only one class with only two attributes (<code>position</code> and <code>image</code>), I already get four constructors taking four parameters at most. Also, I often see people replacing the first constructor with <code>Block(QObject *parent = 0);</code> and <code>Block(const QPixmap &amp;img, QObject *parent = 0);</code> and I'm not sure if this would be a good thing to do.</p> <p>Now then, what if I craete a <code>Rectangle</code> class of my own, which inherits from <code>Block</code> so it also needs to take <code>image</code> and <code>position</code> as it's parameters:</p> <pre><code>Rect(const QPixmap &amp;img = QPixmap(), QObject *parent = 0); Rect(const QRect &amp;rect, const QPixmap &amp;pixmap = QPixmap(), QObject *parent = 0); Rect(const QSize &amp;size, const QPoint &amp;pos = QPoint(), const QPixmap &amp;img = QPixmap(), QObject *parent = 0); Rect(const QPoint &amp;pos, const QPixmap &amp;img = QPixmap(), QObject *parent = 0); Rect(int x, int y, int w = 1, int h = 1, QPixmap &amp;img = QPixmap(), QObject *parent = 0); Rect(int w, int h, QPixmap &amp;img = QPixmap(), QObject *parent = 0); Rect(const QPoint &amp;pos, int w, int h, QPixmap &amp;img = QPixmap(), QObject *parent = 0); Rect(int x, int y, const QSize &amp;size, QPixmap &amp;img = QPixmap(), QObject *parent = 0); Rect(const Rect &amp;rect); </code></pre> <p>As you see, it's starting to look terrible and damn hard to maintain. Just imagine <code>Player</code> class inheriting from <code>Rect</code> and implementing <code>items</code>, <code>health</code>, <code>mana</code>, <code>damage</code>, <code>armor</code>... I find it impossible to code such constructors.</p> <p>So now that I've given you the problem, I'm not sure of the question; I need any tips or tricks to help me with my constructos, when to use default parameters and how often do I need all the constructors and yeah, just some tips to prevent me from creating 500 constructors for one class; should I just go with my original <code>p.setX(5);</code> example?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload