C++操作符重载手册 C++ Operator Overloading

C++核心语法与函数编程速查手册:从基础到现代特性实战指南 在C++编程中,掌握核心语法元素和函数机制是构建高效、安全软件的基础。理解变量、指针、引用等基本概念及其内存管理原理,是避免悬垂指针和内存泄漏等常见错误的关键。从函数声明、参数传递到返回值优化,这些技术直接影响代码的性能和可维护性。随着现代C++的发展,移动语义、lambda表达式和完美转发等特性极大地提升了代码的表达力和运行效率。本手册聚焦于C++基本语法和函数使用,通过直白的解释和代表性代码片段,为开发者提供一份即查即用的实战指南,帮助解决日常开发中关于constexpr、std::move等热词相关的 阅读详情
一、重载规则

I.可以重载的操作符

+ - * / %
^ & | ~ !
= > < += -=
*= /= %= ^= &=
|= >> << >>= <<=
== != >= <= &&
|| ++ -- ->* ,
-> [] () operator new operator new[]
operator delete operator delete []

II.不能重载的操作符

:: . .* ? :
sizeof typeid new delete
static_cast dynamic_cast const_cast reinterpret_cast

III.基本规则

1.一元操作符可以是不带参数的成员函数[1]或带一个参数的非成员函数[1]。
2.二元操作符可以是带一个参数的成员函数[1]或带两个参数的非成员函数[1]。
3.operator=、operator[]、operator()、operator->只能定义为成员函数[1]。
4.operator->的返回值必须是一个指针或能使用->的对象。
5.重载 operator++ 和 operator-- 时带一个 int 参数表示后缀,不带参数表示前缀。
6.除 operator new 和 operator delete 外,重载的操作符参数中至少要有一个非内建数据类型。
7.x@y 搜索范围为:x 成员函数--> 全局函数/X所在名字空间中的函数/Y所在名字空间中的函数/X的友元函
    数/Y的友元函数。
8.重载的的操作符应尽量模拟操作符对内建类型的行为。

二、使用重载


I.操作符重载的一些建议

     1.只将会改变第一个参数的值的操作符(如: +=)定义为成员函数,而将返回一个新对象的操作符(如: +)定义为非成员函数(并使用 += 来实现)。
     2.只有非成员函数才能在左参数上实施性别转换,如果需要进行转换则应将操作符定义为非成员函数。
     3.对一元操作符, 为避免隐式转换最好将其重载为成员函数。
     4.对二元操作符, 为能在左操作数上能进行和右操作数一样的隐式转换, 最好将其重载为非成员函数。
     5.为了遵照使用习惯,operator>>、operator<< 应定义为非成员函数。
     6.重载 operator[] 之类的操作符, 应尽量提供 const 版本和非 const 版本。
     7.关于将操作符定义为成员或非成员可参考以下建议:
操作符 建 议
所有一元操作符   成员
= () [] ->   必须为成员
+= -= /= *= ^= &= != %= >>= <<=   成员
其它二元操作符   非成员

     8.如果默认操作符已经可以施用于你的型别上, 则应尽量避免重载此操作符. 如 operator, 、operator&(取地址) 等等.

II. 重载 operator new

     1.为什么要重载 operator new ?

[效率问题] 通常系统默认提供的分配器速度极慢, 而且分配小型对象时空间浪费严重.
[改变行为] 默认的分配器失败时会抛出异常, 或许你想改变这种行为.

     2. operator new 的行为

[区分三个不同的 new] 
     new 操作符(new 表达式, new operator, new expression): 通常我们调用 X * pX = new X 时使用的就是这个操作符, 它由语言内建, 不能重载, 不能改变其行为. 它包括分配内存的 operator new 和调用构造函数的 placement new.
     operator new :opeator new 是一个函数, void * operator new(size_t size) . 它分配指定大小的内存, 可以被重载, 可以添加额外的参数, 但第一个参数必须为 size_t. operator new 除了被 new operator 调用外也可以直接被调用: void * rawMem = operator new(sizeof(X)). 
     placement new : placement new 在一块指定的内存上使用构造函数, 包含头文件 <new> 之后也可以直接使用 placement new: X * pX = new (rawMem) X. [2]

     与 new operator 类似, 对于 delete operator, 也存在 operator delete: void operator delete(void *), 析构方法 pX->~X().

