operator= 这个函数在dll中

liuzu2016 2012-08-01 09:32:26
// The whole CXyz class is exported with all its methods and members.
//
class XYZAPI CXyz
{
public:
int Foo(int n);
};

// Only CXyz::Foo method is exported.
//
class CXyz
{
public:
XYZAPI int Foo(int n);
};



A careful reader must have already noticed that the Dependency Walker tool showes an additional exported member, that is the CXyz& CXyz::operator =(const CXyz&) assignment operator. What we see is our C++ money at work. According to the C++ Standard, every class has four special member functions:

•Default constructor
•Copy constructor
•Destructor
•Assignment operator (operator =)
If the author of a class does not declare and does not provide an implementation of these members, then the C++ compiler declares them, and generates an implicit default implementation. In the case of the CXyz class, the compiler decided that the default constructor, copy constructor, and the destructor are trivial enough, and optimized them out. However, the assignment operator survived optimization and got exported from a DLL.





以下这句话,怎么里理解?


However, the assignment operator survived optimization and got exported from a DLL.
...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuzu2016 2012-08-01
  • 打赏
  • 举报
回复
这样, 你有空的话,做个试验吧

我的确实dll

对于导出类

是不能够去loadlibrary的

只能隐式加载。




[Quote=引用 8 楼 的回复:]
楼主你可能误会原文作者的意思了,虽然他描述得的确不怎么清楚。
正如我6楼所说,你7楼使用的是静态导入lib的方式,不受原文作者观点的影响。
你用LoadLibrary+GetProcAddress就不行了,其中的原因就是作者的观点。
[/Quote]
Coder_Y_Jao 2012-08-01
  • 打赏
  • 举报
回复
楼主你可能误会原文作者的意思了,虽然他描述得的确不怎么清楚。
正如我6楼所说,你7楼使用的是静态导入lib的方式,不受原文作者观点的影响。
你用LoadLibrary+GetProcAddress就不行了,其中的原因就是作者的观点。
liuzu2016 2012-08-01
  • 打赏
  • 举报
回复


#pragma comment(lib,"..\\Debug\\mydll.lib")

int main()
{



Sample obj;


Sample obj2(obj);

obj2.fun();



return TRUE;

}




以下是dll代码:



class _declspec(dllexport) Sample
{
public:
void fun()
{
printf("%s","hello");
}
};




文章在扯淡, 误人子弟,该怎么用就怎么用, 拷贝构造没显示提供吧,照样使用之。

多谢楼下回帖。






[Quote=引用楼主 的回复:]
// The whole CXyz class is exported with all its methods and members.
//
class XYZAPI CXyz
{
public:
int Foo(int n);
};

