C 确定了外部类指针的最佳方法
C++ determine best way for external class pointer
对于我当前忙于开发的C 应用程序,我有几个类,我需要通过整个代码访问,而无需创建新对象
所以搜索我发现可以使用的一种方法是在外部链接规范中。
我想知道什么是使用这种外部方法的最佳方法,我写了一些示例代码
classone.h
#ifndef CLASSONE_H
#define CLASSONE_H
class ClassOne
{
public:
ClassOne();
void showClassOneInt();
private:
int m_classOneInt;
};
extern ClassOne *classOne;
---------------------------------------
classone.cpp
#include "classone.h"
#include <QDebug>
ClassOne *classOne;
ClassOne::ClassOne()
{
m_classOneInt = 1;
}
void ClassOne::showClassOneInt()
{
qDebug() << "ClassOneInt: " << m_classOneInt;
}
---------------------------------------
classtwo.h
#ifndef CLASSTWO_H
#define CLASSTWO_H
class ClassTwo
{
public:
ClassTwo();
void showClassTwoInt();
private:
int m_classTwoInt;
};
#endif // CLASSTWO_H
---------------------------------------
classtwo.cpp
#include "classtwo.h"
#include <QDebug>
ClassTwo::ClassTwo()
{
m_classTwoInt = 2;
}
void ClassTwo::showClassTwoInt()
{
qDebug() << "ClassTwoInt: " << m_classTwoInt;
}
---------------------------------------
classthree.h
#ifndef CLASSTHREE_H
#define CLASSTHREE_H
class ClassThree
{
public:
ClassThree();
void showClassThreeInt();
private:
int m_classThreeInt;
};
#endif // CLASSTHREE_H
---------------------------------------
classthree.cpp
#include "classthree.h"
#include <QDebug>
ClassThree::ClassThree()
{
m_classThreeInt = 3;
}
void ClassThree::showClassThreeInt()
{
qDebug() << "ClassThreeInit: " << m_classThreeInt;
}
---------------------------------------
classtest.cpp
#include "classtest.h"
#include "classone.h"
#include "classtwo.h"
#include "classthree.h"
//Class one pointer already in header
//Class two
extern ClassTwo *classTwo;
//Class three
extern ClassThree *classThree;
ClassTest::ClassTest()
{
//Execute class one
classOne->showClassOneInt();
//Execute class two
classTwo->showClassTwoInt();
//Execute class three
classThree->showClassThreeInt();
}
---------------------------------------
main.cpp
#include <QCoreApplication>
#include "classone.h"
#include "classtwo.h"
#include "classthree.h"
#include "classtest.h"
//Class one pointer already in header file
//Class two pointer
ClassTwo *classTwo;
//Class three pointer
ClassThree *classThree;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Create object for class one
classOne = new ClassOne;
//Create object for class two
classTwo = new ClassTwo;
//Create object for class three
ClassThree three;
classThree = &three;
//Create a classTest object
ClassTest test;
return a.exec();
}
请您能告诉我什么是最好的方法,谢谢您的帮助。
最好的方法是不要这样做,而是使用依赖注入。
如果您选择这样做,则至少应该使用Getter/Factory功能(即ClassOne &getClassOne()
((,因此可以:
- 确保随机代码不能更改
- 通过首次使用(有时适当,有时不适当(,隐式处理构造顺序。
拥有全球状态通常不是一个好主意,请寻求消除它。
如果无法做到,请尝试Singleton模式。
class Singleton
{
Singleton(); //keep constructors private to avoid creation by others
static Singleton inst;
public:
static Singleton& Instance() {return inst;}
};
Singleton Singleton::inst;
相关文章:
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 指针验证的最佳实践?
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- C++ 将函数指针与最佳性能相结合
- 使用作为参数返回的指针的最佳做法是什么
- 保留计时器集合(对象与指针)的最佳方法
- 将对象附加到智能指针向量的最佳方法
- 删除类型转换指针的最佳方法
- 将指针从派生到基类的方法的最佳方法
- C 确定了外部类指针的最佳方法
- 比较UINTPTR_T和指针类型的最佳方法是什么?
- C++ / 使用指针的最佳实践
- 返回可能在C++中分配在堆栈上的成员时,最佳指针/引用类型是什么
- 两者之间的最佳方法是什么:实例化对象或使用指针
- std::shared_ptr语言 - 将共享指针作为参数传递的最佳做法
- 与自定义分配器一起使用的最佳唯一指针是什么
- 工厂函数的最佳智能指针返回类型是什么
- 返回指针的最佳方式
- 管理指向派生对象的指针集合的最佳方式
- C++参考与指针最佳实践