CppModule 00

c++ ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜

https://github.com/microsoft/AirSim/blob/master/docs/coding_guidelines.md

ํ—๊ฐ€๋ฆฌ์•ˆ ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๊ตฌ์‹์ด๊ณ  ๋‹จ์ ์ด ๋งŽ์•„์„œ ๊ฑฐ์˜ ์•ˆ์“ด๋‹ค๊ณ  ํ•œ๋‹ค. ms์˜ airsim์—์„œ ์“ฐ๊ณ ์žˆ๋Š” Modern C++ Coding Guideline์„ ์ฐธ๊ณ ํ•œ๋‹ค.

๋„ค์ด๋ฐ ๊ทœ์น™

Avoid using any sort of Hungarian notation on names and โ€œ_ptrโ€ on pointers.

Code Element Style Comment
Namespace under_scored Differentiate from class names
Class name CamelCase To differentiate from STL types which ISO recommends (do not use โ€œCโ€ or โ€œTโ€ prefixes)
Function name camelCase Lower case start is almost universal except for .Net world
Parameters/Locals under_scored Vast majority of standards recommends this because _ is more readable to C++ crowd (although not much to Java/.Net crowd)
Member variables under_scored_with_ The prefix _ is heavily discouraged as ISO has rules around reserving _identifiers, so we recommend suffix instead
Enums and its members CamelCase Most except very old standards agree with this one
Globals g_under_scored You shouldnโ€™t have these in first place!
Constants UPPER_CASE Very contentious and we just have to pick one here, unless if is a private constant in class or method, then use naming for Members or Locals
File names Match case of class name in file Lot of pro and cons either way but this removes inconsistency in auto generated code (important for ROS)

0.๊ณผ์ œ ์กฐ๊ฑด ์ •๋ฆฌ

EX00 : Megaphone

c++์Šคํƒ€์ผ๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ. ๋ฌธ์ž์—ด ์‚ฌ์ด๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜๊ณ , ๋ฌธ์ž์—ด ์•ž๋’ค์˜ ํ™”์ดํŠธ์ŠคํŽ˜์ด์Šค๋Š” trim๋œ๋‹ค.

EX01 : My Awesome

์ „ํ™”๋ฒˆํ˜ธ๋ถ€์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ์–ด๋– ํ•œ ๋™์ ํ• ๋‹น๋„ ํ•˜์ง€์•Š์œผ๋ฉฐ, 8๊ฐœ ์ด์ƒ์˜ ์ฝ˜ํ…์ธ ๊ฐ€ ์ž…๋ ฅ๋˜๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ์˜ค๋žœ๋œ๊ฒƒ์— ๋ฎ์–ด์”Œ์šด๋‹ค.

contact์˜ ํ•„๋“œ : first name, last name, nickname, phone number(array),darkest secret.

PhoneBook์€ ์ฝ”๋“œ์—์„œ ์ธ์Šคํ„ด์Šคํ˜•ํƒœ๋กœ ์ •์˜๋œ๋‹ค.

์—ฐ๋ฝ์ฒ˜๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ์ธ์Šคํ„ด์Šค ํ˜•ํƒœ๋กœ ์ •์˜๋œ๋‹ค.

์„ค๊ณ„ ๋ฐฉ์‹ : Phone book๊ณผ ๊ทธ ๋‚ด๋ถ€์˜ 8๊ฐœ์˜ page๋กœ ๊ตฌ๋ถ„.

https://www.cplusplus.com/reference/

EX02 : The Job Of Your Dreams

hppํŒŒ์ผ์ด๋ž‘ test(mainํ•จ์ˆ˜) ๊ทธ๋ฆฌ๊ณ  log๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ, ์ปดํŒŒ์ผ์— ํ•„์š”ํ•œ Account์˜ ๋งด๋ฒ„ํ•จ์ˆ˜๋“ค์„ ์„ค๊ณ„ํ•˜๋Š” ๊ณผ์ œ.

hppํŒŒ์ผ๋“ค์„ ๋ณด๋ฉด staticํ•จ์ˆ˜๋“ค๊ณผ ๋ณ€์ˆ˜๋“ค๊ณ  ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

