摘要:
在C++中,基于以下如下我们通过以引用reference的形式传递变量。 (1)To modify local variables of the caller function A reference(or pointer) allows called function to modify a local variable of the caller function. For example, consider te following example program where fun() is able to modify local variable x of main().... 阅读全文
摘要:
在C++中,引用比指针更加的安全,一方面是因为引用咋定义时必须进行初始化,另一方面是引用一旦被初始化就无法使其与其他对象相关联。 但是,在使用引用的地方仍然会有一些例外。 (1)Reference to value at uninitialized pointer1 int *ptr;2 int &ref = *ptr; //Reference to value at some random memory location (2)Reference to a local variable is returned 1 int& fun()2 {3 int a = 10;4... 阅读全文
摘要:
When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. 1 #include 2 using namespace std; 3 4 int main() 5 { 6 int x = 10; 7 8 // ref is a reference to x. 9 int& ref = x;... 阅读全文
摘要:
Predict the output of the following program? 1 #include 2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 int main() 9 {10 cout 2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 int main() 9 {10 Empty a, b;11 12 if (&a == &b)13 {14 cout 2 using namespace std... 阅读全文
摘要:
A class declaration can contain static object of self type,it can also have pointer to self type,but it can not have a non-static object of self type。 例如,下面的程序可运行。 1 // A class can have a static member of self type 2 #include 3 4 using namespace std; 5 6 class Test 7 { 8 static Test self; ... 阅读全文
摘要:
在C++中,除了以下几点外,struct和class是相同的。 (1)class的成员的默认访问控制是private,而struct的成员的默认访问权限是public。 例如,program 1会编译失败,而program 2可正常运行。 1 // Program 1 2 #include 3 4 class Test 5 { 6 int x; // x is private 7 }; 8 9 int main()10 {11 Test t;12 t.x = 20; // compiler error because x is private13 getc... 阅读全文
摘要:
C++语言1.1.全局变量和局部变量在内存中是否有区别?如果有,是什么区别?解析: 1)预备知识——程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分: (1)栈区(stack)——由编译器自动分配、自动释放,比如存放函数的参数值、局部变量的值等。其操作方式类似于数据结构中的栈。 (2)堆区(heap)——一般由程序员分配释放,若程序员不释放,程序结束时可能由操作系统回收。分配方式类似于链表。 (3)全局静态区——全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域(.data),未初始化的全局变量和未初始化的静态变量在相邻的另一块区域(.bs... 阅读全文