operator++() 和operator++(int)


#include <iostream>
using namespace std;
class A{
public:
int a;
public:
A& operator++(){
cout<<"A& operator++()"<<endl;
a=a+1;
return *this;
}
A operator++(int){
cout<<"A operator++(int)"<<endl;
A b=*this;
a=a+1;
return b;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
A test;
test.a=12;
test++;
++test;
cout<<test.a<<endl;
system("pause");
}

运行结果:


A operator++(int)
A& operator++()
14

operator++() 和operator++(int),布布扣,bubuko.com

时间: 2024-10-12 08:09:50

operator++() 和operator++(int)的相关文章

error LNK2005: &quot;void * __cdecl operator new(unsigned int)&quot; ([email&#160;protected]@Z) 已经在 LIBCMTD.lib(new.obj) 中

在编译文章: 使用GetAdaptersAddresses函数获取物理MAC地址中的代码时,出现以下错误: 错误 1 error LNK2005: "void * __cdecl operator new(unsigned int)" ([email protected]@Z) 已经在 LIBCMTD.lib(new.obj) 中定义 F:\CcProjects\获取网卡物理地址MAC地址\获取网卡物理地址MAC地址\uafxcwd.lib(afxmem.obj) 获取网卡物理地址MA

uafxcwd.lib(afxmem.obj) : error LNK2005: &quot;void * __cdecl operator new(unsigned int)&quot;解决办法

如果在编译MFC程序的时候出现下列及类似的错误: 1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" ([email protected]@Z) 已经在 LIBCMTD.lib(new.obj) 中定义1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete

MFC程序出现uafxcwd.lib(afxmem.obj) : error LNK2005: &quot;void * __cdecl operator new(unsigned int)解决办法

在同一个地方摔倒两次之后,决定记录下来这个东西. 问题 1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" ([email protected]@Z) 已经在 LIBCMTD.lib(new.obj) 中定义1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator del

C++中的new/delete与operator new/operator delete

new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数. new operator(1)调用operator new分配足够的空间,并调用相关对象的构造函数(2)不可以被重载 operator new(1)只分配所要求的空间,不调用相关对象的构造函数.当无法满足所要求分配的空间时,则        ->如果有new_handler,则调用new_handler,否则        ->如果没要求不

C++ new/new operator、operator new、placement new初识

简要释义 1.operator new是内存分配函数(同malloc),C++在全局作用域(global scope)内提供了3份默认的operator new实现,并且用户可以重载operator new. 1 void* operator new(std::size_t) throw(std::bad_alloc);//normal new 2 void* operator new(std::size_t,const std::nothrow_t&) throw();//nothrow ne

C++ new operator, delete operator, operator new, operator delete, new placement

http://www.younfor.com/cpp-new-placement-new-operator-new.html http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html http://kelvinh.github.io/blog/2014/04/19/research-on-operator-new-and-delete/ new operator 就是C++中定义的关键字new,调用new而触发的行为,del

C++中的::operator new, ::operator delete

一般在使用new  和 delete的时候,做了两件事情,一是空间的配置( new 是分配,delete是回收),而是调用对象的析构函数 但是也有办法将这两个过程分开 那就是显式的调用::operator new, ::operator delete,它们只进行空间配置,并不调用对象的析构函数 具体的可以参看下面这个例子: // operator new[] example #include <iostream> // std::cout #include <new> // ::o

调用operator+=来定义operator+比其他方法更有效?

如题,下面给出operator+=和operator+的实现 1 Sales_data& 2 Sales_data::operator+=(const Sales_data &rhs) 3 { 4 units_sold += rhs.units_sold; 5 revenue += rhs.revenue; 6 return *this; 7 } 8 9 Sales_data 10 operator+(const Sales_data &lhs, const Sales_data

operator++()和operator++(int)的区别

// 前缀形式:增加然后取回值 UPInt& UPInt::operator++(){ *this += 1; // 增加 return *this; // 取回值} // postfix form: fetch and increment const UPInt UPInt::operator++(int){ UPInt oldValue = *this; // 取回值 ++(*this); // 增加 return oldValue; // 返回被取回的值}