[operator new 的错误处理]
     默认的 operator new 在内存分配失败时将会抛出 std::bad_alloc 异常; nothrow new [3] 
(X * pX = new (nothrow) X) 在内存分配失败时将会返回 0 . 这种行为可以通过设置 new-handler 来改变. new-handler 是一个回调函数指针, typedef void(*new_handler)(void). 通过 set_new_handler(new_handler) 函数设置回调句柄后, 如果分配内存失败, operator new 将会不断的调用 new-handler 函数, 直到找到足够的内存为止. 为了避免死循环, new-handler 函数必须具备以下行为之一:
     (1).找到可用的内存.
     (2).安装其它的 new-handler 函数.
     (3).卸除 new-handler, 即 set_new_hanlder(0), 这样下此循环将恢复默认行为抛出异常或返回 0.
     (4).抛出异常.
     (5).保存错误日志, 退出程序.

     3.准备重载 operator new

     重载 operator new 时需要兼容默认的 operator new 错误处理方式. 另外, C++ Standard 规定当要求的内存为 0 byte 时也应该返回有效的内存地址. 所以 operator new 的重载实现应大致如下:
void * ... operator new(size_t size ... )
{
     if(size == 0)
         size = 1; 

     while(1)
     { 
         ... // allocate memery
         if(allocate sucessfull)
             return ... // return the pointer.

         new_handler curhandler = set_new_handler(0);
         set_new_handler(curhandler); // get current new handler

         if(curhandler == 0)
             (*curhandler)();
         else
             throw std::bad_alloc();
     }
}
     重载 operator delete 简单许多, 只需注意 C++ Standard 要求删除一个 NULL 是安全的即可.

    4.重载 operator new

     opeator new 的重载和其它操作符大不相同.首先, 即使你不重载, 默认的 operator new 也可施用于你的自定义型别上(operator, 也具有此特性), 而其它操作符如果不进行重载就无法使用. 其次, 其它重载其它操作符时参数个数都是固定的, 而 operator new 的参数个数是可以任意的, 只需要保证第一个参数为 size_t, 返回类型为 void * 即可, 而且其重载的参数类型也不必包含自定义类型. 更一般的说, operator new 的重载更像是一个函数的重载, 而不是一个操作符的重载. 

[★ 用不同的参数重载 operator new]

     通过使用不同的参数类型, 可以重载 operator new, 例如 :
void * operator new(size_t size, int x, int y, int z)
{
     ...
}

X * pX = new (1, 2, 3) X;
     你还可以为 operator new 的重载使用默认值, 其原则和普通函数重载一样, 只要不造成和已存在的形式发生冲突即可. 可能你已经想到了, 你甚至还可以在 operator new 中使用不定参数, 如果你真的需要的话.
void * operator new(size_t size, int x, int y = 0, int z = 0)
{
     ...
}

X * pX = new (10) X;
Y * pY = new (10, 10) Y;
Z * pZ = new (10, 10, 10) Z;

...
void * operator new(size_t size, ...)
...
     在全局空间中也可直接重载 void * operator new(size_t size) 函数, 这将改变所有默认的 new 操作符的行为, 不建议使用.

[★ 重载 class 专属的 operator new]

     为某个 class 重载 operator new 时必须定义为类的静态函数[4], 因为 operator new 会在类的对象被构建出来之前调用. 即是说调用 operator new 的时候还不存在 this 指针, 因此重载的 operator new 必须为静态的. 当然在类中重载 operator new 也可以添加额外的参数, 并可以使用默认值.另外, 和普通函数一样, operator new 也是可以继承的.
