C++ 如何在一个类中调用另一个类中的函数对象?
我在类C++ 如何在一个类中调用另一个类中的函数对象?,c++,function,class,oop,C++,Function,Class,Oop,我在类adilog中有一个函数,它有一个用户输入x11,我想在类BDoc中调用它。怎么做 .h file #ifndef A_DIALOG_H #define A_DIALOG_H class ADialog : public wxDialog { public: ADialog(wxWindow* parent, wxWindowID id, int click); wxTextCtrl *d_x1; void setAData(); }; #endi
adilog
中有一个函数,它有一个用户输入x11
,我想在类BDoc
中调用它。怎么做
.h file
#ifndef A_DIALOG_H
#define A_DIALOG_H
class ADialog : public wxDialog
{
public:
ADialog(wxWindow* parent, wxWindowID id, int click);
wxTextCtrl *d_x1;
void setAData();
};
#endif
.cpp文件的一部分
void ADialog::setAData()
{
double x11;
d_x1->GetValue().ToDouble(&x11);
}
void BDoc::ATCut()
{
//(get the value of x11 that user inputs in this part)
}
.cpp文件的一部分
void ADialog::setAData()
{
double x11;
d_x1->GetValue().ToDouble(&x11);
}
void BDoc::ATCut()
{
//(get the value of x11 that user inputs in this part)
}
让我们班成为BDoc班的朋友。因此,您可以轻松访问类BDoc中的类adilog函数。在调用者类中包含
adilog
类头BDoc
,然后调用SetAData()
如果需要,可以修改
SetAData()
以返回x11
。我在adilog
中没有看到任何方法。SetAData()不是成员函数吗?在类声明中没有这样声明。我不知道它是公共的
还是私人的
。它是静态的还是非静态的?你能发布完整的类吗?哦,好的,对不起,它是公共的,我会编辑它,因为它不是静态的
,你需要BDoc
中的adilog
的一个实例来获取该方法的结果。我这样做了,但是x11没有被访问,我无法更改类型,它是无效的。我的意思是改变类型意味着改变更多的东西,所以我想知道是否还有其他方法round@Leo001将x11
作为成员变量,并通过公共接口访问它怎么样?@Leo001您可以通过d_x1
直接在BDoc
中获取x11
吗?非常感谢,我刚刚找到了一种方法在另一个类中声明指针,然后在adilog::setAData aswxGetApp().getPattern()->xc1中调用它=x11@Leo001因此,朋友的缺失并不是罪魁祸首。忘掉这个“答案”。