Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
Java2Cpp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tobias Glaser
Java2Cpp
Commits
62e2ec9c
Commit
62e2ec9c
authored
3 years ago
by
tobiglaser
Browse files
Options
Downloads
Patches
Plain Diff
adding static vs member demo
parent
edda04bc
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CMakeLists.txt
+5
-0
5 additions, 0 deletions
CMakeLists.txt
src/class-member-variables.cpp
+34
-0
34 additions, 0 deletions
src/class-member-variables.cpp
with
39 additions
and
0 deletions
CMakeLists.txt
+
5
−
0
View file @
62e2ec9c
...
...
@@ -16,6 +16,11 @@ src/testCommandList.cpp
target_include_directories
(
"Info3_Praktikum-test"
PUBLIC include/
)
add_executable
(
"Class-Member-Variable-demo"
src/class-member-variables.cpp
)
# Doxygen documentation
find_package
(
Doxygen
)
if
(
DOXYGEN_FOUND
)
...
...
This diff is collapsed.
Click to expand it.
src/class-member-variables.cpp
0 → 100644
+
34
−
0
View file @
62e2ec9c
#include
<iostream>
using
std
::
cout
;
using
std
::
string
;
class
ControlDeveloper
{
private:
// class variable
// explicit "inline" to initialize in this translatin unit
inline
static
string
CD_Class
=
"CLASS Control-Developer"
;
// member variable
const
string
CD_Member
=
"MEMBER Control-Developer"
;
public:
// declare static to be called without object
static
string
getCDClass
()
{
return
CD_Class
;
}
// regular getter
string
getCDMember
()
{
return
CD_Member
;
}
};
main
()
{
cout
<<
"Demonstration of class vs. member variables:
\n
"
;
cout
<<
"Without constructing an object we can call the static getter from the CD namespace:
\n
"
;
cout
<<
"CD_Class: "
<<
ControlDeveloper
::
getCDClass
()
<<
'\n'
;
cout
<<
"To be able to get the member variable we need to construct"
;
cout
<<
" an object as the value will be specifically created for every object.
\n
"
;
ControlDeveloper
cd
=
ControlDeveloper
();
cout
<<
"We can now call the non static getter:
\n
"
;
cout
<<
"CD_Member: "
<<
cd
.
getCDMember
()
<<
'\n'
;
cout
<<
"We can now also use the object to call the static getter (with the same effect):
\n
"
;
cout
<<
"CD_Class: "
<<
cd
.
getCDClass
()
<<
'\n'
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment