QT 学习笔记

The Q_OBJECT macro at the beginning of the class definition is necessary for all classes that define signals or slots. Classes that use the Q_OBJECT macro must have moc run on them. This isn’t a problem because qmake automatically adds the necessary rules to the makefile. But if you forget to regenerate your makefile using … Read more

c++ 学习笔记

1. Never Return a Pointer to a Local Object 2. Function Declarations Go in Header Files It may be temptingand would be legalto put a function declaration directly in each source file that uses the function. The problem with this approach is that it is tedious and error-prone. By putting function declarations into header files, … Read more

const Objects Are Local to a File By Default

When we define a nonconst variable at global scope, it is accessible throughout the program. We can define a nonconst variable in one file andassuming an appropriate declaration has been madecan use that variable in another file: // file_1.cc int counter; // definition // file_2.cc extern int counter; // uses counter from file_1 ++counter; // … Read more

c/c++写cgi之helloworld

最近比较闲,所以又回头来学习下c/c++, 但看来看去也没什么好学的,没什么项目,光学一门语言确实没什么好看的,那就来用c/c++写个cgi来玩玩吧,之前没做过cgi,所以这样就既可了解cgi又可复习c/c++了。 先来个c/c++的cgi hello world吧,本人是在apache下运行的啊 新建文件hello.c #include <stdio.h> main() {     printf("Content-type:text/html\n\n");     printf("Hello,World!"); } 用命令$gcc –o hello hello.c 生成 hello 然后查看apache配置文件httpd.conf, 设置为:[默认的差不多就是这样] ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" <Directory "/var/www/cgi-bin">     AllowOverride All     Options ExecCGI     Order allow,deny     Allow from all </Directory> 然后把生成的hello复制到 /var/www/cgi-bin/下,如果修改了配置的话,就先重起apache, 这时输入http://localhost/cgi-bin/hello,就应该可以看到结果了

make_pair

关于make_pair(1, “Test”)为什么有错: 1 先声明一些东西: template <typename T1, typename T2> struct pair { T1 first; T2 second; // … }; template <typename T1, typename T2> pair<T1, T2> make_pair(T1 const&, T2 const&); 2 当调用make_pair(1, “Test”)时 2.1 类型推导 #1 T1 = int; #2 由于”Test”的类型是char const[5], 而且函数参数T2 const&是引用, 所以T2 = char const[5] 2.2 实例化函数 pair<int, char const[5]> make_pair<>(int const&, … Read more

内存分配方式

内存分配方式有三种: (1) 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static 变量。 (2) 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。(不要用return 语句返回指向“栈内存”的指针,因为该内存在函数结束时自动消亡) char *GetString(void) { char p[] = “hello world”; return p; // 编译器将提出警告 } void Test4(void) { char *str = NULL; str = GetString(); // str 的内容是垃圾 cout<< str << endl; } 下面这种方式可以避免这种情况: char *GetString2(void) { char *p = “hello world”; return p; } void Test5(void) { char *str … Read more

引用与指针的比较

引用和指针的区别在于下面几点: 1. 创建引用时必须初始化,而且不能初始化为null;  而指针可以不初始化,也可以初始化为null int &a; //error, references must be initialized int *p; //right int &a = NULL; //error, can’t init as NULL int *p = NULL; //right 2. 引用初始化后,就不再改变; 而指针随时可以改变所指的对象。 int a = 1; int b = 2; int &r = a; //r成为a的引用,即别名,r恒为a 的引用,不可改变 r = b;//此时不要以为r变成了b的引用,而是把b的值赋给了a, 此是a, r 都为b的值2 r = 3;//a, r的值都变成了3,b的值不变,仍为2 指针却是可以随时改变所指的对象 int a … Read more

sizeof

c语言里的sizeof 是一个计算数据存储空间大小的单目运算符,它返回数据所占的字节个数, 我们可以先来看看MSND里的定义: sizeof Operator sizeof expression The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t. The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses). When applied to a structure … Read more