初始化静态对象的替代方法。

编程入门 行业动态 更新时间:2024-10-26 18:23:01
本文介绍了初始化静态对象的替代方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现静态对象是在加载共享库时 时初始化的。初始化导致构造函数的调用 。 我可以知道任何方式我可以初始化静态对象而不需要 调用构造函数? 以下是示例编码。 头文件ASURegistrationManager.h #include" ASURegistration。 h" class ASURegistrationManager { public: static ASURegistration& GetASUR管理(); 私人: 静态ASURegistration ASUReg; //静态对象 ASURegistrationManager(); ~ASURegistrationManager(); }; 头文件ASURegistration.h #include" ApplicationInterface.h" class ASURegistration:public IApplicationInterface { public: ASURegistration(); ~ASURegistration(); }; 头文件ApplicationInterface.h 类IApplicationInterface { public: IApplicationInterface( ); //这将产生线程 ~IApplicationInterface(); }; ASURegistration.cpp #include" ASURegistration.h" ASURegistration :: ASURegistration():IApplicationInterface() { } ASURegistration :: ~ASURegistration() { } ASURegistrationManager.cpp #include" ASURegistrationManager.h" ASURegistration ASURegistrationManager :: ASUReg; //这个初始化 调用IApplicationInterface构造函数来创建线程。 ASURegistrationManager :: ASURegistrationManager() { } ASURegistrationManager :: ~ASURegistrationManager() { } ASURegistration& ASURegistrationManager :: GetASURegistration() { return(ASURegistration&)ASUReg; } ApplicationInterface.cpp #include" ApplicationInterface.h" using namespace std; void * APIProcessEvent(void * pvASUInterface)//线程函数 { while(true) { sleep (5); } 返回(0); } IApplicationInterface :: IApplicationInterface() { pthread_t ProcessEventThread; pthread_attr_t attr; cout<< ; 在IApplicationInterface \ n中; if(pthread_attr_init(& attr)!= 0) { std :: cout<<"" pthread_attr_init系统调用线程 failed.\ n" ;; } if(pthread_attr_setscope(& attr,PTHREAD_SCOPE_SYSTE M)!= 0) { std :: cout<<"" pthread_attr_setscope系统调用线程 failed.\" ;; } if(pthread_create(& ProcessEventThread,& attr,APIPro cessEvent,NULL) != 0) { std :: cout<<"无法创建Process Event Thread.\ n" ;; } } IApplicationInterface :: ~IApplicationInterface() { } 我有调用静态方法的cpp函数 ASURegistrationManager :: GetASURegistration()。 #包括< stdio.h> #include" wrap.h" #include" AClass.hpp" #include" CppLibAPI.h" //这只是上面显示的 头文件的组合头文件。 using namespace std; extern" ; C" { void * AClass_new(int x) { ASURegistration& ASUReg = ASURegistrationManager :: GetASURegistration(); return((void *)new AClass(x)); // AClass是一个cpp类 } } 以上cpp程序编译为共享库.so将被使用 由C程序。 C程序如下。 #include< stdio.h> #include" wrap.h" int main(void) { printf(" main()starting\\\"); 返回0; } 当我运行C程序时,输出如下: 在IApplicationInterface main()开始 尽可能看到调用了IApplicationInterface构造函数,它将在C main函数之前生成一个线程。在 中的静态方法AClass_new()cpp函数导致静态 变量ASUReg的初始化,后者又调用IApplicationInterface 构造函数。我需要仅在当时调用cpp函数AClass_new()时调用线程,而不是在程序期间调用 初始化。还有其他的方法来初始化静态对象 这样没有调用构造函数吗? 我试图使用静态指针。这不会导致初始化时构造函数的 调用,因为指针可以将初始化为null。但是我关注静态方法' 返回。 如果我将声明更改为 静态ASURegistration * ASUReg 所以ASUReg可以初始化为 ASURegistration * ASURegistrationManager :: ASUReg = NULL; 是以下陈述是否正确? ASURegistration& ASURegistrationManager :: GetASURegistration() { return(ASURegistration&)* ASUReg; //这会导致任何问题吗? } 谢谢!