์ •์  ๋ฉค๋ฒ„(๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜)

static์ •์  ๋งด๋ฒ„ ๋ณ€์ˆ˜ : .

์„ ์–ธ๊ณผ ์ •์˜(feat. ์ •์  ๋ฉค๋ฒ„)

์ •์  ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์˜ ๊ฒฝ์šฐ ํด๋ž˜์Šค ๋ฐ–(์ „์—ญ??)์—์„œ ์ด๋ฅผ ์ •์˜(์ดˆ๊ธฐํ™”)ํ•ด์•ผํ•œ๋‹ค. ์ƒ์„ฑ์ž์—์„œ ์—ฐ์‚ฐ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ •์˜ ๋ฐ ์ดˆ๊ธฐํ™”๋Š” ๋ถˆ๊ฐ€ํ•˜๋‹ค. -> main๋‚ด๋ถ€์—์„œ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š”๋ฐ? ๋ฒ„์ „๋งˆ๋‹ค ๋‹ค๋ฅด๋‚˜???

์ฐธ๊ณ  : https://ansohxxn.github.io/cpp/chapter8-10/, https://woo-dev.tistory.com/67

test.cpp ํ•ด์„.

test.cpp์—์„œ accounts_t๋Š” typedef std::vector<Account::t> = typedef std::vector<Account>์™€ ๊ฐ™๋‹ค.

mem_fun_ref ํ…œํ”Œ๋ฆฟ:

Function for_each(InputIterator first, InputIterator last, Function fn);

for_each์‚ฌ์šฉ์‹œ, ๊ฐ ์ˆœํ™˜์ž๋ฅผ first๋ถ€ํ„ฐ last๊นŒ์ง€ ๋Œ€์ž…ํ•˜๋ฉด์„œ fn์„ ์‹คํ–‰ํ• ์ˆ˜ ์žˆ๋‹ค. fn์˜ ๊ฒฝ์šฐ ํ•จ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜๋Š”๋ฐ,

  1. fn์ด ์ผ๋ฐ˜ํ•จ์ˆ˜์ธ ๊ฒฝ์šฐ fn๊ทธ๋Œ€๋กœ ์ „๋‹ฌ
  2. fn์ด Account ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์˜ ํฌ์ธํ„ฐ์˜ ๋งด๋ฒ„(โ€ฆ;)ํ•จ์ˆ˜์ธ ๊ฒฝ์šฐ mem_fun(&account::fn)ํ˜•ํƒœ๋กœ ์ „๋‹ฌ
  3. fn์ด Account ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์ด๊ฑฐ๋‚˜ ์ธ์Šคํ„ด์Šค์˜ ์ฐธ์กฐ์˜ ๋งด๋ฒ„ํ•จ์ˆ˜์ธ ๊ฒฝ์šฐ mem_fun_ref(account::fn)ํ˜•ํƒœ๋กœ ์ „๋‹ฌ