class X{
...
static void * operator new(size_t size); // ... (1)
static void * operator new(size_t size, int); // ... (2)
};

class Y : public X{
...
};

class Z : public X{
...
static void * operator new(size_t size); // ... (3)
};

X * pX1 = new X; // call (1)
X * pX2 = ::new X; // call default operator new
X * pX3 = new (0) X; // call (2)

Y * pY1 = new Y; // call (1)

Z * pZ1 = new Z; // call (3)
Z * pZ2 = ::new Z; // call default operator new
Z * pZ3 = X::new Z; // error, no way to call (1)
Z * pZ4 = new (0) Z; // error, no way to call (2)

     5.重载 operator delete

     如果你重载了一个 operator new, 记得一定要在相同的范围内重载 operator delete. 因为你分配出来的内存只有你自己才知道如何释放. 如果你忘记了, 编译器不会给你任何提示, 它将会使用默认的 operator delete 来释放内存. 这种忘记的代价是惨重的, 你得时刻在写下 operator new 的同时写下 operator delete. 
     如果在类中使用 operator delete, 也必须将其声明为静态函数. 因为调用 operator delete 时对象已经被析构掉了. operator delete 的重载可以有两种形式:
(1) void operator delete(void * mem)
(2) void operator delete(void * mem, size_t size)
     并且这两种形式的 operator delete 可以同时存在, 当调用 delete px 时, 如果 (1) 式存在的话将调用 (1) 式. 只有在 (1) 式不存在时才会调用 (2) 式. 对第 (2) 种形式的 operator delete, 如果用基类指针删除派生类对象, 而基类的析构函数没有虚拟的时候, size 的值可能是错误的.

三、重载参考

- const Carrot operator+(const Carrot& lhs, const Carrot& rhs)
{ 
     Carrot result = lhs;
     return result += rhs; 
} 

【注】1. 如果可能, 应考虑使用 operator+= 来实现 operator+ . 
          2. operator+ 不能返回引用, 应返回值类型.
          3. 为了阻止形如 (a + b) = c 的调用, 应将返回值声明为 const .

· const Carrot operator-(const Carrot& lhs, const Carrot& rhs)
- const Carrot operator*(const Carrot& lhs, const Carrot& rhs)
{ 
     ... 
} 

【注】1. operator* 还可以重载为提领操作符.

· const Carrot operator/(const Carrot& lhs, const Carrot& rhs)
· const Carrot operator%(const Carrot& lhs, const Carrot& rhs)
· const Carrot operator^(const Carrot& lhs, const Carrot& rhs)
- const Carrot operator&(const Carrot& lhs, const Carrot& rhs)
{ 
     ... 
} 

【注】1. operator& 还可以重载为取地址操作符.

· const Carrot operator|(const Carrot& lhs, const Carrot& rhs)
- const Carrot Carrot::operator-() const
{ 
     Carrot result = (*this);
     ... // chang the value to negative
     return result; 
} 

【注】1. 一元操作符, 取负.

· const Carrot Carrot::operator~() const
· bool Carrot::operator!() const
· bool operator>(const Carrot& lhs, const Carrot& rhs)
· bool operator<(const Carrot& lhs, const Carrot& rhs)
- Carrot& Carrot::operator=(const Carrot& rhs)
{ 
     if(this == &rhs)
         return *this; // may be (*this) == rhs if needs.

     Barley::operator=(rhs); // if Carrot derived from Barley 

     ... // assignments every memeber of Carrot. 

     return *this; 
} 

【注】1. 为了实现形如 x=y=z=0 的串联赋值操作, operator= 必须传回 *this 的非常量引用. 
          2. 在赋值时应注意检查是否为自赋值 ( a = a ).

- Carrot& Carrot::operator+=(const Carrot& rhs)
{ 
     ... 
     return *this; 
} 