I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can initialize the static object without invoking the constructor? Below is the sample coding. Header file ASURegistrationManager.h #include "ASURegistration.h" class ASURegistrationManager { public: static ASURegistration& GetASURegistration(); private: static ASURegistration ASUReg; //The static object ASURegistrationManager(); ~ASURegistrationManager(); }; Header file ASURegistration.h #include "ApplicationInterface.h" class ASURegistration : public IApplicationInterface { public: ASURegistration(); ~ASURegistration(); }; Header file ApplicationInterface.h class IApplicationInterface { public: IApplicationInterface(); //This will spawn thread ~IApplicationInterface(); }; ASURegistration.cpp #include "ASURegistration.h" ASURegistration::ASURegistration() : IApplicationInterface() { } ASURegistration::~ASURegistration() { } ASURegistrationManager.cpp #include "ASURegistrationManager.h" ASURegistration ASURegistrationManager::ASUReg; //This initialization invoke the IApplicationInterface constructor to create thread. ASURegistrationManager::ASURegistrationManager() { } ASURegistrationManager::~ASURegistrationManager() { } ASURegistration& ASURegistrationManager::GetASURegistration() { return (ASURegistration &)ASUReg; } ApplicationInterface.cpp #include "ApplicationInterface.h" using namespace std; void *APIProcessEvent(void *pvASUInterface)// Thread Function { while(true) { sleep(5); } return(0); } IApplicationInterface::IApplicationInterface() { pthread_t ProcessEventThread; pthread_attr_t attr; cout << "In IApplicationInterface\n"; if(pthread_attr_init(&attr) != 0) { std::cout<<"pthread_attr_init system call for thread failed.\n"; } if(pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTE M) != 0) { std::cout<<"pthread_attr_setscope system call for thread failed.\n"; } if(pthread_create(&ProcessEventThread,&attr,APIPro cessEvent,NULL) != 0) { std::cout<<"Failed to create Process Event Thread.\n"; } } IApplicationInterface::~IApplicationInterface() { } I have the cpp function that calls the static method ASURegistrationManager::GetASURegistration(). #include <stdio.h> #include "wrap.h" #include "AClass.hpp" #include "CppLibAPI.h" //This is just a combination header file of the header files shown above. using namespace std; extern "C" { void * AClass_new(int x) { ASURegistration& ASUReg = ASURegistrationManager::GetASURegistration(); return ((void *)new AClass(x)); //AClass is a cpp class } } The above cpp programs are compiled as a shared library .so to be used by the C program. The C program is as follow. #include <stdio.h> #include "wrap.h" int main(void) { printf("main() starting\n"); return 0; } When I run the C program, the output is as follow: In IApplicationInterface main() starting As you can see the IApplicationInterface constructor is called which will spawn a thread before the C main function. The static method in the AClass_new() cpp function caused the initialisation of the static variable ASUReg which in turns invoke the IApplicationInterface constructor. I need the thread to be spawned only at the point when the cpp function AClass_new() is called instead of during program initialisation. Is there other anyway to initialise the static object such that the constructor is not invoked? I have tried to use a static pointer. That will not cause the invocation of the constructor at initialisation, because a pointer can be initialised to null. But I have concerned on the static method''s return. If I change the declaration to static ASURegistration* ASUReg so ASUReg can be initialised as ASURegistration* ASURegistrationManager::ASUReg=NULL; Is the following statement correct? ASURegistration& ASURegistrationManager::GetASURegistration() { return (ASURegistration &)*ASUReg; //Will this cause any problem? } Thank you!

推荐答案

