Skip to content

Commit fcfd046

Browse files
committed
Передача перечисления в функцию.
1 parent 328d320 commit fcfd046

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
## Перечисления
3434

3535
- [Scoped и un-scoped перечисления с указанием нижележащего интегрального (целочисленного) типа](syncfusion/cpp_succinctly/EnumSample)
36+
- [Передача перечисления в функцию](enum/pass-enum-to-function)
3637

3738
## Объединения
3839

Diff for: enum/pass-enum-to-function/.gitignore

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# ---------------------------------------------------------------------------
2+
# https://github.com/github/gitignore/blob/master/C++.gitignore
3+
4+
# Prerequisites
5+
*.d
6+
7+
# Compiled Object files
8+
*.slo
9+
*.lo
10+
*.o
11+
*.obj
12+
13+
# Precompiled Headers
14+
*.gch
15+
*.pch
16+
17+
# Compiled Dynamic libraries
18+
*.so
19+
*.dylib
20+
*.dll
21+
22+
# Fortran module files
23+
*.mod
24+
*.smod
25+
26+
# Compiled Static libraries
27+
*.lai
28+
*.la
29+
*.a
30+
*.lib
31+
32+
# Executables
33+
*.exe
34+
*.out
35+
*.app
36+
37+
# ---------------------------------------------------------------------------
38+
# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
39+
40+
# General
41+
.DS_Store
42+
.AppleDouble
43+
.LSOverride
44+
45+
# Icon must end with two \r
46+
Icon
47+
48+
# Thumbnails
49+
._*
50+
51+
# Files that might appear in the root of a volume
52+
.DocumentRevisions-V100
53+
.fseventsd
54+
.Spotlight-V100
55+
.TemporaryItems
56+
.Trashes
57+
.VolumeIcon.icns
58+
.com.apple.timemachine.donotpresent
59+
60+
# Directories potentially created on remote AFP share
61+
.AppleDB
62+
.AppleDesktop
63+
Network Trash Folder
64+
Temporary Items
65+
.apdisk
66+
67+
# ---------------------------------------------------------------------------

Diff for: enum/pass-enum-to-function/main.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//---------------------------------------------------------------------------
2+
// Демонстрация передачи перечисления в функцию.
3+
//---------------------------------------------------------------------------
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
// Scoped enumeration:
10+
enum class Color { Red, Orange, Yellow, Blue, Indigo, Violet };
11+
12+
// Unscoped enumeration:
13+
enum Flavor : unsigned short int { Vanilla, Chocolate, Strawberry, Mint };
14+
15+
void print(int usi) {
16+
cout << "void print(int usi)" << endl;
17+
switch (usi) {
18+
case Vanilla: cout << "Vanilla" << endl; break;
19+
case Chocolate: cout << "Chocolate" << endl; break;
20+
case Strawberry: cout << "Strawberry" << endl; break;
21+
case Mint: cout << "Mint" << endl; break;
22+
}
23+
}
24+
25+
/*
26+
// При определении этой функции она возьмет на себя вызов функции:
27+
// Flavor f = Mint;
28+
// print(f);
29+
void print(Flavor& f) {
30+
cout << "void print(Flavor& f)" << endl;
31+
switch (f) {
32+
case Vanilla: cout << "Vanilla" << endl; break;
33+
case Chocolate: cout << "Chocolate" << endl; break;
34+
case Strawberry: cout << "Strawberry" << endl; break;
35+
case Mint: cout << "Mint" << endl; break;
36+
}
37+
}
38+
*/
39+
40+
int main(int argc, char** argv) {
41+
Flavor f = Mint;
42+
print(f);
43+
// void print(int usi)
44+
// Mint
45+
46+
return 0;
47+
}

Diff for: enum/pass-enum-to-function/run.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
EXE_FILE="./program.out"
4+
5+
[ -f "$EXE_FILE" ] && rm "$EXE_FILE"
6+
7+
clang++ --std=c++11 \
8+
`find . -name "*.cpp" | tr '\n' ' '` -o "$EXE_FILE" && "$EXE_FILE"

0 commit comments

Comments
 (0)