通过 C++ 中的函数创建类的新句柄
creating a new handle to class through a function in c++
我不确定我的标题是否有意义。我正在尝试用 c++ 实现一个队列类。以下是我到目前为止的q.cpp课程
#include "q.h"
q::q()
{
for(int i = 0; i < q::QUEUE_SIZE; i++)
{
q::data[i] = '0';
}
q::front = q::rear = -1;
printf(" YEA QUEUE HAS BEEN CREATED ");
}
q* q::createQueue()
{
return new q;
}
现在在我的输出类 (main.cpp) 中,我希望能够通过 createQueue() 函数调用并返回当前队列类对象的句柄来创建新队列
像这样的东西
q* firstQueue = createQueue();
q* secondQueue = createQueue();
我在想我的主要构造函数 q() 需要私有,并且 createQueue 应该像工厂一样运行,所以我的 q.h 文件应该如下所示?
#ifndef Q_H
#define Q_H
#include <stdio.h>
class q
{
public:
void destroy_queue(q* currentQueue);
void enque_byte(q* currentQueue,unsigned char b);
unsigned char deque_byte(q* currentQueue);
static q* createQueue();
private:
static const int QUEUE_SIZE = 2048;
unsigned char data[QUEUE_SIZE];
int front, rear;
q();
};
#endif // Q_H
然后我如何能够通过调用进行排队、取消排队操作
然后对任意数量的队列执行排队和取消排队操作,例如
enqueue(firstQueue,5);
dequeue(firstQueue);
enqueue(secondQueue,10);
..
..
并且不使用对象来调用它们
firstQueue->enqueue(...)
我对代码的组织有点困惑。希望有人能提供关于如何实现这种结构的见解?
你可以写简单的包装器,比如
void enqueue(q* obj, int x) {
obj->enqueue(x);
}
然而,所有这些都没有多大意义;C++习语是使用对象和方法,除非你处于这个成语效果不佳的情况下(例如多方法),否则遵循它比反对它容易得多。
相关文章:
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 如何获取边缘窗口句柄 (HWND)?
- 枚举进程模块在有效句柄上返回无效句柄
- 在 c/c++ 中打开 PhysicalDrive 的句柄并写入 MBR 的 linux 等效是什么?
- 从运行服务的应用程序代码中提取窗口句柄
- 在读取文件后重置句柄
- 是否有像地图这样的C++结构,但我得到的不是值的键,而是值的句柄?
- Microsoft ODBC 无法创建有效的句柄
- 我是否可以使用 win32 句柄以编程方式记录发送到/接收到 USB/COM 的内容
- 获取特定进程的句柄数
- C++ 创建 NdisProt 驱动程序的句柄
- 是否需要关闭来自 WinHTTP 异步的句柄?
- 句柄OK全部崩溃
- 在张量流中创建新操作时指定形状句柄的形状(输出张量的秩问题)
- 如何在C++程序中打开一个新的cmd.exe并获取其句柄
- 为什么这是SAMR句柄(CPP句柄)的相同地址
- 捕获新创建的模态对话框的窗口句柄
- 通过 C++ 中的函数创建类的新句柄
- 如何在C/C++中创建一个新进程,并在WinXp中获得这个新进程句柄
- 用新的控制台窗口创建进程,但覆盖一些std i/o句柄