【注】1. C++ 允许 (x += 1) = 0 形式的赋值操作, operator+= 必须传回 *this 的非常量引用.

· Carrot& Carrot::operator-=(const Carrot& rhs)
· Carrot& Carrot::operator*=(const Carrot& rhs)
· Carrot& Carrot::operator/=(const Carrot& rhs)
· Carrot& Carrot::operator%=(const Carrot& rhs)
· Carrot& Carrot::operator^=(const Carrot& rhs)
· Carrot& Carrot::operator&=(const Carrot& rhs)
· Carrot& Carrot::operator|=(const Carrot& rhs)
- istream& operator>>(istream& _IStr, Carrot& rhs)
{ 
     ... 
     return _IStr; 
} 

【注】1. 为了遵照使用习惯(cin>>x 而不是 x>>cin), 对流操作的 operator>> 应为非成员函数.

- ostream& operator<<(ostream& _OStr, const Carrot& rhs)
{ 
     ... 
     return _OStr; 
}

- const Carrot operator>>(const Carrot& lhs, int rhs)
{ 
     Carrot result = lhs;
     ... 
     return result; 
} 

【注】1. 移位操作的重载方式.

· const Carrot operator<<(const Carrot& lhs, int rhs)
- Carrot& Carrot::operator>>=(int rhs)
{ 
     ... 
     return *this; 
} 

【注】1. 移位操作.

· Carrot& Carrot::operator<<=(int rhs)
- bool operator==(const Carrot& lhs, const Carrot& rhs)
{ 
     ... 
}

· bool operator!=(const Carrot& lhs, const Carrot& rhs)
· bool operator>=(const Carrot& lhs, const Carrot& rhs)
· bool operator<=(const Carrot& lhs, const Carrot& rhs)
- bool operator&&(const Carrot& lhs, const Carrot& rhs) X
{ 
     ... 
} 

【注】1. 基于以下原因, 你应该避免重载 operator&& 和 operator|| :
               (1). && 和 || 是一个逻辑操作符, 只对 bool 型别有才有明确的语意. 
               (2). 重载的 operator&& 和 operator|| 无法模拟操作符默认的骤死式语义[5].
               (3). 你无法保证重载后操作符的参数求值次序(C++ Stand 保证了默认的 && 和 || 按从左到右求值).
          2.自定义型别可以考虑提供到 bool 的转型操作来支持此操作符.

· bool operator||(const Carrot& lhs, const Carrot& rhs) X 
- Carrot& Carrot::operator++()
{ 
     (*this) += 1; // or other implement
     return *this;
} 

【注】1. 前置操作(prefix): ++carrot

- const Carrot Carrot::operator++(int)
{ 
     Carrot oldValue = *this;
     ++(*this);
     return oldValue;
} 

【注】1. 后置操作(postfix): carrot++ 调用时编译器自动生成一个 0 作为参数.
          2. 为了禁止 carrot++++ 操作, 返回值应为 const.
          3. 从实现和参数可以看出,后置操作的效率远远低于前置操作, 所以如非必要尽量使用前置操作.
          4. 为了保证递增行为一致, 并便于维护后置操作最好使用前置操作来完成递增行为.

· Carrot& Carrot::operator--()
· const Carrot Carrot::operator--(int)
- const Carrot operator,(const Carrot& lhs, const Carrot& rhs) X
{ 
     ...
} 

【注】1. 基于以下原因, 你应该避免重载 operator, :
               (1). 即使没有重载, 默认,号操作符也可以施用于自定义型别上. 
               (2). C++ 保证 , 操作符的求值是从左到右的, 而重载后无法保证此行为.

- const PMFC Carrot::operator->*(ReturnType (T::*pmf)()) const
{ 
     ...
} 

【注】1. operator->* 重载比较少见, 有兴趣可参考Implementing operator->* for Smart Pointers.

- const Carrot* Carrot::operator&() const X
{ 
     ...
} 

【注】1. 你应该尽量避免重载取地址操作符.

