Skip to content

败犬日报 2024-10-31

1. windows cpp 获取当前时间,精确到秒,性能越高越好

调 win api https://learn.microsoft.com/zh-cn/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime

2. 面试问到了虚基类(虚继承)

虚继承写法是 class Derived: virtual public Base

用于菱形继承,继承的多个 Base 最终只有一个 Base 实例。

如果 Base 是有状态的,虚继承就是必要的。比如 cocos2d 里的很多对象都继承自一个引用计数类,多继承就会导致引用计数模块重复,虚继承就可以解决这个问题。

3. 模板类的实例化在成员函数定义前

cpp
template <typename T>
struct X {
    void foo();
};

template struct X<int>;

template <typename T>
void X<T>::foo() {}

上面代码 gcc 会导出 X<int>::foo() 符号,clang 不会。

cpp
template <typename T>
struct X {
    void foo();
};

template <typename T>
void X<T>::foo() {}

template struct X<int>;

这样换个顺序就都会导出了。(并且这才是正确的写法)

似乎 clang 才是正确的 https://eel.is/c++draft/temp.explicit#10