Last active
January 31, 2018 07:15
-
-
Save ChunMinChang/9b70462936dc469cbb832e46ebde0acd to your computer and use it in GitHub Desktop.
Usage of const #cppTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ g++ class-array.cpp --std=c++11 | |
#include <iostream> | |
class A | |
{ | |
public: | |
A() {} | |
~A() {} | |
void setValue(int v) { value = v; } | |
int getValue() { return value; } | |
private: | |
int value; | |
}; | |
class B | |
{ | |
public: | |
B(int v) | |
: value(v) | |
{} | |
~B() {} | |
void setValue(int v) { value = v; } | |
int getValue() { return value; } | |
private: | |
int value; | |
}; | |
int main() | |
{ | |
A array[3]; | |
array[0].setValue(10); | |
array[1].setValue(12); | |
array[2].setValue(16); | |
std::cout << array[0].getValue() << std::endl; | |
std::cout << array[1].getValue() << std::endl; | |
std::cout << array[2].getValue() << std::endl; | |
// B list[2]; | |
// class-array.cpp:38:5: error: no matching constructor for initialization of | |
// 'B [2]' | |
// B list[2]; | |
// ^ | |
// class-array.cpp:17:3: note: candidate constructor not viable: requires single | |
// argument 'v', but no arguments were provided | |
// B(int v) | |
// ^ | |
// class-array.cpp:14:7: note: candidate constructor | |
// (the implicit copy constructor) not viable: requires 1 argument, but 0 | |
// were provided | |
// class B | |
// ^ | |
// 1 error generated. | |
B list[2] { {3}, {6} }; // C++ 11 style. | |
std::cout << list[0].getValue() << std::endl; | |
std::cout << list[1].getValue() << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class A | |
{ | |
public: | |
A(int aValue) | |
: mValue(aValue) | |
{ | |
} | |
~A() | |
{ | |
} | |
void setValue(int aValue) | |
{ | |
mValue = aValue; | |
} | |
int PlusOne() | |
{ | |
mValue = mValue + 1; | |
return mValue; | |
} | |
// int PlusOne2() const | |
// { | |
// mValue = mValue + 1; | |
// return mValue; | |
// } | |
// | |
// Cause error if we uncomment the PlusOne2() above: | |
// | |
// test.cpp:28:12: error: cannot assign to non-static data member within const member function | |
// 'PlusOne2' | |
// mValue = mValue + 1; | |
// ~~~~~~ ^ | |
// test.cpp:26:7: note: member function 'A::PlusOne2' is declared const here | |
// int PlusOne2() const | |
// ~~~~^~~~~~~~~~~~~~~~ | |
// 1 error generated. | |
// | |
// The const added after a member function can prevent the member variables | |
// from being changed. The function ends with a const has only | |
// "read" privilege. It can only "read" the member variables. | |
// It can NOT "write" any member variable. That's why it cause an error. | |
// | |
// By this property, we could add "const" at the end to all the | |
// read-only functions like below. | |
int getValue() const | |
{ | |
// mValue = 10; // Cause a compiler error if we try to change mValue here! | |
return mValue; | |
} | |
// On the other hand, if we put const in the return type of functions: | |
// const int getValue(), that means we return a int value that cannot be changed. | |
private: | |
int mValue; | |
}; | |
int main() | |
{ | |
A a(3); | |
std::cout << a.getValue() << std::endl; | |
std::cout << a.PlusOne() << std::endl; | |
// std::cout << a.PlusOne2() << std::endl; | |
std::cout << a.getValue() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment