c++ trap compilation
发表于|更新于
|总字数:98|阅读时长:1分钟
收集一下奇奇怪怪的玩意。。。。
虚函数的默认参数
#include <iostream>
using namespace std;
class Base {
public:
virtual void show(int x = 10) = 0;
virtual ~Base() { cout << "~Base\n"; }
};
class Derive : public Base {
public:
void show(int x = 20) override {
cout << "x = " << x << endl;
}
~Derive() override { cout << "~Derive\n"; }
};
int main(int argc, char* argv[]) {
Base* bb = new Derive();
bb->show();
delete(bb);
return 0;
}
相关推荐

2025-08-13
CRTP
CRTPCRTP = Curiously Recurring Template Pattern。核心形式是:让派生类把自己作为模板参数传给基类 template<typename Derived> struct Base { /* 使用 static_cast<Derived*>(this) 调用派生接口 */ }; struct Derived : Base<Derived> { /* ... */ }; 这是 静态多态(compile-time polymorphism) 的一种实现:基类在编译期就能调用派生类的方法,达成“没有虚函数表、零运行时开销”的多态。 工作原理(Why / How)基类通过 static_cast<Derived*>(this) 或 static_cast<const Derived*>(this) 把自己转回派生类型,然后直接调用派生实现。因为在最终对象内确实包含派生子对象,所以在合法用法下这是安全的(前提:对象确实是 Derived 的实例)...

2023-06-26
cpp中的rvo
c++11 以后支持了nrvo(命名的rvo),先来一张图 直接贴代码 #include <iostream> #include <string> #include <vector> using namespace std; class BigObject { public: BigObject() { cout << "Constructor\n"; } ~BigObject() { cout << "Destructor\n"; } BigObject(BigObject const&) { cout << "Copy Constructor\n"; } BigObject& operator=(BigObject const&) { cout << "Assignment Operator\n"; return *this; ...

2023-06-21
cpp中的并发
cpp11之后,cpp本身对并发的支持大大增强,这里记录一下 1 std::thread classcpp的thread类型,创建即运行,不像java那样需要start,thread表示一个系统资源,一个系统线程 构造函数 function construct constructor 1 c++thread() noexcept; constructor 2 thread( thread&& other ) noexcept; constructor 3 template<class F, class... Args>explicit thread(F&& f, Args&&... args); copy constructor thread(const thread&) = delete; assign copy operator thread& operator=(const thread&) = delete; assign move operator thre...

2023-06-27
boost中好用的算法
基于 boost 1.82… timer 计时器过去老的 boost/timer.hpp 已经废弃了,目前推荐使用的是 boost/timer/timer.hpp, 主要包括下面了2个类 class detail boost::timer::cpu_timer 计时器 boost::timer::auto_cpu_timer 计时器,基于cpu_timer实现,在析构的时候输出耗时 struct cpu_times { nanosecond_type wall; // 挂钟时间 nanosecond_type user; // 用户时间 nanosecond_type system; // 系统时间 void clear() {wall = user = system = 0LL; } }; 默认的输出格式为下: “%w s wall, %u s user + %s s system = %t s CPU (%p%)\n” format meanin...

2023-06-26
cpp字符串
c++ string 的allocate和cowstring 可以说是c++ stl 中最常用的容器了,首先弄明白 cpp 中的string 到底是在哪里存储的,下面的代码我们重载了全局的new和delete #include <string> #include <iostream> #include <cstdlib> #include <vector> using namespace std; // 重载全局 new void* operator new(std::size_t size) { cout << "[Allocating " << size << " bytes]\n"; return malloc(size); } // 重载全局的 delete // void operator delete(void* ptr) noexcept { // cout << "[Deallocating]\n"; // return fre...

2025-08-02
cpp特殊函数生成规则
在C++中,特殊成员函数(special member functions)是指由编译器自动生成(或隐式声明)的类成员函数,包括默认构造函数、析构函数、拷贝构造函数、拷贝赋值运算符、移动构造函数和移动赋值运算符。这些函数在特定情况下会由编译器自动提供,但其生成规则受到类定义和用户提供的声明的影响。以下是C++中特殊成员函数的生成规则的详细说明,基于C++11及以后的标准。 1. 特殊成员函数的种类C++中的特殊成员函数包括以下六种: 默认构造函数 (T::T();) 无参数的构造函数,用于创建对象。 析构函数 (T::~T();) 用于清理对象资源。 拷贝构造函数 (T::T(const T&);) 用于通过复制已有对象来构造新对象。 拷贝赋值运算符 (T& operator=(const T&);) 用于将一个对象的内容复制到另一个已有对象。 移动构造函数 (C++11 引入, T::T(T&&);) 用于通过移动已有对象的资源来构造新对象。 移动赋值运算符 (C++11 引入, T& operator=(...