์ฐธ๊ณ  : (ifyouwanna ํ‹ฐ์Šคํ† ๋ฆฌ)[https://ifyouwanna.tistory.com/entry/memfun-%EC%99%80-memfunref-%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98%EC%97%90-%EB%A9%A4%EB%B2%84%ED%95%A8%EC%88%98-%EC%A0%84%EB%8B%AC]

์ปดํŒŒ์ผ์‹œ ์—๋Ÿฌ

 Undefined symbols for architecture x86_64:
  "Account::makeDeposit(int)", referenced from:
      _main in tests-6deae4.o
  "Account::makeWithdrawal(int)", referenced from:
      _main in tests-6deae4.o
  "Account::displayAccountsInfos()", referenced from:
      _main in tests-6deae4.o
  "Account::Account(int)", referenced from:
      void std::__1::allocator<Account>::construct<int>(Account*, int const&) in tests-6deae4.o
  "Account::~Account()", referenced from:
      std::__1::allocator<Account>::destroy(Account*) in tests-6deae4.o
  "Account::displayStatus() const", referenced from:
      _main in tests-6deae4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

log ๋ถ„์„

>> ์ƒ์„ฑ์ž
[19920104_091532] index:0;amount:42;created
[19920104_091532] index:1;amount:54;created
[19920104_091532] index:2;amount:957;created
[19920104_091532] index:3;amount:432;created
[19920104_091532] index:4;amount:1234;created
[19920104_091532] index:5;amount:0;created
[19920104_091532] index:6;amount:754;created
[19920104_091532] index:7;amount:16576;created
>> Account::displayAccountsInfos();
[19920104_091532] accounts:8;total:20049;deposits:0;withdrawals:0
>> std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) );
[19920104_091532] index:0;amount:42;deposits:0;withdrawals:0
[19920104_091532] index:1;amount:54;deposits:0;withdrawals:0
[19920104_091532] index:2;amount:957;deposits:0;withdrawals:0
[19920104_091532] index:3;amount:432;deposits:0;withdrawals:0
[19920104_091532] index:4;amount:1234;deposits:0;withdrawals:0
[19920104_091532] index:5;amount:0;deposits:0;withdrawals:0
[19920104_091532] index:6;amount:754;deposits:0;withdrawals:0
[19920104_091532] index:7;amount:16576;deposits:0;withdrawals:0
>>	for ( acc_int_t it( acc_begin, dep_begin );
		  it.first != acc_end && it.second != dep_end;
		  ++(it.first), ++(it.second) ) {

		(*(it.first)).makeDeposit( *(it.second) );
	}
[19920104_091532] index:0;p_amount:42;deposit:5;amount:47;nb_deposits:1
[19920104_091532] index:1;p_amount:54;deposit:765;amount:819;nb_deposits:1
[19920104_091532] index:2;p_amount:957;deposit:564;amount:1521;nb_deposits:1
[19920104_091532] index:3;p_amount:432;deposit:2;amount:434;nb_deposits:1
[19920104_091532] index:4;p_amount:1234;deposit:87;amount:1321;nb_deposits:1
[19920104_091532] index:5;p_amount:0;deposit:23;amount:23;nb_deposits:1
[19920104_091532] index:6;p_amount:754;deposit:9;amount:763;nb_deposits:1
[19920104_091532] index:7;p_amount:16576;deposit:20;amount:16596;nb_deposits:1
>> Account::displayAccountsInfos();
[19920104_091532] accounts:8;total:21524;deposits:8;withdrawals:0
>> std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) );
[19920104_091532] index:0;amount:47;deposits:1;withdrawals:0
[19920104_091532] index:1;amount:819;deposits:1;withdrawals:0
[19920104_091532] index:2;amount:1521;deposits:1;withdrawals:0
[19920104_091532] index:3;amount:434;deposits:1;withdrawals:0
[19920104_091532] index:4;amount:1321;deposits:1;withdrawals:0
[19920104_091532] index:5;amount:23;deposits:1;withdrawals:0
[19920104_091532] index:6;amount:763;deposits:1;withdrawals:0
[19920104_091532] index:7;amount:16596;deposits:1;withdrawals:0
>>	for ( acc_int_t it( acc_begin, wit_begin );
		  it.first != acc_end && it.second != wit_end;
		  ++(it.first), ++(it.second) ) {

		(*(it.first)).makeWithdrawal( *(it.second) );
	}
