- 博客(39)
- 收藏
- 关注
原创 Effective C++ 总结4 类和函数:设计与声明 (条款18 - 28)
<br />18. 争取使类的接口完整并且最小<br />1) 一个完整的接口是指那种允许用户做他们想做的任何合理的事情的接口<br />2) 一个最小的接口,是指那种函数尽可能少、每两个函数都没有重叠功能的接口<br /> <br /> <br />19. 分清成员函数,非成员函数和友元函数<br />1) <br />class rational {<br />public:<br /> ...<br /> const rational operator*(const rational& rhs)
2010-12-07 11:22:00 562
原创 Effective C++ 总结3 构造函数,析构函数和赋值操作符 (条款11 - 17)
<br />11. 为需要动态分配内存的类声明一个拷贝构造函数和一个赋值操作符<br />1) 原因:如果不写copy constuctor和operator =, 如果类中含有指针准备指向动态内存,那么在copy或者=的时候会造成2个指针指向同一个对象(bitwise bopy)<br />2) 当实现拷贝构造函数和赋值操作符非常麻烦的时候,特别是可以确信程序中不会做拷贝和赋值操作的时候,可以只声明这些函数(声明为private成员)而不去定义(实现)它们.<br /> <br /> <br />12.
2010-12-06 17:32:00 595
原创 Effective C++ 总结2 内存管理 (条款5 - 10)
5. 对应的new和delete要采用相同的形式 用new的时候会发生两件事。首先,内存被分配(通过operator new 函数),然后,为被分配的内存调用一个或多个构造函数。 用delete的时候,也有两件事发生:首先,为将被释放的内存调用一个或多个析构函数,然后,释放内存(通过operator delete 函数)。 对于 delete来说会有这样一个重要的问题:内存中有多少个对象要被删除?答案决定了将有多少个析构函数会被调用。string *stringptr1 = new
2010-12-06 14:51:00 517
原创 Effective C++ 总结1 从C转向C++(条款1 - 4)
<br />1. 尽量用const和inline而不用#define<br /> 尽量用编译器而不用预处理<br /> <br />2. 尽量用<iostream>而不用<stdio.h><br /> <br />3. 尽量用new和delete而不用malloc和free<br /> new和delete可以这么有效地与构造函数和析构函数交互<br /> 可能存在的问题:比如有一个函数GetMemory()返回一个动态内存,由调用者负责释放,但是调用者不知道应该用free还是delete
2010-12-06 14:02:00 598
原创 Effective C++ 总结1 从C转向C++(条款1 - 4)
<br />1. 尽量用const和inline而不用#define<br /> 尽量用编译器而不用预处理<br /> <br />2. 尽量用<iostream>而不用<stdio.h><br /> <br />3. 尽量用new和delete而不用malloc和free<br /> new和delete可以这么有效地与构造函数和析构函数交互<br /> 可能存在的问题:比如有一个函数GetMemory()返回一个动态内存,由调用者负责释放,但是调用者不知道应该用free还是delete
2010-12-06 14:01:00 585
原创 Effective C++ 总结1 从C转向C++(条款1 - 4)
<br />1. 尽量用const和inline而不用#define<br /> 尽量用编译器而不用预处理<br /> <br />2. 尽量用<iostream>而不用<stdio.h><br /> <br />3. 尽量用new和delete而不用malloc和free<br /> new和delete可以这么有效地与构造函数和析构函数交互<br /> 可能存在的问题:比如有一个函数GetMemory()返回一个动态内存,由调用者负责释放,但是调用者不知道应该用free还是delete
2010-12-06 13:59:00 514
原创 Effective C++ 总结1 从C转向C++(条款1 - 4)
<br />1. 尽量用const和inline而不用#define<br /> 尽量用编译器而不用预处理<br /> <br />2. 尽量用<iostream>而不用<stdio.h><br /> <br />3. 尽量用new和delete而不用malloc和free<br /> new和delete可以这么有效地与构造函数和析构函数交互<br /> 可能存在的问题:比如有一个函数GetMemory()返回一个动态内存,由调用者负责释放,但是调用者不知道应该用free还是delete
2010-12-06 13:57:00 682
原创 直接初始化 和 复制初始化 的细微差别
<br />直接初始化 Object obj1(obj2);<br />复制初始化 Object obj1 = obj2;<br /> <br /> <br />区别<br />1. 一般情况下都调用copy constructor<br /> <br />2. 如果obj2 和obj1 不是一个类型(父类和子类),并且定了乐explicit构造函数 那么直接初始化会调用这个constructor<br />#include <iostream>using namespace std;cl
2010-12-03 14:18:00 980
转载 Template Specialization and Partial Template Specialization
<br />(1) 类模板特化<br />有时为了需要,针对特定的类型,需要对模板进行特化,也就是特殊处理.例如,stack类模板针对bool类型,因为实际上bool类型只需要一个二进制位,就可以对其进行存储,使用一个字或者一个字节都是浪费存储空间的.<br />template <class T><br />class stack {};<br />template < ><br />class stack<bool> { //…// };<br />上述定义中template < >告诉编
2010-12-03 13:56:00 1147
原创 Question 48: In C++, which of the following statements accurately describe a base class destructor calling a virtual function ov
<br /> A. The base class destructor calls the virtual function override of the derived class through the vtable.<br /> B. The base class destructor cannot call the virtual function override of the derived class because the derived class portion of th
2010-12-03 10:57:00 2581
原创 Question 46: Which of the following statements describe correct methods of handling C++ exceptions?
<br /> <br /> A. Once an exception is thrown, the compiler unwinds the heap, freeing any memory dynamically allocated within the block from which the exception was thrown.<br /> B. In a hierarchy of exception classes, the order of handling exceptions
2010-12-03 10:55:00 1960
原创 (NOT CLEAR)Question 47: Which of the following statements correctly describe functions of the endl manipulator for the ostream o
<br /> A. It only flushes the standard output stream.<br /> B. It puts a newline character into the standard output stream and flushes the standard output stream.<br /> C. It puts an end-of-output character into the standard output stream.<br />
2010-12-03 10:55:00 1714
原创 (NOT CLEAR)Question 45: Which of the following options are returned by the typeid operator in C++?
<br /> A. A reference to a const std::type_info object<br /> B. A const std::type_info object<br /> C. A const reference to a const std::type_info object<br /> D. A reference to a std::type_info object<br /> E. A const reference to a std::ty
2010-12-03 10:52:00 1368
原创 (NOT CLEAR)Question 44: What member function of std::fstream could a C++ developer invoke in order to change the target output f
<br /> A. setbuffer<br /> B. setfilebuf<br /> C. rdbuf<br /> D. filebuf<br /> E. streambuf
2010-12-03 10:48:00 1561
原创 Question 43: Which of the following define valid string constants in C++?
<br /> A. #define MESSAGE "Whoever said that you could run this program" &<br />"must not have known what you'd do."<br /> B. #define GREETING = "Hello!"<br /> C. #define MESSAGE = "This is a long message, but I know you have"<br />#define MESSAGE
2010-12-03 10:47:00 1416
原创 Question 42: A C++ developer wants to explicitly specialize the template function below for the char * type:
template void fn(T a){...}Which of the following methods can the developer use to carry out this specialization? A. void fn(char* a){...} B. template void fn(char* a){...} C. void fn(T a){...} D. template void fn(char* a){...} E. template
2010-12-03 10:45:00 1470
原创 Question 41: Which of the following operators must be overloaded by function objects in the Standard Template Library?
<br /> A. operator +()<br /> B. operator ++()<br /> C. operator ==()<br /> D. operator ()()<br /> E. operator []()<br /><br /><br /><br />D function object
2010-12-03 10:42:00 1840
原创 (NOT CLEAR)Question 40: In the declaration below, p is a pointer to an array of 5 int pointers:
<br />int *(*p)[5];<br /> <br />Which of the following statements can be used to allocate memory for the first dimension in order to make p an array of 3 arrays of 5 pointers to type int?<br /> <br /> A. p = new int [3][5]*;<br /> B. p = new int (*)[
2010-12-03 10:41:00 1810
原创 Question 39: Which of the following are possible outputs of the C++ code below?
<br />#include <iostream>class TestPrint{public: void Print() { std::cout << "TestPrint" << std::endl; } void Print() const { std::cout << "const TestPrint" << std::endl; } void Print() vol
2010-12-03 10:38:00 1265
原创 Question 38: What is the output of the program below in C++?
<br />#include <iostream>using namespace std;class Object{public: Object() {} void Print() const { cout << "const" << endl; } void Print() { cout << "mutable" << endl; }};void pri
2010-12-03 10:33:00 1571
原创 Question 37: In C++, which of the following statements regarding the code below are valid?
<br />#include <iostream>class Outer{ int m_o; public: class Inner { int m_i; public: Inner(){} Inner(Outer m_outer, int x) { m_outer.m_o = x; } }; Outer(int y)
2010-12-03 10:25:00 1945
原创 Question 36: Which of the following statements regarding functions' default arguments in C++ are correct?
<br /> A. Default arguments cannot be of pointer type.<br /> B. Default arguments cannot be of a user-defined type.<br /> C. Default arguments can never precede non-default arguments.<br /> D. Default arguments exist in the global heap and not
2010-12-03 10:15:00 1825
原创 Question 35: Protected, or private, inheritance, as opposed to public inheritance, models which type of relationship in C++?
<br /> A. Has-a<br /> B. Is-implemented-in-terms-of<br /> C. Was-a<br /> D. Can-only-have-one-of<br /> E. Shares-a-relationship-with<br /><br /><br />B
2010-12-03 10:09:00 1436
原创 Question 34: Under which of the following conditions will a C++ developer use polymorphism?
<br /> A. When there is a looping construct that uses a continue more than once<br /> B. When there is a need for the code written today to call code written tomorrow<br /> C. When there is a need to check for the type of an object to determine wh
2010-12-03 10:08:00 2308
原创 Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
<br /> A. new SomeClass(pmem);<br /> B. new(pmem) SomeClass;<br /> C. new pmem SomeClass;<br /> D. new SomeClass pmem;<br /> E. new (pmem, SomeClass);<br /><br /><br />B
2010-12-03 10:01:00 1342
原创 (NOT CLEAR)Question 32: In C++, there is a standard global object wcin of type wistream. What is the template type of the wistr
<br /> A. std::basic_istream<wchar_t, char_traits<wchar_t> ><br /> B. std::basic_istream<char, char_traits<char> ><br /> C. std::istream<char, char_traits<char> ><br /> D. std::istream<wchar_t, char_traits<wchar_t> ><br /> E. std::wistream<c
2010-12-03 09:58:00 1680
原创 Question 31: A C++ developer wants to handle a static_cast() operation for the class String shown below. Which of the following
<br />class String {<br />public:<br /> //...<br /> //declaration goes here<br />};<br /> <br /> A. char* operator();<br /> B. char* operator char*();<br /> C. String operator char*();<br /> D. operator char*();<br /> E. char* operator S
2010-12-03 09:57:00 1843
转载 关于构造函数的调用顺序
<br />在说构造函数之前我们得先弄明白几个问题,首先是什么是类的构造函数,什么是类的成员对象,什么是基类,然后我们再来说构造函数的调用顺序。<br /> 1、 类的构造函数<br /> 构造函数的功能主要用于在类的对象创建时定义初始化的状态。它没有返回值,也不能用void来修饰,这就保证了它不仅什么也不用自动返回,而且根本不能有任何选择。构造函数不能被直接调用,必须通过new运算符在创建对象时才会自动调用,一般方法在程序执行到它的时候被调用。一个类可以有多个构造函数,根据其参数个数的不同或参数类型的
2010-12-02 18:14:00 949
原创 Question 30: What is the order of destructor calls for an object of class Y inherited from class X that has an object of class A
<br />A. ~Y() ~X() ~A()<br />B. ~X() ~A() ~Y()<br />C. ~Y() ~A() ~X()<br />D. ~A() ~Y() ~X()<br />E. ~X() ~Y() ~A()<br /><br /><br />C<br /><br /><br />构造函数调用次序: 基类构造函数(按申明顺序) 派生类成员变量构造函数 派生类自己构造函数<br /><br />析构函数调用次序: 与基类完全相反
2010-12-02 18:13:00 1408
原创 Question 29: The C++ code below generates a compiler error. Which of the following solutions can be used to correctly access the
<br />class SomeClass{ public: int data; protected: class Nest { public: int nested; }; public: static Nest* createNest(){return new Nest;}};void use_someclass()
2010-12-02 17:59:00 2642 1
转载 函数对象 function object
函数对象(也称“算符”)是重载了“()”操作符的普通类对象.尽管函数指针被广泛用于实现函数回调,但C++还提供了一个重要的实现回调函数的方法,那就是函数对象。函数对象(也称“算符”)是重载了“()”操作符的普通类对象。因此从语法上讲,函数对象与普通的函数行为类似。 用函数对象代替函数指针有几个优点,首先,因为对象可以在内部修改而不用改动外部接口,因此设计更灵活,更富有弹性。函数对象也具备有存储先前调用结果的数据成员。在使用普通函数时需要将先前调用的结果存储在全程或者本地静态变量中,但是全程或者本地静态变量有
2010-12-02 11:16:00 874
原创 new delete全解析
<br />1. new operator (new操作符)<br /> 1) 分配空间 调用operator new<br /> 2) 调用构造函数<br /> <br /> Sample:<br /> string * ps = new string("Memory");<br /> <br />2. operator new<br /> 完全申明<br /> void* operator new(size_t size);<br /> 只负
2010-12-01 17:24:00 953
原创 Question 28: Which of the following statements accurately describe the new[] operator in C++?
A. It calls the class default constructor for each element of the array. B. It calls the class copy constructor for each element of the array. C. It calls operator new[](size_t). D. It calls operator new[](size_t, int). E. It stores the num
2010-12-01 16:11:00 2109
原创 Question 27: Which of the following reasons describe why a destructor cannot throw an exception in C++?
A. It can invoke the terminate() handler. B. Since the object is being destroyed, it is illogical to throw an exception then. C. The C++ language does not permit it; a throw statement in a destructor will be caught as an error by the compiler.
2010-12-01 14:53:00 2178
原创 Question 26: Which of the following options describe the expected overhead for a class that has 5 virtual functions?
<br />A. Every object of the class holds the address of a structure holding the addresses of the 5 virtual functions.<br /> B. Every object of the class holds the addresses of the 5 virtual functions.<br /> C. Every object of the class holds the addr
2010-12-01 14:50:00 3423 1
原创 Question 25: For the code snippet below, which of the following statements provide the correct order of constructor calls when o
<br />class Base{ public: Base(){cout << "In Base Ctor/n";} class Nest { public: Nest(){cout << "In Nest Ctor/n"; } }; };class Derive : public Base{ public: Derive(){cout << "In De
2010-12-01 14:40:00 2042
原创 Question 24: Which of the following template declarations provide the correct syntax to write a template class
<br />template <class T> class Derived;<br /> <br />that has to inherit from another template class<br /> <br />template <class T> class Base;?<br /> <br /> A. template <class T, class Q> class Derived<Q> : public Base<T><br /> where Q can be used as
2010-12-01 14:31:00 1424
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人