【C++】Class A::instance()->func()用法

本文探讨了C++中Singleton模式的实现方式,通过一个具体的代码示例,解析了如何使用静态成员函数instance()来获取类的唯一实例,并调用其成员函数。这一模式在确保类只有一个实例的同时,提供了全局访问点。

今天看到这个代码有点懵,特此记录:

instance()的定义应该也是A *instance()

它返回一个classA的对象指针

A *p

p = Class A::instance();

p->func()

 

暂时没有搜到其它资料,参考了百度知道的一个回答,待以后确认了更新。

BOOL CDialogConfig::OnInitDialog() { CDialogEx::OnInitDialog(); // TODO: 在此添加额外的初始化 try { // 1. 初始化驱动 //sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance(); sql::Driver* driver = sql::mysql::get_driver_instance();1>------ 已启动生成: 项目: Basic, 配置: Debug x64 ------ 1>CDialogConfig.cpp 1>CDialogConfig.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) void __cdecl check(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_?check@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z),该符号在函数 "void __cdecl check_lib(void)" (?check_lib@@YAXXZ) 中被引用 1>CDialogConfig.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) void __cdecl check(class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > const &)" (__imp_?check@@YAXAEBV?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@std@@@2@@std@@@Z),该符号在函数 "void __cdecl check_lib(void)" (?check_lib@@YAXXZ) 中被引用 1>CDialogConfig.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) class sql::mysql::MySQL_Driver * __cdecl sql::mysql::_get_driver_instance_by_name(char const * const)" (__imp_?_get_driver_instance_by_name@mysql@sql@@YAPEAVMySQL_Driver@12@QEBD@Z),该符号在函数 "class sql::mysql::MySQL_Driver * __cdecl sql::mysql::get_driver_instance_by_name(char const * const)" (?get_driver_instance_by_name@mysql@sql@@YAPEAVMySQL_Driver@12@QEBD@Z) 中被引用 1>C:\Users\Administrator\Desktop\Basic\Basic\x64\Debug\Basic.exe : fatal error LNK1120: 3 个无法解析的外部命令 1>已完成生成项目“Basic.vcxproj”的操作 - 失败。 ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
10-23
int EloFormatApp::FormatPartition(const map<string, string>& formatPartition, const bool& isSecureFormat) { FUNC_ENTRY(""); int errorStatus(ERROR_SUCCESS); UNREFERENCED_PARAMETER(isSecureFormat); if (formatPartition.empty()) { m_isFormatError = true; errorStatus = ERROR_INVALID_DATA; LOGERRORMSG(ZONE_ERROR, "null data . Error: %u.", errorStatus); FUNC_EXIT("returns %u", errorStatus); return errorStatus; } #ifdef __arm__ int status(ERROR_SUCCESS); map <string, string>::const_iterator partition = formatPartition.begin(); for (; partition != formatPartition.end(); ++partition) { errorStatus = CheckPartitionMountPath(partition->first ,partition->second); if(ERROR_SUCCESS == errorStatus) { if ( umount2(partition->second.c_str(), static_cast<int>(MNT_DETACH)) != 0 ) { errorStatus = ERROR_UNMOUNT; std::string errorLog("failed to unmount " + partition->second ); m_formatRetryData.partitionName.push_back(partition->first); m_isFormatError = true; continue; } } else { if(!(Poco::File(partition->first).exists() && Poco::File(partition->first).isDevice())) { std::string errorLog("checking mount path for " + partition->first + " failed" ); LOGERRORMSG(ZONE_ERROR, "%s ", errorLog.c_str()); m_formatRetryData.partitionName.push_back(partition->first); m_isFormatError = true; continue; } } std::shared_ptr<ICommandInterface> commandInterfaceInstance; if(isSecureFormat) { errorStatus = WipePartition(partition->first); if(ERROR_SUCCESS != errorStatus) { //formating partition even if secure wipe for partition is failed. commandInterfaceInstance = CommandInterfaceFactory::GetCommandInterfaceWrapper(); if(commandInterfaceInstance) { status = commandInterfaceInstance->CommandInterface_System({"mkfs.ext4 -O ^orphan_file -F ", std::string(partition->first), " -L ",getPartitionLabelToFormat(partition->first)}); if (ERROR_SUCCESS != status) { std::string errorLog("failed to format" + partition->first + " using mkfs.ext4" ); LOGERRORMSG(ZONE_ERROR, "%s ", errorLog.c_str()); } } else { LOGERRORMSG(ZONE_ERROR, "Command interface class instance was not created"); } std::string errorLog("failed to wipe partition " + partition->first); LOGERRORMSG(ZONE_ERROR, "%s ", errorLog.c_str()); m_formatRetryData.partitionName.push_back(partition->first); m_isFormatError = true; continue; } } commandInterfaceInstance = CommandInterfaceFactory::GetCommandInterfaceWrapper(); if(commandInterfaceInstance) { status = commandInterfaceInstance->CommandInterface_System({"mkfs.ext4 -O ^orphan_file -F ", std::string(partition->first), " -L ", getPartitionLabelToFormat(partition->first)}); if (ERROR_SUCCESS != status) { errorStatus = ERROR_INTERNAL_ERROR; std::string errorLog("failed to format" + partition->first + " using mkfs.ext4" ); LOGERRORMSG(ZONE_ERROR, "%s ", errorLog.c_str()); m_formatRetryData.partitionName.push_back(partition->first); m_isFormatError = true; continue; } } else { LOGERRORMSG(ZONE_ERROR, "Command interface class instance was not created"); } const unsigned long mountFlags = MS_RELATIME|MS_SYNCHRONOUS|MS_NOEXEC; if ( mount(partition->first.c_str(), partition->second.c_str(), FILESYSTEM_EXT4, mountFlags, nullptr) != 0 ) { errorStatus = ERROR_INTERNAL_ERROR; std::string errorLog("failed to mount" + partition->first); LOGERRORMSG(ZONE_ERROR, "%s ", errorLog.c_str()); m_formatRetryData.partitionName.push_back(partition->first); m_isFormatError = true; continue; } } #endif FUNC_EXIT(""); return errorStatus; } 这段代码做了什么
07-22
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值