0%

为什么C++中,父类的析构函数要声明为虚函数?下面让我们测试一下。

1.当父类析构函数为非虚函数时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

class Parent
{
public:
Parent()
{
cout << "Parent construct." << endl;
}

~Parent()
{
cout << "Parent deconstruct." << endl;
}
};


class Child: public Parent
{
public:
Child()
{
cout << "Child construct." << endl;
}

~Child()
{
cout << "Child deconstruct." << endl;
}
};

int main(int argc, char** argv)
{
Parent* p = new Child();
delete p;

cout << "==========" << endl;

Child* c = new Child();
delete c;
}
阅读全文 »

如何将 echo 输出的文字加上前景色、背景色、闪烁等效果

格式

1
echo -e "\033[31m hello \033[0m kitty"
  • 参数 “-e”
    确保 echo 将紧跟字符串转义处理
  • 格式 “\033[31m”
    “\033”(也可用”\e”)被转义为ESC, “\033[ XXX m”将修改当前输出属性, XXX 即例子中的 31为带设置的属性
  • 格式 “\033[0m”
    重置当前输出属性
  • “hello” “kitty”
    待输出字符串
阅读全文 »

CGI(Common Gateway Interface)

一个协议,约定了web服务器与客户端之间通信的数据及数据格式。
实现了CGI协议的程序叫做CGI脚本或CGI程序。

In computing, Common Gateway Interface (CGI) offers a standard protocol for web servers to execute programs that execute like Console applications (also called Command-line interface programs) running on a server that generates web pages dynamically. Such programs are known as CGI scripts or simply as CGIs. The specifics of how the script is executed by the server are determined by the server. In the common case, a CGI script executes at the time a request is made and generates HTML

阅读全文 »

API

1
2
3
4
5
6
7
8
9
10
11
12
13
-- create function,It returns a value of type thread, which represents the new coroutine
co = coroutine.create(funtion
print("hi")
end)

-- check the state of a coroutine
coroutine.status(co)

-- starts the execution of a coroutine
coroutine.resume(co)

-- The real power of coroutines stems from the yield function, which allows a running coroutine to suspend its execution so that it can be resumed later
coroutine.yield()

Status

  • suspend
  • running
  • normal
  • dead

Status Switching

1
2
3
4
5
6
7
8
9
10
11
(create)
|
|
v caller resume the coroutime resume other coroutine
suspend -------------------------------> running --------------------------------> normal
<------------------------------ | <-------------------------------
yield |return other coroutine yield/return
(return controll to caller) |
v
dead
(return controll to caller)

note

Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.

A useful facility in Lua is that a pair resume-yield can exchange data between them. The first resume, which has no corresponding yield waiting for it, passes its extra arguments as arguments to the coroutine main function

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function status()
print("co1's status :"..coroutine.status(co1).." ,co2's status: "..coroutine.status(co2))
end

co1 = coroutine.create(function ( a )
print("arg is :"..a)
status()

local stat, rere = coroutine.resume(co2,"2")
print("resume's return is "..rere)
status()

local stat2,rere2 = coroutine.resume(co2,"4")
print("resume's return is "..rere2)

local arg = coroutine.yield("6")
end)

co2 = coroutine.create(function ( a )
print("arg is :"..a)
status()

local rey = coroutine.yield("3")
print("yeild's return is " .. rey)
status()

coroutine.yield("5")
end)

stat,mainre = coroutine.resume(co1,"1")
status()
print("last return is "..mainre)

output

1
2
3
4
5
6
7
8
9
10
11
arg  1
co1's status :running ,co2's status: suspended
arg is :2
co1's status :normal ,co2's status: running
resume's return is 3
co1's status :running ,co2's status: suspended
yeild's return is 4
co1's status :normal ,co2's status: running
resume's return is 5
co1's status :suspended ,co2's status: suspended
last return is 6

Reference & Thanks

先假设:A 发出一个请求到 B,期待想获得一个数据 c

阻塞(blocking-IO)与非阻塞(non-blocking-IO)

阻塞与非阻塞的区别在于,该请求是否能立即返回

  • 阻塞 请求要等B处理完c再返回(此处并没说,c会一起返回,这是“同步与异步”讨论的问题)
  • 非阻塞 请求立即返回,它只负责通知B,不会等待B处理完c
阅读全文 »

浅拷贝

Lua语言基础数据类型的拷贝采用值传递的方式;table类型变量的拷贝采用弱引用的方式,像C++语言中的引用传参数。

1
2
3
4
5
local a = {12}
local b = a

print(a)        -->table: 0x82b080
print(b)        -->table: 0x82b080

可以看到a,b 指向了同一个内存地址,即指向了同一个元素。

阅读全文 »