Skip to content

Commit 913aba1

Browse files
committedSep 27, 2023
Значения по умолчанию для параметров виртуальных методов.
1 parent 777711c commit 913aba1

File tree

11 files changed

+307
-0
lines changed

11 files changed

+307
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
- [Виртуальный вариативный метод](virtual/virtual-variadic-method)
136136
- [Перегрузка метода с std::shared_ptr-параметром](overloading/call-is-ambiguous)
137137
- [Текучий интерфейс](method/fluent-interface)
138+
- [Значения по умолчанию для параметров виртуальных методов](virtual/virtual-method-default-parameter)
138139

139140

140141
# Константность

‎virtual/virtual-method-default-parameter/.gitignore

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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/JetBrains.gitignore
39+
40+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
41+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
42+
43+
# User-specific stuff
44+
.idea/**/workspace.xml
45+
.idea/**/tasks.xml
46+
.idea/**/usage.statistics.xml
47+
.idea/**/dictionaries
48+
.idea/**/shelf
49+
50+
# AWS User-specific
51+
.idea/**/aws.xml
52+
53+
# Generated files
54+
.idea/**/contentModel.xml
55+
56+
# Sensitive or high-churn files
57+
.idea/**/dataSources/
58+
.idea/**/dataSources.ids
59+
.idea/**/dataSources.local.xml
60+
.idea/**/sqlDataSources.xml
61+
.idea/**/dynamic.xml
62+
.idea/**/uiDesigner.xml
63+
.idea/**/dbnavigator.xml
64+
65+
# Gradle
66+
.idea/**/gradle.xml
67+
.idea/**/libraries
68+
69+
# Gradle and Maven with auto-import
70+
# When using Gradle or Maven with auto-import, you should exclude module files,
71+
# since they will be recreated, and may cause churn. Uncomment if using
72+
# auto-import.
73+
# .idea/artifacts
74+
# .idea/compiler.xml
75+
# .idea/jarRepositories.xml
76+
# .idea/modules.xml
77+
# .idea/*.iml
78+
# .idea/modules
79+
# *.iml
80+
# *.ipr
81+
82+
# CMake
83+
cmake-build-*/
84+
85+
# Mongo Explorer plugin
86+
.idea/**/mongoSettings.xml
87+
88+
# File-based project format
89+
*.iws
90+
91+
# IntelliJ
92+
out/
93+
94+
# mpeltonen/sbt-idea plugin
95+
.idea_modules/
96+
97+
# JIRA plugin
98+
atlassian-ide-plugin.xml
99+
100+
# Cursive Clojure plugin
101+
.idea/replstate.xml
102+
103+
# SonarLint plugin
104+
.idea/sonarlint/
105+
106+
# Crashlytics plugin (for Android Studio and IntelliJ)
107+
com_crashlytics_export_strings.xml
108+
crashlytics.properties
109+
crashlytics-build.properties
110+
fabric.properties
111+
112+
# Editor-based Rest Client
113+
.idea/httpRequests
114+
115+
# Android studio 3.1+ serialized cache file
116+
.idea/caches/build_file_checksums.ser
117+
118+
# ---------------------------------------------------------------------------
119+
# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
120+
121+
# General
122+
.DS_Store
123+
.AppleDouble
124+
.LSOverride
125+
126+
# Icon must end with two \r
127+
Icon
128+
129+
# Thumbnails
130+
._*
131+
132+
# Files that might appear in the root of a volume
133+
.DocumentRevisions-V100
134+
.fseventsd
135+
.Spotlight-V100
136+
.TemporaryItems
137+
.Trashes
138+
.VolumeIcon.icns
139+
.com.apple.timemachine.donotpresent
140+
141+
# Directories potentially created on remote AFP share
142+
.AppleDB
143+
.AppleDesktop
144+
Network Trash Folder
145+
Temporary Items
146+
.apdisk
147+
148+
# ---------------------------------------------------------------------------
149+
# https://github.com/github/gitignore/blob/master/CMake.gitignore
150+
151+
CMakeLists.txt.user
152+
CMakeCache.txt
153+
CMakeFiles
154+
CMakeScripts
155+
Testing
156+
Makefile
157+
cmake_install.cmake
158+
install_manifest.txt
159+
compile_commands.json
160+
CTestTestfile.cmake
161+
_deps
162+
163+
# ---------------------------------------------------------------------------