对于上一篇文章感到抱歉,请早点点按发送按钮。 [snip] Sorry about the previous post, hit the send button to early. [snip] 如果我改变了声明静态ASURegistration * ASUReg 所以ASUReg可以初始化为 ASURegistration * ASURegistrationManager :: ASUReg = NULL; 以下是声明正确吗? ASURegistration& ASURegistrationManager :: GetASURegistration() {返回(ASURegistration&)* ASUReg; //这会导致任何问题吗?} 谢谢! If I change the declaration to static ASURegistration* ASUReg so ASUReg can be initialised as ASURegistration* ASURegistrationManager::ASUReg=NULL; Is the following statement correct? ASURegistration& ASURegistrationManager::GetASURegistration() { return (ASURegistration &)*ASUReg; //Will this cause any problem? } Thank you!

上面看起来还可以。你应该删除演员。 ASURegistration& ASURegistrationManager :: GetASURegistration() { 返回* ASUReg; } john

The above looks OK. You should remove the cast. ASURegistration& ASURegistrationManager::GetASURegistration() { return *ASUReg; } john

Yu < XI ***** @ yahoo>在消息中写道 news:47 ************************** @ posting.google.c om ... Hi, "Yu" <xi*****@yahoo> wrote in message news:47**************************@posting.google.c om... 我发现静态对象在加载共享库时已初始化。初始化导致了构造函数的调用。我可以知道如何在不调用构造函数的情况下初始化静态对象吗? 下面是样本编码。 头文件ASURegistrationManager.h #include" ASURegistration.h" 类ASURegistrationManager {公开:静态ASURegistration& GetASURegistration(); 私人:静态ASURegistration ASUReg; //静态对象 ASURegistrationManager(); ~ASURegistrationManager(); }; 如下所示,我认为它只会调用构造函数 首次致电GetASURegistration() class ASURegistrationManager { public: 静态ASURegistration& GetASURegistration(){ 静态ASURegistration ASUReg; 返回ASUReg; } private: ASURegistrationManager(); ~ASURegistrationManager(); }; 谢谢! I have found that the static object is initialised at the time when the shared libary is loaded. The initialisation caused the invocation of the constructor. May I know of any way that I can initialize the static object without invoking the constructor? Below is the sample coding. Header file ASURegistrationManager.h #include "ASURegistration.h" class ASURegistrationManager { public: static ASURegistration& GetASURegistration(); private: static ASURegistration ASUReg; //The static object ASURegistrationManager(); ~ASURegistrationManager(); }; What about the following, I think it will only invoke the constructor on the first call to GetASURegistration() class ASURegistrationManager { public: static ASURegistration& GetASURegistration() { static ASURegistration ASUReg; return ASUReg; } private: ASURegistrationManager(); ~ASURegistrationManager(); }; Thank you!

> >如果我将声明更改为 > > If I change the declaration to 静态ASURegistration * ASUReg 所以ASUReg可以初始化为 ASURegistration * ASURegistrationManager :: ASUReg = NULL; 以下陈述是否正确? ASURegistration& ASURegistrationManager :: GetASURegistration() {返回(ASURegistration&)* ASUReg; //这会导致任何问题吗?} 谢谢! static ASURegistration* ASUReg so ASUReg can be initialised as ASURegistration* ASURegistrationManager::ASUReg=NULL; Is the following statement correct? ASURegistration& ASURegistrationManager::GetASURegistration() { return (ASURegistration &)*ASUReg; //Will this cause any problem? } Thank you!

以上看起来还可以。你应该删除演员阵容。 ASURegistration& ASURegistrationManager :: GetASURegistration() {返回* ASUReg; } 约翰

The above looks OK. You should remove the cast. ASURegistration& ASURegistrationManager::GetASURegistration() { return *ASUReg; } john

您好约翰, 上面会崩溃。以下应该是我的问题。谢谢! ASURegistration& ASURegistrationManager :: GetASURegistration() { if(!ASUReg) { ASURegistration asuReg = ASURegistration( ); ASUReg =& asuReg; } 返回* ASUReg; } Yu :)

Hi John, The above will crash. The following should should my problem. Thanks! ASURegistration& ASURegistrationManager::GetASURegistration() { if (!ASUReg) { ASURegistration asuReg = ASURegistration(); ASUReg = &asuReg; } return *ASUReg; } Yu :)

更多推荐

初始化静态对象的替代方法。

本文发布于:2023-11-06 07:46:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   静态   对象   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!