[19920104_091532] index:0;p_amount:47;withdrawal:refused
[19920104_091532] index:1;p_amount:819;withdrawal:34;amount:785;nb_withdrawals:1
[19920104_091532] index:2;p_amount:1521;withdrawal:657;amount:864;nb_withdrawals:1
[19920104_091532] index:3;p_amount:434;withdrawal:4;amount:430;nb_withdrawals:1
[19920104_091532] index:4;p_amount:1321;withdrawal:76;amount:1245;nb_withdrawals:1
[19920104_091532] index:5;p_amount:23;withdrawal:refused
[19920104_091532] index:6;p_amount:763;withdrawal:657;amount:106;nb_withdrawals:1
[19920104_091532] index:7;p_amount:16596;withdrawal:7654;amount:8942;nb_withdrawals:1
>>	Account::displayAccountsInfos();
[19920104_091532] accounts:8;total:12442;deposits:8;withdrawals:6
std::for_each( acc_begin, acc_end, std::mem_fun_ref( &Account::displayStatus ) );
[19920104_091532] index:0;amount:47;deposits:1;withdrawals:0
[19920104_091532] index:1;amount:785;deposits:1;withdrawals:1
[19920104_091532] index:2;amount:864;deposits:1;withdrawals:1
[19920104_091532] index:3;amount:430;deposits:1;withdrawals:1
[19920104_091532] index:4;amount:1245;deposits:1;withdrawals:1
[19920104_091532] index:5;amount:23;deposits:1;withdrawals:0
[19920104_091532] index:6;amount:106;deposits:1;withdrawals:1
[19920104_091532] index:7;amount:8942;deposits:1;withdrawals:1
>> ์†Œ๋ฉธ์ž
[19920104_091532] index:0;amount:47;closed
[19920104_091532] index:1;amount:785;closed
[19920104_091532] index:2;amount:864;closed
[19920104_091532] index:3;amount:430;closed
[19920104_091532] index:4;amount:1245;closed
[19920104_091532] index:5;amount:23;closed
[19920104_091532] index:6;amount:106;closed
[19920104_091532] index:7;amount:8942;closed

Account::Account(int) = Account( int initial_deposit );

accounts_t accounts( amounts, amounts + amounts_size );

test.cpp 24 accounts_t accounts( amounts, amounts + amounts_size ); ์ƒ์„ฑ์ž ํ˜ธ์ถœ์‹œ [19920104_091532] index:0;amount:42;created ์ถœ๋ ฅ.

Account::displayAccountsInfos() = static void displayAccountsInfos( void );

Account::displayAccountsInfos();

[19920104_091532] accounts:8;total:20049;deposits:0;withdrawals:0

Account::makeDeposit(int) = void makeDeposit( int deposit );

for ( acc_int_t it( acc_begin, dep_begin );
		  it.first != acc_end && it.second != dep_end;
		  ++(it.first), ++(it.second) ) {

		(*(it.first)).makeDeposit( *(it.second) );
	}

[19920104_091532] index:0;p_amount:42;deposit:5;amount:47;nb_deposits:1

Account::makeWithdrawal(int) = bool makeWithdrawal( int withdrawal );

for ( acc_int_t it( acc_begin, wit_begin );
		  it.first != acc_end && it.second != wit_end;
		  ++(it.first), ++(it.second) ) {

		(*(it.first)).makeWithdrawal( *(it.second) );
	}

[19920104_091532] index:0;p_amount:47;withdrawal:refused

[19920104_091532] index:1;p_amount:819;withdrawal:34;amount:785;nb_withdrawals:1

Account::~Account() = ~Account( void );

return 0;

Account::displayStatus() const = void displayStatus( void ) const; [19920104_091532] index:0;amount:47;closed

๊ตฌํ˜„ํ•˜๋ฉด์„œ ์ถ”๊ฐ€.

  1. ๊ตฌํ˜„ํŒŒ์ผ(.cpp)์—์„œ ์ •์  ๋ฉค๋ฒ„๋ฅผ ์ •์˜ํ• ๋•Œ staticํ‚ค์›Œ๋“œ๋ฅผ ์“ฐ์ง€ ์•Š๋Š”๋‹ค(link error : a storage class may not be specified here). ->
  2. ์ •์  ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ํด๋ž˜์Šค ๊ตฌํ˜„ํŒŒ์ผ(.cpp)์—์„œ ์ •์˜ํ• ๋•Œ int Accout::_nbAccounts = 0;ํ˜•ํƒœ๋กœ ์ „์—ญ์œผ๋กœ ์ •์˜ํ•œ๋‹ค.
  3. std::cout.width(n), std::cout.fill('?')์˜ ๊ฒฝ์šฐ, std::cout์— <<๋กœ ๋‚˜๋‰˜์–ด์„œ ๋“ค์–ด์˜จ ๋‹จ์œ„๋ณ„๋กœ ์ ์šฉ๋จ.
  4. const์œ„์น˜ : https://easycoding91.tistory.com/6

1์ฐจํ‰๊ฐ€ ํ”ผ๋“œ๋ฐฑ :