‎virtual/virtual-method-default-parameter/.idea/.gitignore

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎virtual/virtual-method-default-parameter/.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎virtual/virtual-method-default-parameter/.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎virtual/virtual-method-default-parameter/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎virtual/virtual-method-default-parameter/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎virtual/virtual-method-default-parameter/.idea/virtual-method-default-parameter.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.26)
2+
project(VirtualMethodDefaultParameter)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(${PROJECT_NAME} main.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- [GotW #5: Overriding Virtual Functions](http://www.gotw.ca/gotw/005.htm)
2+
- GeeksforGeeks / [Default Arguments and Virtual Function in C++](https://www.geeksforgeeks.org/default-arguments-and-virtual-function-in-cpp/)
3+
- Code Yarns / [Do not use default arguments in C++ virtual functions](https://codeyarns.com/tech/2020-01-14-do-not-use-default-arguments-in-c-virtual-functions.html#gsc.tab=0)
4+
- [C++ static code analysis: Parameters in an overriding virtual function shall either use the same default arguments as the function they override, or else shall not specify any default arguments](https://rules.sonarsource.com/cpp/RSPEC-1006/)
5+
6+
# Stack Exchange
7+
8+
- [c++ - Can virtual functions have default parameters?](https://stackoverflow.com/questions/3533589/can-virtual-functions-have-default-parameters)
9+
- [object oriented - Is overriding a pure virtual function with default arguments good or bad? - Software Engineering Stack Exchange](https://softwareengineering.stackexchange.com/questions/355074/is-overriding-a-pure-virtual-function-with-default-arguments-good-or-bad)
10+
- [c++ - Default arguments on virtual methods](https://stackoverflow.com/questions/74954687/default-arguments-on-virtual-methods)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <iostream>
2+
3+
struct Interface
4+
{
5+
virtual void Foo(int param = -1) = 0;
6+
// Clang-Tidy: Default arguments on virtual or override methods are prohibited
7+
};
8+
9+
class Abstract
10+
: public Interface
11+
{
12+
public:
13+
void Foo(int param) override
14+
{
15+
std::cout << "Abstract = " << param << std::endl;
16+
}
17+
};
18+
19+
class Base
20+
: public Abstract
21+
{
22+
public:
23+
void Foo(int param = 1) override
24+
{
25+
std::cout << "Base = " << param << std::endl;
26+
}
27+
};
28+
29+
class Impl
30+
: public Base
31+
{
32+
public:
33+
void Foo(int param) override
34+
{
35+
std::cout << "Impl = " << param << std::endl;
36+
}
37+
};
38+
39+
class Concrete
40+
: public Impl
41+
{
42+
public:
43+
void Foo(int param = 3) override
44+
{
45+
std::cout << "Concrete = " << param << std::endl;
46+
}
47+
};
48+
49+
void test1()
50+
{
51+
std::cout << "Concrete" << std::endl;
52+
Concrete concrete;
53+
concrete.Foo();
54+
55+
std::cout << std::endl;
56+
57+
std::cout << "Impl" << std::endl;
58+
Impl& impl = concrete;
59+
impl.Foo(2);
60+
61+
std::cout << std::endl;
62+
63+
std::cout << "Base" << std::endl;
64+
Base& base = impl;
65+
base.Foo();
66+
67+
std::cout << std::endl;
68+
69+
std::cout << "Abstract" << std::endl;
70+
Abstract& abstract = base;
71+
abstract.Foo(0);
72+
73+
std::cout << std::endl;
74+
75+
std::cout << "Interface" << std::endl;
76+
Interface& interface = abstract;
77+
interface.Foo();
78+
}
79+
/*
80+
Concrete
81+
Concrete = 3
82+
83+
Impl
84+
Concrete = 2
85+
86+
Base
87+
Concrete = 1
88+
89+
Abstract
90+
Concrete = 0
91+
92+
Interface
93+
Concrete = -1
94+
*/
95+
96+
int main()
97+
{
98+
test1();
99+
return 0;
100+
}

0 commit comments

Comments
 (0)
Please sign in to comment.