我在项目A里面
#pragma once
#ifndef TestSingleton_API_EXPORT
#define TestSingleton_API_EXPORT _declspec(dllexport)
#else
#define TestSingleton_API_EXPORT _declspec(dllimport)
#endif
namespace test
{
class TestSingleton_API_EXPORT TestSingleton
{
public:
static TestSingleton* getTestSingleton(int);
virtual void test() = 0;
};
}
#include "TestSingleton.h"
#include<iostream>
#include"../test/Singleton.h"
#include"TestSingleton2.h"
using namespace std;
namespace test
{
class TestSingletonPro :public TestSingleton
{
friend class Singleton<TestSingletonPro>;
private:
TestSingletonPro(int aaa)
:m_pTestSingleton2(Singleton<TestSingleton2>::instance(aaa))
{
TestSingleton2* pTestSingleton2 = Singleton<TestSingleton2>::instance();
}
virtual void test()
{
cout << __FUNCTION__ << endl;
}
private:
TestSingleton2* m_pTestSingleton2 = nullptr;
};
TestSingleton* TestSingleton::getTestSingleton(int aaaa)
{
return Singleton<TestSingletonPro>::instance(aaaa);
}
}
#pragma once
#include<iostream>
#include"../test/Singleton.h"
using namespace std;
namespace test{
class TestSingleton2
{
friend class Singleton<TestSingleton2>;
private:
TestSingleton2(int aaqa) {
cout << __FUNCTION__ << endl;
}
public:
void test2()
{
cout << __FUNCTION__ << endl;
}
};
}
在项目B中
#pragma once
#include <utility>
#include <mutex>
#include <type_traits>
// Trait to check if T can be default-constructed
template<typename T, typename = void>
struct CanDefaultConstruct : std::false_type {};
template<typename T>
struct CanDefaultConstruct<T, std::void_t<decltype(T())>> : std::true_type {};
template<typename T>
class Singleton
{
public:
static T* instance()
{
if constexpr (CanDefaultConstruct<T>::value)
{
if (s_instance == nullptr)
{
std::lock_guard<std::mutex> lock(s_mutex); // 确保线程安全
if (s_instance == nullptr) // 双重检查锁定
{
s_instance = new T(); // 使用默认构造函数创建实例
}
}
}
return s_instance;
}
template<typename... Args>
static T* instance(Args&&... args)
{
if (s_instance ==nullptr)
{
std::lock_guard<std::mutex> lock(s_mutex); // Ensure thread-safety
if (s_instance == nullptr) // Double-checked locking
{
s_instance = new T(std::forward<Args>(args)...);
}
}
return s_instance;
}
static void destroyInstance()
{
if (s_instance != nullptr)
{
delete s_instance;
s_instance = nullptr;
}
}
private:
Singleton(){}
Singleton(const Singleton<T>&) {}
~Singleton() {
if (s_instance != nullptr)
{
delete s_instance;
}
}
Singleton<T>& operator = (const Singleton<T>)
{}
private:
static T* s_instance;
static std::mutex s_mutex;
};
template<typename T>
T*Singleton<T>::s_instance = nullptr;
template<typename T>
std::mutex Singleton<T>::s_mutex;
#include <iostream>
#include"../Test1/TestSingleton.h"
#include"../Test1/TestSingleton2.h"
#include"Singleton.h"
using namespace test;
int main() {
TestSingleton* pTestSingleton = TestSingleton::getTestSingleton(123234444);
TestSingleton2* pTestSingleton2 = Singleton<TestSingleton2>::instance();
pTestSingleton2->test2();
return 0;
}
我的问题是 在项目B的main中 TestSingleton2* pTestSingleton2 = Singleton::instance();这句话以后 取不出A项目的代码里已经初始化的单例 这里一直返回nullptr