// Only CXyz::Foo method is exported.
//
class CXyz
{
public:
……
[/Quote]
Coder_Y_Jao 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

析构没意义?

内存能回收吗?

对象能销毁?

[/Quote]

没有意义的意思是导出是没有意义的,并不代表他不存在,所以你可以使用lib静态导入,或者在dll中导出一个CreateCXyz()这样的函数得到对象实例,再由另一个函数释放。
Coder_Y_Jao 2012-08-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

(2)写dll的人,不显示提供这个函数,也保留?

(3)保留与不保留,都能使用。作者没必要强调吧

[/Quote]
(2):原文中已经说名了:then the C++ compiler declares them,即编译器会添加进去,所以也保留

(3):不保留自然就不能使用这个函数了。
liuzu2016 2012-08-01
  • 打赏
  • 举报
回复
析构没意义?

内存能回收吗?

对象能销毁?



[Quote=引用 1 楼 的回复:]
一、请尽量不要使用dll导出类。
二、导出CXyz后,默认构造、赋值构造、析构等函数没有意义,被编译器优化掉了,赋值运算符则保留了下来。
[/Quote]
liuzu2016 2012-08-01
  • 打赏
  • 举报
回复
第一点先不说

书第二点吧

这段英文,看了好几遍, 不懂作者为什么强调operator=?



赋值运算符保留下来是什么意思?

(1)写dll的人,显示写出这个函数,可以保留

(2)写dll的人,不显示提供这个函数,也保留?

(3)保留与不保留,都能使用。作者没必要强调吧



[Quote=引用 1 楼 的回复:]
一、请尽量不要使用dll导出类。
二、导出CXyz后,默认构造、赋值构造、析构等函数没有意义,被编译器优化掉了,赋值运算符则保留了下来。
[/Quote]
zhanshen2891 2012-08-01
  • 打赏
  • 举报
回复
1楼的头像真好看.
Coder_Y_Jao 2012-08-01
  • 打赏
  • 举报
回复
一、请尽量不要使用dll导出类。
二、导出CXyz后,默认构造、赋值构造、析构等函数没有意义,被编译器优化掉了,赋值运算符则保留了下来。
//以下假设你已经配置好VC6的GDI+环境 1.新建一个基于CScrollView的单文档程序, 2.在stdafx.h内添加如下代码: #ifndef ULONG_PTR #define ULONG_PTR unsigned long* #endif #include "GdiPlus.h" using namespace Gdiplus; #pragma comment(lib,"gdiplus.lib") 3.在GdiApp.h里的CGdiAppApp加入两个成员: GdiplusStartupInput m_gdiplusStartupInput;// ULONG_PTR m_pGdiToken;// 4.在GdiApp.cpp的CGdiAppApp构造函数CGdiAppApp()添加如下代码: GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);// 5.在GdiApp.h里给CGdiAppApp添加一个析构函数~CGdiAppApp(); 6.在GdiApp.cpp添加CGdiAppApp的析构函数~CGdiAppApp(): CGdiAppApp::~CGdiAppApp()// { GdiplusShutdown(m_pGdiToken);// } 7.在GdiAppView.h的CGdiAppView添加成员变量: Bitmap* m_pbmp; 并在GdiAppView.cpp的GdiAppView构造函数和析构函数添加如下代码: CGdiAppView::CGdiAppView():m_pbmp(0)// { // TODO: add construction code here } CGdiAppView::~CGdiAppView() { delete m_pbmp;// m_pbmp=0;// } 8.按Ctrl+W,在弹出的对话框里选择Message Maps选项卡 class name选择CGdiAppView Object IDs里选ID_FILE_OPEN 然后在Message里双击COMMAND 弹出的对话框选择Ok 这时会在Member functions:里多出一条刚添加进去的消息映射函数 双击它会自动跳转进入GdiAppView.cpp的void CGdiAppView::OnFileOpen()函数处 然后添加如下代码: void CGdiAppView::OnFileOpen() { //见源码处 } 9.为了使8处代码有用,还需要: 在GdiAppView.h里添加两个函数声明: int A2U(const char* szA,wchar_t* szU,size_t cnt);// std::wstring A2U(const char* szA);// 并#include 然后在GdiAppView.cpp里加入这两个函数的实现 10.在GdiAppView.cpp的OnDraw函数添加显示代码: void CGdiAppView::OnDraw(CDC* pDC) { //见源码处 } 11.结束.有问题联系hastings1986@163.com //可能你Debug版本编译不过,则打开Gdi+的头文件GdiPlusBase.h //并修改为如下(其实就加了两个重载版本的new和delete): #ifndef _GDIPLUSBASE_H #define _GDIPLUSBASE_H class GdiplusBase { public: void (operator delete)(void* in_pVoid) { DllExports::GdipFree(in_pVoid); } void* (operator new)(size_t in_size) { return DllExports::GdipAlloc(in_size); } void (operator delete[])(void* in_pVoid) { DllExports::GdipFree(in_pVoid); } void* (operator new[])(size_t in_size) { return DllExports::GdipAlloc(in_size); } //////////////////////////////// void* (operator new)(size_t in_size,

64,266

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