Hi.

An enum has the advantage that you have a certain level of type safety and in 
my opinion it makes the API much more readable.

foo(int x)

is far less certain on what to pass then

foo(ErrorCode x)

I immediately see what the range of possible values is (based on the enum 
definition) while the int could be any arbitrary int constant you came along. 
QtCreator and its predictive input/tab completion will thank you for it.


--

Alex

________________________________
From: [email protected] 
[[email protected]] on behalf of 
Mandeep Sandhu [[email protected]]
Sent: Friday, February 14, 2014 11:29
To: [email protected]
Subject: [Development] Best practice for defining an integer constant

I have a need for defining an integer constant that'll be used for initializing 
a member variable of a private class.

The Qt coding conventions (http://qt-project.org/wiki/Coding-Conventions) 
recommend using an enum over 'const int'.

The rationale given there is that an enum will be replaced at compile-time 
resulting in 'faster code'. Won't that be the case with 'const int' as well? I 
think a 'const int' will be inlined in the code. CMIIW.

Also, there are a couple of instances in our current code where such (static 
and non-static) constants are used. Whats the recommended way?

Thanks,
-mandeep

_______________________________________________
Development mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to