nonmember operator
- 25. Which operator can be implemented as a nonmember operator?
- A. =
- B. ()
- C. []
- D. +
- 28. If the class name is X, what't the type of its "this" pointer (in a nonstatic, non-const member function) ?
- B. X* const
- 29. Which is the most prefered way of throwing and handling exceptions?
- A. Throw by value and catch by reference
- 31. The conditional compilation
- C. It is compiling a program based on a condition
- 39. Seek time is
- B. Time taken by read/write head mechanism to position itself over an appropriate cylinder
- 40. Latency time is
- C. Time taken by the appropriate sector to come under read/write head
字符串中找子串
(s.find("abcdefg")==string::npos)
如果存在包含关系find函数返回的就是主串与子串相匹配的下标,
如果不存在包含关系就返回 npos(一个常数,表示不存在)
类中的protected成员
Driver类是Bus类中的子类,被保护;代码中的driver不是类中的有血缘的成员,自然不能定义为不是Bus::Driver相关指针。
#include <iostream>
using namespace std;
class Bus {
protected:
class Driver; // forward declarations
public:
static Driver* createDriver() {
return new Driver;
}
// friend void useBus();
private:
int seats;
protected:
class Driver {
public:
string name;
};
};
class DeriveDriver : public Bus {};
void useBus()
{
Bus::Driver* driver = Bus::createDriver(); // 'Driver' is a protected member of 'Bus'
// DeriveDriver::Driver* driver = Bus::createDriver(); // 'Driver' is a protected member of 'Bus'
driver->name = "DRIVER";
}
int main(int argc, char* argv[])
{
return 0;
}
模板中的变量
代码可正常编译,但其中的int duration有存在的必要么?
#include <iostream>
using namespace std;
template <typename T, int duration>
double power (double amplitude, int frequency = 1000) { return amplitude * frequency; }
int main(int argc, char* argv[])
{
double output;
output = power<int, 100>(3.0, 'a');
// output = power<500>(3.0); // No matching function for call to 'power'
output = power<int, 500>(3.0, 100);
// output = power<int>(3.0, 100); // No matching function for call to 'power'
// output = power<int, char>(3.0); // No matching function for call to 'power'
output = power<int, 500>(3.0);
cout << output << endl;
return 0;
}
关键字noexcept
(1) 一个操作或者函数不可能抛出任何异常。
void swap(Type& x, Type& y) noexcept //C++11
{
x.swap(y);
}
(2) 如果操作x.swap(y)不发生异常,那么函数swap(Type& x, Type& y)一定不发生异常。
void swap(Type& x, Type& y) noexcept(noexcept(x.swap(y))) //C++11
{
x.swap(y);
}
拷贝构造函数 copy constructor
自定义类具有“赋值/拷贝”属性。写法参数如下:
//拷贝构造函数
CExample(const CExample & c)
{
a=c.a;
printf("copy constructor is called\n");
}
alignof() 和 alignas()
Q12. Which regarding the C++ 11 operators alignof() and alignas() are correct?
- A. alignof () is used to obtain the alignment of a specified variable or type.
- B. alignas () is used to specify custom alignment of variables and user-defined types.
- E. alignas () works on expressions. (整型常量表达式)
callable objects
C++语言有几种callable objects:函数、函数指针、lambda表达式、bind创建的对象以及重载了函数调用运算符()的类。
Ref: C++中可调用对象与function以及bind
Q14. When using the multi-threading library in the C++ standard library, which are NOT callable objects which may be run in a thread?
- A. A class with an overridden operator()
- B. A class member function
- C. A lambda function
- D. An executable file
- E. A regular function
运算符重载
[c++] Operator overloading【需要再仔细整理一遍】
iota 符号
iota( ++sequence.begin(), sequence.end(), 1 );
不同于c语言中的用到atoi()函数,是“字符串转为数字”。
这里是生成一个序列。
#include<iostream>
#include<numeric>
#include<vector>
using namespace std;
int main(void)
{
vector<int> v(10);
iota(v.begin(), v.end(), 2); // 最后一个参数是“起始数字”
vector<int>::iterator it = v.begin();
while(it != v.end())
{
cout<<*it++<<" ";
}
return 0;
}
其他类似的方法:
// 1 2 3 4 5 6 7 8 9 10 11 12
// iota( sequence.begin(), sequence.end(), 1 );
// 0 1 1 0 1 5 8 13 21 34 55 89
// move( sequence.begin(), sequence.begin() + 2, sequence.begin() + 3 );
// 0 1 1 1 1 1 1 1 1 1 1 1
// for_each( sequence.begin(), sequence.end(),
// []( int& elem ) { if ( elem > 1 ) elem = 1; } );
// 0 1 1 0 1 5 8 13 21 34 55 89
// copy_if( sequence.begin (), sequence.begin() + 2, sequence.begin() + 3,
// []( int elem ) { return elem <= 1; } );
"多态"类型
Q23. Which are types of polymorphism in C++?
- A. Bi-metric polymorphism
- B. Hybrid polymorphism
- C. Parametric polymorphism
- D. Post processing polymorphism
- E. Single polymorphism
- The Four Polymorphisms in C++ :
- http://www.catonmat.net/blog/cpp-polymorphism/【详见此链接】
- Subtype polymorphism is also known as runtime polymorphism.
- Parametric polymorphism is also known as compile-time polymorphism.
- Ad-hoc polymorphism is also known as overloading.
- Coercion is also known as (implicit or explicit) casting.
别名 a type alias
Q27. Which are valid ways to create a type alias?
- A. template<class T> using vec = std::vector<T>; vec<float> incomes;
- C. typedef int intAlias; intAlias count;
- E. template<typename T> using myGenric = T; myGenric<int> age;
A和E何时会用到呢?
处理任何异常
需要参数位置写“省略号”。
try {
// 保护代码
} catch(...) {
// 能处理任何异常的代码
}
异常的执行顺序
对于try..catch捕获异常的形式来说,对于异常的捕获,可以有多个catch。对于try里面发生的异常,他会根据发生的异常和catch里面的进行匹配(怎么匹配,按照catch块从上往下匹配),当它匹配某一个catch块的时候,他就直接进入到这个catch块里面去了,后面在再有catch块的话,它不做任何处理,直接跳过去,全部忽略掉。
numeric_limits
在C/C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息。
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << std::endl;
std::cout << "Minimum value for float: " << std::numeric_limits<float>::min() << std::endl;
继承与函数 overriding
- Q34. Which are true about the declarations representing the C++ class hierachy below?
- A. Level5::func2(int) will override any functionality defined in Level2::func2(int).
- B. Level1::func1() functionality has been removed from Level2 but is still available to subsequent levels.
- C. Level5::func4(char) will produce a compiler error since it is not a true override.
- D. Level5::func3(int) will produce a compiler error since virtual functions implemented elsewhere cannot be deleted.
- E. Level5 results in a compiler error since it cannot inherit from Levei3.
涉及的知识点
Ref: C++中的默认函数与default和delete用法
有些时候我们希望限制默认函数的生成。典型的是禁止使用拷贝构造函数,以往的做法是将拷贝构造函数声明为private的并不提供实现,这样当拷贝构造对象时编译不能通过,C++11则使用delete关键字显式指示编译器不生成函数的默认版本。
Ref: C++11 之 override
函数后跟着const:
表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为隐含了对this指针的const引用);唯一的例外是对于mutable修饰的成员。
加了const的成员函数可以被非const对象和const对象调用,但不加const的成员函数只能被非const对象调用。
在声明需要重写的函数后,加关键字 override。
#include <iostream>
using namespace std;
class Level1 {
public:
void func1();
};
class Level2 : public Level1 {
public:
void func1() = delete;
int func2(char);
};
class Level3 final : public Level2 {
public:
virtual double func3(int);
};
class Level4 : public Level2 {
virtual double func4(int);
double func5(char a);
};
class Level5 : public Level3 { // Base 'Level3' is marked 'final'
public:
double func2(char) override; // Only virtual member functions can be marked 'override'
virtual double func3(int) = delete;
virtual double func4(char) override; // 'func4' marked 'override' but does not override any member functions
};
int main(int argc, char **argv)
{
Level3 obj;
obj.func1(); // Attempt to use a deleted function
return 0;
}
unordered associative containers
- Q35. Which correctly describe C++11 unordered associative containers, for example std::unordered_set, std::unordered_map, std::unordered multiset, std::unordered—multimap?
- A. They perform their lookup based on a hash function.
constexpr 关键字
修饰 “常量表达式“。因为是形式上的”常量“,所以可以放在了List<“常量表达式”>中。
template <int N>
class list
{ };
constexpr int sqr1(int arg)
{ return arg*arg; }
int sqr2(int arg)
{ return arg*arg; }
int main()
{
const int X = 2;
list<sqr1(X)> mylist1; //可以,因为sqr1时constexpr函数
list<sqr2(X)> mylist2; //不可以,因为sqr2不是constexpr函数
return 0;
}
constexpr修饰的变量,须在编译时进行初始化。
#include <iostream>
using namespace std;
class Vector {
public:
constexpr Vector( double i, double j, double k ) // Line 1
: ivec( i ), jvec( j ), kvec( k )
{}
constexpr Vector( const Vector &rhs ) // Line 2
: ivec( rhs.ivec ), jvec( rhs.jvec ), kvec( rhs.kvec )
{}
private:
double ivec, jvec, kvec; // Line 3
};
int main(int argc, char **argv)
{
Vector vec1( 2, 7, -1 ); // Line 4
// constexpr Vector vec2( vec1 ); // Line 5
Vector vec2( vec1 ); // Line 5
return 0;
}
线程的 “锁”
D. If a thread "locks" a resource, that resource is automatically unlocked when the thread terminates.
锁了资源,还是需要手动再放开。
static_assert 静态断言
Line 2 results in a compiler error if type std::vector<int> is not an empty class.
非空,编译时就能触发is_empty,就会直接引发“编译错误”,而非“运行时错误”。
使用static_assert,我们可以在编译期间发现更多的错误,用编译器来强制保证一些契约,并帮助我们改善编译信息的可读性,尤其是用于模板的时候。
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void processCollection(T t) {
// Static_assert failed "collection is empty"
static_assert(std::is_empty<T>::value, "collection is empty"); // Line 1
// ...
};
int main(int argc, char **argv)
{
std::vector<int> ages;
// ...
processCollection<std::vector<int>>(ages); // Line 2
return 0;
}
weak_ptr
weak_ptr是为了配合shared_ptr而引入的一种智能指针 (解决shared_ptr
循环引用的问题),因为它不具有普通指针的行为,没有重载operator*和->,它的最大作用在于协助shared_ptr工作,像旁观者那样观测资源的使用情况。
weak_ptr可以从一个shared_ptr或者另一个weak_ptr对象构造,获得资源的观测权。但weak_ptr没有共享资源,它的构造不会引起指针引用计数的增加。
- Q46. which about std::weak_ptr in C++ are correct?
- A. An instance can transfer ownership of its contained pointer if assigned to an object of std::unique_ptr.
- B. The object being referenced by the std::unique_ptr instance must be checked to see if it still exists before it can be accessed.
- C. Access to an instance's contained pointer is via operator->().
- D. Each instance does not increase the reference count
- E. An instance must be initialized by either an object of std::shared_ptr or another std::weak_ptr object.
A46. D ?
Goto: weak_ptr Class | Microsoft Docs
异常处理(细节)
- Q50. Which of the following statements correctly describe C++ exceptions?
- A, When an exception is thrown, and there is more than one applicable exception handler for the given exception type, control is transferred to the handler nearest to the point of the exception.
- B. When an exception is caught, it cannot be thrown further.
- C. A try block can be declared without a catch block.
- D. When an exception is thrown, all objects dynamically created on the heap are released and their destructors invoked.
- E. If there are multiple catch blocks, the exceptions they catch may not be from the same inheritance hierarchy.
- A50. C ?【不太确定】