-
[ Anything ] C++ - 1Graphics 2021. 6. 21. 21:44
Prefer const, enum, inline to define
このルールはpreprocessorよりcompilerを使ったほうがよいってことです。理由は簡単です。defineで宣言された場合はC++の文法として扱わなくなります。Preprocessorは#Defineを使って宣言されたVariableをCompilerから隠します。それでそのVariableはSymbolTableへ入ることができなくなり、Compiling Timeで発生したエラーが#Defineと関係があった場合はDebuggingするときSymbolTableを参照することができなくなってDebuggingが難しくなってしまいます。特に別の Header FileにあるMacroの場合はエラーが発生したときに一体どこからエラーが発生したのか判断が難しくなります。
打開策はMacroを使うよりConstant表現を用いることです。

こうしてConstで宣言したVariableはSymbolTableへ入ります。
Floating-Point Constant
Signed Real Numberを表す10進数。
ㇾパランス:https://docs.microsoft.com/en-us/cpp/c-language/c-floating-point-constants?view=msvc-160
char* / const char* / char* const / const char* const
char* is a mutable pointer to a mutable character or string;
const char* is a mutable pointer to an immutable character or string;
char* const is an immutable pointer to a mutable character or string;
cosnt char* const is an immutable pointer to an immutable character or string;
Main reason we cannot convert const char* to char* is that since we cannot change contents of const char* pointer, therefore converting const char* to char* which is able to chage its contents of pointer violates immutable character or string rule.
Why? char* and const char* are just pointers. char* pointers can adjust value of its pointers, therefore statements followed will authorise char* pointers to change contents of const char* pointers.(and these kind of actions are not allowed.)
converting char* to const char* is available since these kind of actions are just chaging pointers to another locations.
const char* name1 = "AA";
char* name2 = "BB";
name2 = name1; // Error!
name1 = name2; // available!
However、generally using std::string is preferable to char* or const char*. 理由はまだ不明だがこれからどこかで説明します。
ref https://stackoverflow.com/questions/9834067/difference-between-char-and-const-char
Difference between char* and const char*?
What's the difference between char* name which points to a constant string literal, and const char* name
stackoverflow.com
Class specific constant
In case we need to use constant data into the class

a nonstatic member reference must be relative to a specific objectC/C++(245
Using static const int can solve this problem.

0. Any type of objects which are declared constant will never be changed until entire process over. Therefore even if new object of a class is created, constant object will not be created or be copied.
1. Those are declaration of a class not a definition.
2. Briefly, to avoid complicated linker rules it is not allowed to 'define' object inside of class. Refer to the link below. const member can be 'declared inside of a class but cannot be defined inside of a class.
3. If i want to declare static integer or any type of objects, i can declare a object within class but cannot define a object. I need to define 'outside' of its class scope.
* When compiler insists to know exact size of num i can also use enumeration below.
ref https://stackoverflow.com/questions/14495536/how-to-initialize-const-member-variable-in-a-class
How to initialize const member variable in a class?
#include using namespace std; class T1 { const int t = 100; public: T1() { cout << "T1 constructor: " << t << endl; } }; When I am trying to
stackoverflow.com

Simple test code below.

g++ compiler.
'Graphics' 카테고리의 다른 글
[5] - Window Framework (0) 2021.07.10 [4] - Mouse and Char (0) 2021.07.03 [3] - Window Messages (0) 2021.07.03 [2] - Message Pump (0) 2021.07.03 [1] - Creating a window (0) 2021.07.02