- Coca& Carrot::operator*()
{ 
     ...
} 

【注】1. 重载提领操作符应提供 const 版和非 const 版.

· const Coca& Carrot::operator*() const
- Coca* Carrot::operator->()
{ 
     ...
} 

【注】1. 重载 operator-> 应提供 const 版和非 const 版.
          2. operator-> 的返回值必须为一个指针或可以应用 -> 操作的类型.
          3. 如果 operator-> 的返回的不是一个指针, C++ 会继续在返回类型上应用 -> 直到得到一个指针为止.

· const Coca* Carrot::operator->() const
- Coca& Carrot::operator[](KeyType index)
{ 
     ...
} 

【注】1. operator[] 应提供 const 版本和非 const 版.
          2. KeyType 可以为任意类型, 但通常为 int 型.

· const Coca& Carrot::operator[](KeyType index) const 
- AnyType Carrot::operator()(...)
{ 
     ...
} 

【注】1. operator () 重载的是函数运算符, 改变表达式优先级的 () 是不能重载的.
          2. 重载了 operator() 的对象被成为仿函数, 使用方法和函数指针类似.

- static void* Carrot::operator new(size_t size, ...)
{
     if(size == 0)
         size = 1; 

     while(1)
     { 
         ... // allocate memery
         if(allocate sucessfull)
             return ... // return the pointer.

         new_handler curhandler = set_new_handler(0);
         set_new_handler(curhandler); // get current new handler

         if(curhandler == 0)
             (*curhandler)();
         else
             throw std::bad_alloc();
     }

【注】1. 参考重载 operator new.

· static void* Carrot::operator new[](size_t size, ...)
- static void Carrot::operator delete(void * memory, size_t size)
{ 
     if(memorty == NULL)
         return;
     ...
} 

【注】1. 参考重载 operator new.

· static void Carrot::operator delete[](void * momery, size_t size)

▲注:
1.本文中成员函数仅指非静态成员函数;非成员函数包括自由函数,静态成员函数,友元函数。←
2.这种调用方法使用了 operator new 的一种重载形式: void * operator new(size_t, void * buff) . 这种重载方式直接返回了传入的内存地址, 所以这种调用方式中 operator new 其实什么也没做, 于是这个 new operator 调用只使用了其中调用构造函数的部分, 相当于是在调用 placement new. 该重载在 VC7.1 中定义如下:
#define __PLACEMENT_NEW_INLINE
inline void *__cdecl operator new(size_t, void *_Where) _THROW0()
{ // construct array with placement at _Where
return (_Where);
}
←
3.和 placement new 一样, nothrow new 也是 operator new 的一种重载形式, nothrow 是 中声明的一个对象. 该重载在 VC7.1 中的声明为:
...
struct nothrow_t
     { // placement new tag type to suppress exceptions
     };

extern const nothrow_t nothrow; // constant for placement new tag
...
void *__cdecl operator new(size_t, const std::nothrow_t&)
     _THROW0();
←
4.在 VC7.1 中, 不将 operator new 声明为静态函数仍然可以通过编译, 但是如果在函数里面使用了非静态成员则会产生编译错误. 可见, 即使不将 operator new 声明为静态函数编译器也会将其作为静态函数来处理的. 使用是即使编译器默认把 operator new 当作静态函数也应该显式的将其声明为 static 函数. ←
5. 即对 && 操作符如果左操作数为 false, 则直接返回 false 而不会去检测右操作数; 对 || 操作符如果左操作数为 true, 则直接返回 true 而不会检测右操作数的值. 于是类似这样的代码可以是安全的:
     if( (p != NULL) && strlen(p) < 10 )... 
如果 p == NULL, strlen 就不会被调用. 但是重载的 operator&& 和 operator|| 是作为一个函数, 要在所有的参数求值完成后才会调用此操作符.←

▲参考资料:

1. Bjarne Stroustrup.The C++ Programming Language(Special Edition)
2. Scott Meyers.Effective C++(2nd Edition)
3. Andrei Alexandrescu.Modern C++ Design
4. Robert B.Murray.C++ Strategies and Tactics
5. Scott Meyers.More Effective C++
6. John Lakos.Large-Scale C++ Software Design
7. Scott Meyers.Implementing operator->* for Smart Pointers
程序设计二(面向对象)_实训6_关系运算符重载 同样的,虽然如此,鉴于关系运算符的参数形式,我们还是建议:如果需要重载关系运算符的话,最好使用普通函数进行重载。算术运算符既可以以成员函数形式重载,也可以以普通函数形式重载。算术运算符既可以以成员函数形式重载,也可以以普通函数形式重载。需要注意的是,二者取其一。即如果以成员函数形式重载了算术运算符,就不要再以普通函数重载(相同参数的)。即如果以成员函数形式重载了算术运算符,就不要再以普通函数重载(相同参数的)。即如果以成员函数形式重载了算术运算符,就不要再以普通函数重载(相同参数的)。 阅读详情

相关推荐

C++ primer 14章习题答案

14.1节 练习14.1 相同点:对于优先级和结合型以及操作数的数目都不变。 不同点:重载操作符必须具有至少一个class或枚举类型的操作数。 14.2

digitalkee的博客 703

C++操作符重载,有点晕。。。

一、重载规则 I.可以重载的操作符 +-*/%^&|~!=>>>==*,->[]()operator newoperator new[]operator deleteoperator delete [] II.不能重载的操作符 ::..*? :sizeoftypeidnewdeletestatic_castdynamic_castconst_castreinterpret_cast III.基本规则1.一元操作符可以是不带参数的成员函数[1]或带一个参数的非成员函数[1]。2.二元操作符可以是带一个参数的成

随缘,随心 868

基于STM32的最简易蓝牙遥控小车

基于STM32的蓝牙遥控小车,基于HAL库编写代码,具有移动功能,读者可自行添加其他扩展功能。按下状态的对应关系:2前进,4左转,5停止,6右转,8后退,松开状态都是5,都选择16进制。首先按照小车的图纸把小车的机械部分装好。做这个小车的成本在200元左右,包括焊接设备,杜邦线等。

weixin_43469377的博客 1万+

int operator[](int pos)const;与int& operator[](int pos);的区别

int operator[](int pos)const;与int& operator[](int pos);的区别反回类型为引用的下标运算符一般在使用时用作左值,比如c[1]=1;在这种情况下,必须要求c[1]的结果是一个可以赋值的左值,因此int& operator[](int pos);这个函数的版本主要是为左值设定的,反回对变量的引用就可以作为左值。int operator[](int p

weixin_37178531的博客 2863

电磁炉线圈加热仿真全流程:用Ansys Maxwell分析涡流效应与热损耗

本文详细介绍了使用Ansys Maxwell进行电磁炉线圈加热仿真的全流程。通过构建2D轴对称涡流场模型,深入分析了高频下的涡流效应、趋肤效应与热损耗分布,为优化线圈设计、提升加热效率与均匀性提供了量化依据和实用指南。

sss66的博客 979

Python爬虫实战:股票历史数据抓取与量化回测全流程详解

我们以**东方财富网(eastmoney.com)和雪球网(xueqiu.com)**为主要目标,演示如何爬取A股历史行情。最常用的HTTP请求库,简单易用,适合静态页面数据请求。本文详细介绍了从股票历史数据抓取到简单回测的完整流程,代码示例基于最新技术,结构清晰,适合初学者和进阶者学习量化投资实战。希望对你入门Python爬虫和量化交易有所帮助!

2201_76125261的博客 1783

C指针解析

1.指针到底是什么? 指针(pointer)到底是什么,弄清楚这个问题很重要,这是我们所讨论的话题的源头,而在阐述指针是什么之前,我们需要先来看一下变量的概念。 我们知道,计算机的内存(primary storage)被划分为多个存储单元,这些存储单元可以以单个或者顺序相连组成一个更大单元的方式被使用。每一个单独的存储单元都是一个字节(byte),它通常由8个位(bit)组成,每一个位可以

逍遥飞鹤的专栏 1650

华为OD机试 - 敌情监控 - 滑动窗口(Java 双机位C卷 200分)

说明 第一行第一个正整数5,表示敌人有5个营地,第二个正整数2,表示连续营地 的数量是2, 第3个正整数3, 表示后续会有3条查询的命令;接下来一行有 5个正整数,分别表示每个营地里开始的人的数,第1个营地开始有1个人,第 2个营地开始有2个人,第3个营地开始有2个人,第4个营地开始有3个人,第 5 个营地开始有3个人;第3条命令:Query 2 4 表示查询第2到第4个营地的总人数,结果:2+8=10 第一个输出是: 10,注意这里第3个营地人数在第2条命令中增加了6。第一行3个正整数:N K L。

学Java,找哪吒 766

C++中const用于函数重载

C++中const用于函数重载 常成员函数和非常成员函数之间的重载 首先先回忆一下常成员函数 声明:<类型标志符>函数名(参数表)const; 说明: (1)const是函数类型的一部分,在实现部分也要带该关键字。 (2)const关键字可以用于对重载函数的区分。 (3)常成员函数不能更新类的成员变量,也不能调用该类中没有用const修饰的成员函数,只能调用常成员函数。 ...

weixin_30292843的博客 363

8-bit量化在YOLOv11中的部署实践-(使用PTQ、QAT量化技术实现模型体积压缩和推理加速)

本文系统介绍了YOLOv11模型量化技术,主要包括三个核心部分:首先阐述模型量化的基础理论,包括量化分类、数学原理和性能影响;其次分析YOLOv11架构特点及其量化敏感层;最后详细讲解训练后量化(PTQ)的具体实现流程。研究表明,8-bit量化可使YOLOv11模型大小缩减至1/4,推理速度提升1.7-2.3倍,精度损失控制在1%以内。文章特别强调了校准算法选择(如最小最大值法和KL散度法)对量化效果的关键影响,并提供了完整的代码实现示例,为YOLOv11的高效部署提供实用指导。

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 1631

学Simulink--复杂路况场景实例:基于Simulink的智能车辆高速公路行驶仿真(超车与车道变换)

本文详细介绍了基于Simulink的智能车辆高速公路超车仿真系统设计方法。系统包含多车道交通流建模、三层超车决策逻辑(触发条件、可行性检查、执行许可)、五次多项式变道轨迹规划以及横向纵向协同控制等核心模块。通过设定三车道场景(自车初始速度100km/h,前方慢车72km/h),演示了如何实现安全高效的左变道超车全过程(5秒完成3.75m横向移动),并验证了变道平滑性(最大横向加速度1.8m/s²)和安全距离(最小TTC 5.2s)。文章还强调了传感器融合的必要性,并提供了从交通流生成到控制器设计的完整Sim

xiaoheshang_123的博客 400

operator[]()的作用以及使用时可能出现的问题

在上述示例中,`MyClass`类重载了`operator[]()`函数,允许通过索引访问类中的整数数组元素。在`main()`函数中,创建了一个`MyClass`对象`myClass`,并使用`operator[]`进行索引访问,将值存储在数组中。总的来说,`operator[]()`是一个强大的工具,用于提供类对象的数组风格访问。在定义`operator[]()`时,需要仔细考虑索引检查、返回类型、可变性、异常处理和重载等问题,以确保其行为符合预期并具有良好的可读性和可维护性。

Kegi_的博客 800

Open3D 使用RANSAC分割平面【2026最新版】

使用RANSAC从点云中分割平面,用segement_plane函数。博客长期更新,本文最新更新时间为:2026年2月28日。

点云侠的博客 37万+

C++函数重载:从语法糖到重载决议的深度解析与实战指南

函数重载是C++中实现静态多态的核心机制,它允许在同一作用域内定义多个同名函数,通过参数列表的差异来区分。其底层原理依赖于编译器的名字修饰技术,将参数类型信息编码到函数名中,从而在链接阶段实现唯一标识。这一特性极大地提升了代码的可读性和接口的简洁性,是构建类型安全、表达力强的API的重要基础。在工程实践中,函数重载与重载决议机制紧密相关,编译器需要根据实参类型从候选函数集中选择最佳匹配,这涉及到精确匹配、类型提升和标准转换等复杂规则。理解这些规则对于避免模糊调用错误、设计清晰的类接口至关重要。特别是在现代C

weixin_34357436的博客 428

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

C++ operator关键字(重载运算符)

operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名。 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算符的使用方法与其原来一致,另一方面扩展其功能只能通过函数的方式(c++中,“功能”都是由函数实现的)。 1.声明重载操作符的方式: ①.实现为类成员函数 重载的操作符在类体中被声明,声明方式如同普通成员函数一样,只不过他的名字包含关键字operator,以及紧跟其后的一个c++预定义的操作符。 如

是鲤鱼啊 1326

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

C++编程思想 第1卷 第12章 运算符重载 重载赋值符 operator=行为 类中指针

简单地拷贝一个指针意味着以相同的存储单元的对象而结束 当做一个赋值运算或一个拷贝构造函数时,最简单的方法是拷贝这个指针所涉及的 一切,这非常直接   //: C12:CopyingWithPointers.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bru...

eyetired的博客 426

Six this指针 重载运算符operator

1:隐藏的this指针: ①对于类的非静态成员,每一个对象都有自己的一份复制,即每个对象都有自己的数据成员。 ②成员函数是每个对象共享的,调用共享的成员函数通过类中隐藏的this指针来找到自己的数据成员。 ③类型:类(类型)指针类型 ④在const方法中,为当前类类型的const指针类型 ⑤用户可以使用this指针访问数据成员。 ...

weixin_64912037的博客 178

操作符重载手册(经典收藏)

一、重载规则I.可以重载的操作符 + - * / % ^ & | ~

侍鱼的专栏 1190

解析C++中的new、operator new、operator new[]和delete、operator delete、operator delete[](下)

注:以下测试均在VS2015下进行,其他编译器可能会有轻微差别。 继续整理第二部分的内容。 二、operator new/delete,以及其对应的数组版本new[]/delete[]。 operator new优先调用全局重载的,所以::operator new(sizeof(COperatorTest))和operator new(sizeof(COper

beyongwang的博客 2121

通过重载避免隐式类型转换

以下是一段代码,如果没有什么不寻常的原因,实在看不出什么东西:  class UPInt {                                 // unlimited precision  public:                                       // integers 类    UPInt();    UPInt(int value); 

wangdamingll的博客 412

isalnum ;isspace ;编译问题;unresolved overloaded function type

一般遇到这种问题主要参考文章为 https://stackoverflow.com/questions/26574286/ptr-funisalnum-wont-compile-in-g 对于isspace有诸多的一行函数方法,但是并不实用建议使用命名空间限定使用范围 std::not1(std::ptr_fun(::isspace)) std::not1(std::ptr_fun(...

sjgenius的博客 4583

c++ operator(重载操作符)

operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator整体上视为一个函数名。      这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算符的使用方法与其原来一致,另一方面扩展其功能只能通过函数的方式(c++中,“功能”都是由函数实现的)。    一、为什么使用操作符重载? 对于系统的所有操作符,一般情况下,只支持基本

tutb12345的专栏 2729

编写高质量代码——区分new的三种形态

■new operator ■operator new ■placement new

见证自己的成长之路~ 1045
下一篇: 推荐系统五大问题
wildgoose
博客等级 码龄15年 5粉丝 2原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值