support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View File

@@ -0,0 +1,23 @@
# please run `bazel run basic_content/inline:inline_virtual`
# please run `bazel run basic_content/inline:main`
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "inline",
hdrs = ["inline.h"],
)
cc_binary(
name = "inline_virtual",
srcs = ["inline_virtual.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "main",
srcs = ["inline.cpp"],
deps = [
":inline",
],
copts = ["-std=c++11"]
)

View File

@@ -27,7 +27,7 @@ public:
{
};
void f1(int x); ///< 声明后要想成为内联函数必须在定义处加inline关键字。
void f2(int x); ///< 声明后要想成为内联函数必须在定义处加inline关键字。
};
```
@@ -128,7 +128,7 @@ int main()
delete ptr;
ptr = nullptr;
system("pause");
return 0;
}
```

Binary file not shown.

View File

@@ -1,57 +1,42 @@
#include <iostream>
#include "inline.h"
#include <iostream>
using namespace std;
/**
* @brief inline要起作用,inline要与函数定义放在一起,inline是一种“用于实现的关键字,而不是用于声明的关键字”
* @brief
* inline要起作用,inline要与函数定义放在一起,inline是一种“用于实现的关键字,而不是用于声明的关键字”
*
* @param x
* @param y
*
* @return
* @return
*/
int Foo(int x,int y); // 函数声明
inline int Foo(int x,int y) // 函数定义
int Foo(int x, int y); // 函数声明
inline int Foo(int x, int y) // 函数定义
{
return x+y;
return x + y;
}
// 定义处加inline关键字推荐这种写法
inline void A::f1(int x){
}
inline void A::f1(int x) {}
/**
* @brief 内联能提高函数效率,但并不是所有的函数都定义成内联函数!内联是以代码膨胀(复制)为代价,仅仅省去了函数调用的开销,从而提高函数的执行效率。
* @brief
* 内联能提高函数效率,但并不是所有的函数都定义成内联函数!内联是以代码膨胀(复制)为代价,仅仅省去了函数调用的开销,从而提高函数的执行效率。
* 如果执行函数体内代码的时间相比于函数调用的开销较大,那么效率的收货会更少!另一方面,每一处内联函数的调用都要复制代码,将使程序的总代码量增大,消耗更多的内存空间。
* 以下情况不宜用内联:
* 1 如果函数体内的代码比较长,使得内联将导致内存消耗代价比较高。
* (2) 如果函数体内出现循环,那么执行函数体内代码的时间要比函数调用的开销大。
*
* @return
* @return
*/
int main()
{
cout<<Foo(1,2)<<endl;
}
int main() { cout << Foo(1, 2) << endl; }
/**
* 编译器对 inline 函数的处理步骤
* 将 inline 函数体复制到 inline 函数调用点处;
* 为所用 inline 函数中的局部变量分配内存空间;
* 将 inline 函数的的输入参数和返回值映射到调用方法的局部变量空间中;
* 如果 inline 函数有多个返回点,将其转变为 inline 函数代码块末尾的分支(使用 GOTO
* 如果 inline 函数有多个返回点,将其转变为 inline 函数代码块末尾的分支(使用
* GOTO
*/

View File

@@ -1,19 +1,18 @@
class A
{
#pragma once
class A {
public:
void f1(int x);
void f1(int x);
/**
* @brief 类中定义了的函数是隐式内联函数,声明要想成为内联函数,必须在实现处(定义处)加inline关键字。
*
* @param x
* @param y
*/
void Foo(int x,int y) ///< 定义即隐式内联函数!
{
};
void f1(int x); ///< 声明后要想成为内联函数必须在定义处加inline关键字。
/**
* @brief
* 类中定义了的函数是隐式内联函数,声明要想成为内联函数,必须在实现处(定义处)加inline关键字。
*
* @param x
* @param y
*/
void Foo(int x, int y) ///< 定义即隐式内联函数!
{
};
void f2(int x); ///< 声明后要想成为内联函数必须在定义处加inline关键字。
};

View File

@@ -1,35 +1,31 @@
#include <iostream>
#include <iostream>
using namespace std;
class Base
{
public:
inline virtual void who()
{
cout << "I am Base\n";
}
virtual ~Base() {}
class Base {
public:
inline virtual void who() { cout << "I am Base\n"; }
virtual ~Base() {}
};
class Derived : public Base
{
public:
inline void who() // 不写inline时隐式内联
{
cout << "I am Derived\n";
}
class Derived : public Base {
public:
inline void who() // 不写inline时隐式内联
{
cout << "I am Derived\n";
}
};
int main()
{
// 此处的虚函数 who()是通过类Base的具体对象b来调用的编译期间就能确定了所以它可以是内联的但最终是否内联取决于编译器。
Base b;
b.who();
int main() {
// 此处的虚函数
// who()是通过类Base的具体对象b来调用的编译期间就能确定了所以它可以是内联的但最终是否内联取决于编译器。
Base b;
b.who();
// 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。
Base *ptr = new Derived();
ptr->who();
// 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。
Base *ptr = new Derived();
ptr->who();
// 因为Base有虚析构函数virtual ~Base() {}),所以 delete会先调用派生类Derived析构函数再调用基类Base析构函数防止内存泄漏。
delete ptr;
// 因为Base有虚析构函数virtual ~Base() {}),所以 delete
// 时会先调用派生类Derived析构函数再调用基类Base析构函数防止内存泄漏。
delete ptr;
return 0;
}
return 0;
}