sashoalm
sashoalm

Reputation: 79685

Qt naming convention for variables and getters so they don't collide?

In one of my classes I have a member "QSize frameSize". I wanted to make a getter function for it, and naturally I thought about "QSize frameSize()" but that would be the same as the variable name! In Qt the convention is not to use getFrameSize, so how do they avoid name collisions in their own source code?

Upvotes: 1

Views: 3167

Answers (3)

Samuel Harmer
Samuel Harmer

Reputation: 4412

If you are trying to extend Qt with your own classes, it's generally foo() and setFoo(), internally you could use m_foo or simply _foo. Take a look at the source if you want to be precise.

There are many ways of naming things and ultimately it comes down to preference (yours or your employer's). It's not too important so long as you're consistent and clear. This advice extends to Qt's dynamic properties too.

Upvotes: 0

Neox
Neox

Reputation: 2004

You might also want to take a look at KDE Coding Style.

Upvotes: 0

Frerich Raabe
Frerich Raabe

Reputation: 94539

Well - first of all: use the source, Luke! The Qt source code is available under open-source licenses, so you're free to check how they're doing things.

That being said, in the vast majority of cases Qt doesn't have this problem because most public classes only have a single member variable (a "d-pointer") since they implement the pimpl idiom. So they might have functions like

int QSomeThing::foo() const {
    return d->foo;
}

Last but not least, I'm not aware of any global naming rule for member variables in the Qt source code, but there are a few well-established conventions. The one I happen to use is to prefix the name of all member variables with m_ (denoting that it's a member). So the above function, in my source code, would (assuming I don't use the pimpl idiom) look like:

int QSomeThing::foo() const {
    return m_foo;
}

Your mileage may vary.

Upvotes: 9

Related Questions