gpt4 book ai didi

c++ - 如何通过与类无关的其他函数调用实例化的类成员变量?

转载 作者:行者123 更新时间:2023-11-27 23:34:50 24 4
gpt4 key购买 nike

我现在正在创建一个文件事务系统(通过 FTP),使用 GUI 的 wxWidgets 和来自 CodeProject (http://www.codeproject.com/KB/threads/SynchronizedThreadNoMfc.aspx) 的多线程类 - 请先阅读文章以供引用。

在我的 GUI 中,我有一个文本框 (wxTextCtrl),它存储要发送到 FTP 服务器的文件路径,我想通过多线程函数获取它的值。

到目前为止,这是我的代码:(简化;多个文件)

/////// Organizer.h // Main header file that utilizes all other headers
#include <wx/wx.h>
#include <wx/datectrl.h>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
#include "Threading.h"
#include "MainDlg.h"
#include "svDialog.h"

///////// Threading.h // Please read the article given above
#include "ou_thread.h"
using namespace openutils;

extern HINTERNET hInternet; // both declared in MainDlg.cpp
extern HINTERNET hFtpSession;

class svThread : public Thread
{
private:
char* ThreadName;
public:
svThread(const char* szThreadName)
{
Thread::setName(szThreadName);
this->ThreadName = (char* )szThreadName;
}
void run()
{
if(this->ThreadName == "Upload")
{
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
hFtpSession = InternetConnect(hInternet, L"myserver.com", INTERNET_DEFAULT_FTP_PORT, L"user", L"pass", INTERNET_SERVICE_FTP, 0, 0);

std::string filenameOnServer((char* )tb_file->GetValue().c_str()); // HERE..the tb_file..
std::vector<std::string> filepathParts;
__strexp(filenameOnServer, "\\", filepathParts); // this is user-defined function that will split a string (1st param) with the given delimiter (2nd param) to a vector (3rd param)
filenameOnServer = filepathParts.at(filepathParts.size() - 1); // get only the filename

if(FtpPutFile(hFtpSession, tb_file->GetValue().c_str(), (LPCWSTR)filenameOnServer.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
{
MessageBox(NULL, L"Upload Complete", L"OK", 0);
}
else
{
MessageBox(NULL, L"Upload Failed", L"OK", 0);
}
}
}
};

////////// svDialog.h
class svDialog : public wxFrame
{
public:
svDialog(const wxString &title);
void InitializeComponent();
void ProcessUpload(wxCommandEvent &event); // function (button event) that will start the UPLOAD THREAD
wxTextCtrl *tb_file; // this is the textbox
//....other codes
};

///////////svDialog.cpp
#include "Organizer.h"
Thread *UploadRoutine;

svDialog::svDialog(const wxString &title) : wxFrame(...) // case unrelated
{
InitializeComponent();
}
void svDialog::InitializeComponent()
{
tb_file = new wxTextCtrl(...);
//......other codes
}
void svDialog::ProcessUpload(wxCommandEvent &event)
{
UploadRoutine = new svThread("Upload");
UploadRoutine->start();
//......other codes
}

////// MainDlg.cpp // (MainDlg.h only contains the MainDlg class declaration and member function prototypes)
#include "Organizer.h"

HINTERNET hInternet;
HINTERNET hFtpSession;
IMPLEMENT_APP(MainDlg) // wxWidgets macro

bool MainDlg::OnInit() // wxWidgets window initialization function
{
//......other codes
}

好吧,正如您在我上面的代码中看到的,我想获取 tb_file 的内容(使用 tb_file->GetValue())并将其传递给多线程函数(void run())以供上传稍后。

如有任何帮助,我们将不胜感激!

谢谢。 (抱歉代码太长了..)

最佳答案

这很简单。

您应该创建一个接受 std::string(或任何其他参数)并将其(它们)存储在 svThread 对象中的启动函数。然后你可以在运行函数中访问它:

class svThread : public Thread
{
private:
char* ThreadName;
std::string FileName;

public:
svThread(const char* szThreadName)
{
Thread::setName(szThreadName);
this->ThreadName = (char* )szThreadName;
}

void Start(const std::string& filename)
{
this->FileName = filename;
Thread::Start();
}

void Run()
{
// ...
if(FtpPutFile(hFtpSession, FileName,(LPCWSTR)filenameOnServer.c_str(), FTP_TRANSFER_TYPE_BINARY, 0))
// ...
}
};

在你的对话类中,你只需要像这样开始你的线程:

UploadRoutine = new svThread("Upload");
UploadRoutine->start(tb_file->GetValue().c_str());

关于c++ - 如何通过与类无关的其他函数调用实例化的类成员变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1481996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com