4

If my header looks like this:

class foo {

public:
    foo();
    ~foo();

    QString b();
    QString c();
    QString a();

    void setB(QString s);
    void setC(QString s);
    void setA(QString s);

private:
    QString m_B;
    QString m_C;
    QString m_A;
};

Should my source neccessarily correspond like this:

foo::foo() {...}
foo::~foo() {...}

QString foo::b() { return m_B; }
QString foo::c() { return m_C; }
QString foo::a() { return m_A; }

void foo::setB(QString s) { m_B = s; }
void foo::setC(QString s) { m_C = s; }
void foo::setA(QString s) { m_A = s; }

or something like this:

foo::foo() {...}
foo::~foo() {...}

QString foo::b() { return m_B; }
void foo::setB(QString s) { m_B = s; }

QString foo::c() { return m_C; }
void foo::setC(QString s) { m_C = s; }

QString foo::a() { return m_A; }
void foo::setA(QString s) { m_A = s; }

or something else entirely? Or perhaps I should be reorganizing my header differently?

Thanks!

Anon
  • 3,565
  • 3
  • 27
  • 45
  • 2
    possible duplicate of [How would you know if you've written readable and easily maintainable code?](http://programmers.stackexchange.com/questions/141005/how-would-you-know-if-youve-written-readable-and-easily-maintainable-code) – gnat Jul 16 '15 at 19:08
  • 3
    I put them in the same order out of OCD, but it does not matter because the declarations are in the header. Order of definitions is irrelevant. In other words: primary opinion-based. –  Jul 16 '15 at 19:29
  • 2
    Recommended reading: **[Gorilla vs. Shark](https://blog.stackexchange.com/2011/08/gorilla-vs-shark/)**. –  Jul 16 '15 at 19:58
  • 1
    I personally find it surprising when the order of declarations do not match the order of definitions. I was going to see if someone asked such a question, sorry to see its closed. I know C++ doesn't require the same ordering in the implementation file but as a matter of style and minimal surprise I think maintaining order of declaration and definition help especially when learning a new class and following it down the line with a header open in one monitor and the implementation in the other monitor. – jxramos Oct 13 '16 at 20:49
  • 1
    @jxramos Maybe there is a way we can reword this question to be reopened. – Anon Oct 13 '16 at 20:51
  • 1
    I came here because I was wondering if there is **any technical difference** when shuffling definitions over declarations. That's not an opinion-based question. For instance, is the resulting object file 100% byte identical? Only if that is guaranteed it becomes a matter of opinion. – bluenote10 Apr 30 '19 at 08:47

0 Answers0