mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-08 13:05:18 +08:00
support bazel complie this project and format code.
This commit is contained in:
17
basic_content/const/BUILD
Normal file
17
basic_content/const/BUILD
Normal file
@@ -0,0 +1,17 @@
|
||||
# please run `bazel run basic_content/const:const_function`
|
||||
# please run `bazel run basic_content/const:const_num`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "const_function",
|
||||
srcs = ["const_function.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "const_num",
|
||||
srcs = ["const_num.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
@@ -19,7 +19,7 @@ const int a=100;
|
||||
+ 类型检查
|
||||
|
||||
+ const常量与`#define`宏定义常量的区别:
|
||||
> ~~<u>**const常量具有类型,编译器可以进行安全检查;#define宏定义没有数据类型,只是简单的字符串替换,不能进行安全检查。**</u>~~感谢两位大佬指出这里问题,见:[issue](https://github.com/Light-City/CPlusPlusThings/issues/5)
|
||||
> <strong><s>const常量具有类型,编译器可以进行安全检查;#define宏定义没有数据类型,只是简单的字符串替换,不能进行安全检查。</s></strong>感谢两位大佬指出这里问题,见:[issue](https://github.com/Light-City/CPlusPlusThings/issues/5)
|
||||
|
||||
+ const常量支持所有类型。
|
||||
+ 其他情况下它只是一个 `const` 限定的变量,不要将与常量混淆。
|
||||
@@ -28,7 +28,7 @@ const int a=100;
|
||||
|
||||
```cpp
|
||||
void f(const int i){
|
||||
i++; //error!
|
||||
i++; // error!
|
||||
}
|
||||
```
|
||||
|
||||
@@ -58,9 +58,9 @@ int main(){
|
||||
> const常量在不同文件的访问
|
||||
|
||||
```cpp
|
||||
//extern_file1.cpp
|
||||
// extern_file1.cpp
|
||||
extern const int ext=12;
|
||||
//extern_file2.cpp
|
||||
// extern_file2.cpp
|
||||
#include<iostream>
|
||||
extern const int ext;
|
||||
int main(){
|
||||
@@ -88,22 +88,34 @@ const int i,j=0 // error: uninitialized const ‘i’
|
||||
与指针相关的const有四种:
|
||||
|
||||
```c++
|
||||
const char * a; //指向const对象的指针或者说指向常量的指针。
|
||||
char const * a; //同上
|
||||
char * const a; //指向类型对象的const指针。或者说常指针、const指针。
|
||||
const char * const a; //指向const对象的const指针。
|
||||
const char * a; // 指向const对象的指针或者说指向常量的指针。
|
||||
char const * a; // 同上
|
||||
char * const a; // 指向类型对象的const指针。或者说常指针、const指针。
|
||||
const char * const a; // 指向const对象的const指针。
|
||||
```
|
||||
|
||||
> **小结:** <br>
|
||||
> 如果*const*位于`*`的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;<br>如果const位于`*`的右侧,*const*就是修饰指针本身,即指针本身是常量。
|
||||
|
||||
**另一种解读方式**<br>
|
||||
利用英文从右边往左边读,并且以to为分界,to之前为描述指针的特性,to之后为描述目标的特性<br>
|
||||
```c++
|
||||
const char * p; //p is a pointer to const char
|
||||
char const * p; //同上
|
||||
char * const p; //p is a const pointer to char
|
||||
const char * const p; //p is a const pointer to const char
|
||||
```
|
||||
当指针被加上const特性,则指针不可改变指向的地址<br>
|
||||
当指向的目标特性为char,则内容可以透过指针被修改,如: *char='y';<br>
|
||||
当指向的目标特性为const char,则内容不可透过指针修改<br>
|
||||
|
||||
具体使用如下:
|
||||
|
||||
(1) **指向常量的指针**
|
||||
|
||||
```cpp
|
||||
const int *ptr;
|
||||
*ptr = 10; //error
|
||||
*ptr = 10; // error
|
||||
```
|
||||
|
||||
ptr是一个指向int类型const对象的指针,const定义的是int类型,也就是ptr所指向的对象类型,而不是ptr本身,所以ptr可以不用赋初始值。但是不能通过ptr去修改所指对象的值。
|
||||
@@ -113,7 +125,7 @@ ptr是一个指向int类型const对象的指针,const定义的是int类型,
|
||||
```c++
|
||||
const int p = 10;
|
||||
const void * vp = &p;
|
||||
void *vp = &p; //error
|
||||
void *vp = &p; // error
|
||||
```
|
||||
|
||||
另外一个重点是:**允许把非const对象的地址赋给指向const对象的指针**。
|
||||
@@ -123,7 +135,7 @@ void *vp = &p; //error
|
||||
```c++
|
||||
const int *ptr;
|
||||
int val = 3;
|
||||
ptr = &val; //ok
|
||||
ptr = &val; // ok
|
||||
```
|
||||
|
||||
我们不能通过ptr指针来修改val的值,即使它指向的是非const对象!
|
||||
@@ -140,22 +152,33 @@ cout<<*ptr<<endl;
|
||||
|
||||
(2) **常指针**
|
||||
|
||||
const指针必须进行初始化,且const指针的值不能修改。
|
||||
const指针必须进行初始化,且const指针指向的值能修改,但指向不能修改。
|
||||
|
||||
```cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
|
||||
int num=0;
|
||||
int * const ptr=# //const指针必须初始化!且const指针的值不能修改
|
||||
int * t = #
|
||||
*t = 1;
|
||||
int num=0, num1=1;
|
||||
int * const ptr=# // const指针必须初始化!且const指针的指向不能修改
|
||||
ptr = &num1; // error! const指针不能修改指向!
|
||||
cout<<*ptr<<endl;
|
||||
}
|
||||
```
|
||||
|
||||
上述修改ptr指针所指向的值,可以通过非const指针来修改。
|
||||
代码出现编译错误:const指针不能修改指向。
|
||||
|
||||
```cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int num=0, num1=1;
|
||||
int * const ptr=# // const指针必须初始化!且const指针的指向不能修改
|
||||
*ptr = 1;
|
||||
cout<<*ptr<<endl;
|
||||
}
|
||||
```
|
||||
|
||||
代码无事发生,正常输出1。
|
||||
|
||||
最后,当把一个const常量的地址赋值给ptr时候,由于ptr指向的是一个变量,而不是const常量,所以会报错,出现:const int`*` -> int `*`错误!
|
||||
|
||||
@@ -164,7 +187,7 @@ int main(){
|
||||
using namespace std;
|
||||
int main(){
|
||||
const int num=0;
|
||||
int * const ptr=# //error! const int* -> int*
|
||||
int * const ptr=# // error! const int* -> int*
|
||||
cout<<*ptr<<endl;
|
||||
}
|
||||
```
|
||||
@@ -284,7 +307,7 @@ const对象只能访问const成员函数,而非const对象可以访问任意的
|
||||
例如:
|
||||
|
||||
```cpp
|
||||
//apple.cpp
|
||||
// apple.cpp
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
@@ -298,41 +321,42 @@ public:
|
||||
int getCount() const;
|
||||
|
||||
};
|
||||
//main.cpp
|
||||
#include<iostream>
|
||||
#include"apple.cpp"
|
||||
using namespace std;
|
||||
|
||||
Apple::Apple(int i):apple_number(i)
|
||||
// apple.cpp
|
||||
Apple::Apple(int i) : apple_number(i)
|
||||
{
|
||||
|
||||
}
|
||||
int Apple::add(){
|
||||
take(1);
|
||||
int Apple::add(int num)
|
||||
{
|
||||
take(num);
|
||||
return 0;
|
||||
}
|
||||
int Apple::add(int num) const{
|
||||
int Apple::add(int num) const
|
||||
{
|
||||
take(num);
|
||||
return num;
|
||||
return 0;
|
||||
}
|
||||
void Apple::take(int num) const
|
||||
{
|
||||
cout<<"take func "<<num<<endl;
|
||||
std::cout << "take func " << num << std::endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
add(); // error
|
||||
// add(); // error
|
||||
return apple_number;
|
||||
}
|
||||
int main(){
|
||||
int main()
|
||||
{
|
||||
Apple a(2);
|
||||
cout<<a.getCount()<<endl;
|
||||
cout << a.getCount() << endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
}
|
||||
// main.cpp
|
||||
```
|
||||
> 编译: g++ -o main main.cpp apple.cpp<br>
|
||||
> 编译:bazel run basic_content/const/class_const/first_example:main<br>
|
||||
|
||||
此时报错,上面getCount()方法中调用了一个add方法,而add方法并非const修饰,所以运行报错。也就是说const成员函数只能访问const成员函数。
|
||||
|
||||
|
||||
16
basic_content/const/class_const/c++11_example/BUILD
Normal file
16
basic_content/const/class_const/c++11_example/BUILD
Normal file
@@ -0,0 +1,16 @@
|
||||
# please run `bazel run basic_content/const/class_const/c++11_example:main`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "apple",
|
||||
srcs = ["apple.cpp"],
|
||||
hdrs = ["apple.h"],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["main.cpp"],
|
||||
deps = [
|
||||
":apple",
|
||||
],
|
||||
)
|
||||
@@ -1,15 +1,19 @@
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
int people[100];
|
||||
public:
|
||||
Apple(int i);
|
||||
//使用c++11标准编译
|
||||
static const int apple_number=10;
|
||||
//const int apple_number=10;
|
||||
void take(int num) const;
|
||||
int add(int num);
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
Apple::Apple(int i) {}
|
||||
int Apple::add(int num) {
|
||||
take(num);
|
||||
return 0;
|
||||
}
|
||||
int Apple::add(int num) const {
|
||||
take(num);
|
||||
return 0;
|
||||
}
|
||||
void Apple::take(int num) const { cout << "take func " << num << endl; }
|
||||
int Apple::getCount() const {
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
13
basic_content/const/class_const/c++11_example/apple.h
Normal file
13
basic_content/const/class_const/c++11_example/apple.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
class Apple {
|
||||
public:
|
||||
Apple(int i);
|
||||
// 使用c++11标准编译
|
||||
static const int apple_number = 10;
|
||||
// const int apple_number=10;
|
||||
void take(int num) const;
|
||||
int add(int num);
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
Binary file not shown.
@@ -1,31 +1,11 @@
|
||||
#include<iostream>
|
||||
#include"apple.cpp"
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
Apple::Apple(int i)
|
||||
{
|
||||
|
||||
}
|
||||
int Apple::add(int num){
|
||||
take(num);
|
||||
}
|
||||
int Apple::add(int num) const{
|
||||
take(num);
|
||||
}
|
||||
void Apple::take(int num) const
|
||||
{
|
||||
cout<<"take func "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
int main(){
|
||||
Apple a(2);
|
||||
cout<<a.getCount()<<endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
int main() {
|
||||
Apple a(2);
|
||||
cout << a.getCount() << endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
}
|
||||
|
||||
16
basic_content/const/class_const/first_example/BUILD
Normal file
16
basic_content/const/class_const/first_example/BUILD
Normal file
@@ -0,0 +1,16 @@
|
||||
# please run `bazel run basic_content/const/class_const/first_example:main`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "apple",
|
||||
srcs = ["apple.cpp"],
|
||||
hdrs = ["apple.h"],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["main.cpp"],
|
||||
deps = [
|
||||
":apple",
|
||||
],
|
||||
)
|
||||
@@ -1,13 +1,20 @@
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
int people[100];
|
||||
public:
|
||||
Apple(int i);
|
||||
const int apple_number;
|
||||
void take(int num) const;
|
||||
int add(int num);
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
|
||||
Apple::Apple(int i) : apple_number(i) {}
|
||||
int Apple::add(int num) {
|
||||
take(num);
|
||||
return 0;
|
||||
}
|
||||
int Apple::add(int num) const {
|
||||
take(num);
|
||||
return 0;
|
||||
}
|
||||
void Apple::take(int num) const {
|
||||
std::cout << "take func " << num << std::endl;
|
||||
}
|
||||
int Apple::getCount() const {
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
11
basic_content/const/class_const/first_example/apple.h
Normal file
11
basic_content/const/class_const/first_example/apple.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
class Apple {
|
||||
public:
|
||||
Apple(int i);
|
||||
const int apple_number;
|
||||
void take(int num) const;
|
||||
int add(int num);
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
@@ -1,32 +1,12 @@
|
||||
#include<iostream>
|
||||
#include"apple.cpp"
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
Apple::Apple(int i):apple_number(i)
|
||||
{
|
||||
|
||||
}
|
||||
int Apple::add(int num){
|
||||
take(num);
|
||||
}
|
||||
int Apple::add(int num) const{
|
||||
take(num);
|
||||
}
|
||||
void Apple::take(int num) const
|
||||
{
|
||||
cout<<"take func "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
int main(){
|
||||
Apple a(2);
|
||||
cout<<a.getCount()<<endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
int main() {
|
||||
Apple a(2);
|
||||
cout << a.getCount() << endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
}
|
||||
|
||||
18
basic_content/const/class_const/overload_example/BUILD
Normal file
18
basic_content/const/class_const/overload_example/BUILD
Normal file
@@ -0,0 +1,18 @@
|
||||
# please run `bazel run basic_content/const/class_const/overload_example:main`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "apple",
|
||||
srcs = ["apple.cpp"],
|
||||
hdrs = ["apple.h"],
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["main.cpp"],
|
||||
deps = [
|
||||
":apple",
|
||||
],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
@@ -1,13 +1,22 @@
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
int people[100];
|
||||
public:
|
||||
Apple(int i);
|
||||
static const int apple_number;
|
||||
void take(int num) const;
|
||||
int add();
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
|
||||
int Apple::apple_number = 10;
|
||||
|
||||
Apple::Apple(int i) { apple_number = i; }
|
||||
int Apple::add() {
|
||||
take(1);
|
||||
return 0;
|
||||
}
|
||||
int Apple::add(int num) const {
|
||||
take(num);
|
||||
return num;
|
||||
}
|
||||
void Apple::take(int num) const {
|
||||
std::cout << "take func " << num << std::endl;
|
||||
}
|
||||
int Apple::getCount() const {
|
||||
take(1);
|
||||
add(); // error
|
||||
return apple_number;
|
||||
}
|
||||
11
basic_content/const/class_const/overload_example/apple.h
Normal file
11
basic_content/const/class_const/overload_example/apple.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
class Apple {
|
||||
public:
|
||||
Apple(int i);
|
||||
static int apple_number;
|
||||
void take(int num) const;
|
||||
int add();
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
Binary file not shown.
@@ -1,38 +1,12 @@
|
||||
#include<iostream>
|
||||
#include"apple.cpp"
|
||||
using namespace std;
|
||||
const int Apple::apple_number=10;
|
||||
#include<iostream>
|
||||
#include"apple.cpp"
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
Apple::Apple(int i):apple_number(i)
|
||||
{
|
||||
|
||||
}
|
||||
int Apple::add(){
|
||||
take(1);
|
||||
return 0;
|
||||
}
|
||||
int Apple::add(int num) const{
|
||||
take(num);
|
||||
return num;
|
||||
}
|
||||
void Apple::take(int num) const
|
||||
{
|
||||
cout<<"take func "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
add(); // error
|
||||
return apple_number;
|
||||
}
|
||||
int main(){
|
||||
Apple a(2);
|
||||
cout<<a.getCount()<<endl;
|
||||
a.add(10);
|
||||
// const Apple b(3);
|
||||
// b.add(); // error
|
||||
return 0;
|
||||
int main() {
|
||||
Apple a(2);
|
||||
cout << a.getCount() << endl;
|
||||
a.add(10);
|
||||
// const Apple b(3);
|
||||
// b.add(); // error
|
||||
return 0;
|
||||
}
|
||||
|
||||
17
basic_content/const/class_const/static_example/BUILD
Normal file
17
basic_content/const/class_const/static_example/BUILD
Normal file
@@ -0,0 +1,17 @@
|
||||
# please run `bazel run basic_content/const/class_const/static_example:main`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "apple",
|
||||
srcs = ["apple.cpp"],
|
||||
hdrs = ["apple.h"],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["main.cpp"],
|
||||
deps = [
|
||||
":apple",
|
||||
],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
@@ -1,14 +1,22 @@
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
int people[100];
|
||||
public:
|
||||
Apple(int i);
|
||||
static int ap; //在类实现文件中定义并初始化
|
||||
static const int apple_number;
|
||||
void take(int num) const;
|
||||
int add(int num);
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
|
||||
const int Apple::apple_number = 10;
|
||||
int Apple::ap = 666;
|
||||
Apple::Apple(int i) {}
|
||||
int Apple::add(int num) {
|
||||
take(num);
|
||||
return 0;
|
||||
}
|
||||
int Apple::add(int num) const {
|
||||
take(num);
|
||||
return 0;
|
||||
}
|
||||
void Apple::take(int num) const {
|
||||
std::cout << "take func " << num << std::endl;
|
||||
}
|
||||
int Apple::getCount() const {
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
11
basic_content/const/class_const/static_example/apple.h
Normal file
11
basic_content/const/class_const/static_example/apple.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
class Apple {
|
||||
public:
|
||||
Apple(int i);
|
||||
static int ap; // 在类实现文件中定义并初始化
|
||||
static const int apple_number;
|
||||
void take(int num) const;
|
||||
int add(int num);
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
Binary file not shown.
@@ -1,34 +1,11 @@
|
||||
#include<iostream>
|
||||
#include"apple.cpp"
|
||||
using namespace std;
|
||||
const int Apple::apple_number=10;
|
||||
int Apple::ap=666;
|
||||
Apple::Apple(int i)
|
||||
{
|
||||
|
||||
}
|
||||
int Apple::add(int num){
|
||||
take(num);
|
||||
}
|
||||
int Apple::add(int num) const{
|
||||
take(num);
|
||||
}
|
||||
void Apple::take(int num) const
|
||||
{
|
||||
cout<<"take func "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
int main(){
|
||||
Apple a(2);
|
||||
cout<<a.getCount()<<endl;
|
||||
cout<<a.ap<<endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
int main() {
|
||||
Apple a(2);
|
||||
std::cout << a.getCount() << std::endl;
|
||||
std::cout << a.ap << std::endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void f(const int i){
|
||||
i=10; // error: assignment of read-only parameter ‘i’
|
||||
cout<<i<<endl;
|
||||
void f(const int i) {
|
||||
i = 10; // error: assignment of read-only parameter ‘i’
|
||||
cout << i << endl;
|
||||
}
|
||||
|
||||
int main(){
|
||||
f(1);
|
||||
}
|
||||
int main() { f(1); }
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
const int b = 10;
|
||||
b = 0; //error
|
||||
const string s = "helloworld";
|
||||
const int i,j=0;
|
||||
int main() {
|
||||
const int b = 10;
|
||||
b = 0; // error
|
||||
const string s = "helloworld";
|
||||
const int i, j = 0;
|
||||
}
|
||||
|
||||
14
basic_content/const/extern_const/BUILD
Normal file
14
basic_content/const/extern_const/BUILD
Normal file
@@ -0,0 +1,14 @@
|
||||
# please run `bazel run basic_content/const/extern_const:const_file1` and `# please run `bazel run basic_content/const/extern_const:file`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "const_file1",
|
||||
srcs = ["const_file1.cpp", "const_file2.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "file",
|
||||
srcs = ["file1.cpp", "file2.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
@@ -1 +1 @@
|
||||
extern const int ext=12;
|
||||
extern const int ext = 12;
|
||||
@@ -1,11 +1,8 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
/**
|
||||
* by 光城
|
||||
* compile: g++ -o file const_file2.cpp const_file1.cpp
|
||||
* execute: ./file
|
||||
*/
|
||||
extern const int ext;
|
||||
int main(){
|
||||
|
||||
std::cout<<ext<<std::endl;
|
||||
}
|
||||
int main() { std::cout << ext << std::endl; }
|
||||
@@ -1 +1 @@
|
||||
int ext;
|
||||
int ext;
|
||||
@@ -1,11 +1,8 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
/**
|
||||
* by 光城
|
||||
* compile: g++ -o file file2.cpp file1.cpp
|
||||
* execute: ./file
|
||||
*/
|
||||
extern int ext;
|
||||
int main(){
|
||||
|
||||
std::cout<<(ext+10)<<std::endl;
|
||||
}
|
||||
int main() { std::cout << (ext + 10) << std::endl; }
|
||||
24
basic_content/const/funciton_const/condition1/BUILD
Normal file
24
basic_content/const/funciton_const/condition1/BUILD
Normal file
@@ -0,0 +1,24 @@
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition1`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition2`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition3`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "condition1",
|
||||
srcs = ["condition1.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "condition2",
|
||||
srcs = ["condition2.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "condition3",
|
||||
srcs = ["condition3.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
@@ -1,8 +1,7 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(){
|
||||
const int *ptr;
|
||||
*ptr=10; //error
|
||||
int main() {
|
||||
const int *ptr;
|
||||
*ptr = 10; // error
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(){
|
||||
const int p = 10;
|
||||
const void *vp = &p;
|
||||
void *vp = &p; //error
|
||||
int main() {
|
||||
const int p = 10;
|
||||
const void *vp = &p;
|
||||
void *vp = &p; // error
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
const int *ptr;
|
||||
int val = 3;
|
||||
ptr = &val; //ok
|
||||
int *ptr1 = &val;
|
||||
*ptr1=4;
|
||||
cout<<*ptr<<endl;
|
||||
|
||||
int main() {
|
||||
const int *ptr;
|
||||
int val = 3;
|
||||
ptr = &val; // ok
|
||||
int *ptr1 = &val;
|
||||
*ptr1 = 4;
|
||||
cout << *ptr << endl;
|
||||
}
|
||||
|
||||
23
basic_content/const/funciton_const/condition2/BUILD
Normal file
23
basic_content/const/funciton_const/condition2/BUILD
Normal file
@@ -0,0 +1,23 @@
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition1`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition2`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition3`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "condition1",
|
||||
srcs = ["condition1.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "condition2",
|
||||
srcs = ["condition2.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "condition3",
|
||||
srcs = ["condition3.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
Binary file not shown.
@@ -1,10 +1,9 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
|
||||
int num=0;
|
||||
int * const ptr=# //const指针必须初始化!且const指针的值不能修改
|
||||
int * t = #
|
||||
*t = 1;
|
||||
cout<<*ptr<<endl;
|
||||
int main() {
|
||||
int num = 0;
|
||||
int *const ptr = # // const指针必须初始化!且const指针的指向不能修改
|
||||
int *t = #
|
||||
*t = 1;
|
||||
cout << *ptr << endl;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
|
||||
const int num=0;
|
||||
int * const ptr=# //error! const int* -> int*
|
||||
cout<<*ptr<<endl;
|
||||
int main() {
|
||||
const int num = 0;
|
||||
int *const ptr = # // error! const int* -> int*
|
||||
cout << *ptr << endl;
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,8 +1,7 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
|
||||
const int num=10;
|
||||
const int * const ptr=# //error! const int* -> int*
|
||||
cout<<*ptr<<endl;
|
||||
int main() {
|
||||
const int num = 10;
|
||||
const int *const ptr = #
|
||||
cout << *ptr << endl;
|
||||
}
|
||||
|
||||
9
basic_content/const/funciton_const/condition3/BUILD
Normal file
9
basic_content/const/funciton_const/condition3/BUILD
Normal file
@@ -0,0 +1,9 @@
|
||||
# please run `bazel run basic_content/const/funciton_const/condition3:condition1`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "condition1",
|
||||
srcs = ["condition1.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
Binary file not shown.
@@ -1,10 +1,8 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
|
||||
const int p = 3;
|
||||
const int * const ptr = &p;
|
||||
cout<<*ptr<<endl;
|
||||
|
||||
int main() {
|
||||
const int p = 3;
|
||||
const int *const ptr = &p;
|
||||
cout << *ptr << endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user