从 c++ 中的子窗体访问父函数

发布于 2024-12-02 14:50:47 字数 361 浏览 5 评论 0原文

我已经在 C# 或 PHP 甚至动作脚本中看到了这个问题的几个解决方案,但在 C++ 中却没有。我有一个父窗体,它通过新建子窗体并在其上调用 ShowWindow 来调用子窗体。我现在需要子表单能够调用父表单的(公共)函数之一。

我的第一个想法是在子级构造函数中将父级传递给子级,但由于子级不知道父级是什么,我在子级构造函数定义中收到错误。父级知道子级是什么(我在父级窗体的头文件中 #included 子级窗体的头文件),但我无法在不发生冲突的情况下将父级的头文件包含在子级的头文件中。

关于在 C++ 中实现这项工作的更好方法或方法有什么想法吗?另外,我正在使用 C++ Builder 2010 仅供参考。

我已经找到了这个问题的解决方案,并将很快发布。

I have seen a couple of solutions to this problem in c# or PHP or even action script, but not in c++. I have a parent form which calls a child form by newing it and calling ShowWindow on it. I now need the child form to be able to call one of the parent form's (public) functions.

My first thought was to pass the parent to the child in the child's constructor, but as the child does not know what the parent is I get an error in my child's constructor definition. The parent knows what the child is (I #included the child form's header file in the parent form's header file), but I can't include the parent's header file in the child's header file without conflict.

Any ideas on better ways or ways to make this work in c++? Also, I am using C++ Builder 2010 fyi.

I have found the solution to this and will be posting it shortly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤痕我心 2024-12-09 14:50:47

您的问题是交叉依赖:父类和子类需要相互了解。但问题是他们不需要知道太多。解决方案是使用前向声明,如下所示:

parent.h 中:

#include "child.h"

class Parent {
    Child c;
    Parent() : c( this ) {}
};

child.h 中:

class Parent; // this line is enough for using pointers-to-Parent and references-to-Parent, but is not enough for defining variables of type Parent, or derived types from Parent, or getting sizeof(Parent) etc

class Child {
public:
    Child(Parent* p) : parent( p ) {}

private:
    Parent *parent;
};

Your problem is that of cross-dependency: parent and child classes need to know about each other. But the thing is that they don't need to know too much. A solution is to use a forward declaration like this:

In parent.h:

#include "child.h"

class Parent {
    Child c;
    Parent() : c( this ) {}
};

In child.h:

class Parent; // this line is enough for using pointers-to-Parent and references-to-Parent, but is not enough for defining variables of type Parent, or derived types from Parent, or getting sizeof(Parent) etc

class Child {
public:
    Child(Parent* p) : parent( p ) {}

private:
    Parent *parent;
};
她说她爱他 2024-12-09 14:50:47
    On the constructor in my parent class is gives the error "Cannot convert from "TEISForm * const" to "TForm1 *" Any ideas?

这是因为 LabelFrm 被声明为指针。您应该将其声明为成员(我现在不确定这在 VCL 中是否正确),或者使用类似 LabelFrm = new TForm1(this, "", this) 的语法初始化指针在适当的地方(阅读VCL文档,子窗口应该在哪里初始化。

实际上,我现在明白了,它们甚至不像窗口那样具有父子关系,你可以在构造函数中初始化。

    On the constructor in my parent class is gives the error "Cannot convert from "TEISForm * const" to "TForm1 *" Any ideas?

This is because LabelFrm is declared as a pointer. You should either declare it as a member (I'm not sure right now whether this is correct in VCL), or initialize the pointer using syntax like LabelFrm = new TForm1(this, "", this) in the appropriate place (read VCL docs for this, where should the child windows be initialized.

Actually, I get it now, they're not even in parent-child relationship as windows, you can initialize in constructor.

划一舟意中人 2024-12-09 14:50:47

看来最好的方法如下:

Parent.h:

class Child;
class Parent{
public:
Parent(){
//Constructor
}

Parent.cpp:

#include Child.h

//declare a child ie. Child chld = new Child(this);

Child.h:

class Parent;
class Child{
public:
Child(){
//Constructor
}

Child.cpp:

#include Parent.h

Parent prnt;

//point Tell your child that it's parent is of type "Parent" in it's constructor
 Child::Child(TComponent *Owner){
prnt = (Parent *)Owner;
    }

您不应该与 .h 文件中声明的多个内容发生任何冲突,但您完全可以能够在不定义类的情况下声明类,然后再定义它们。

It appears that the best way to do this is as follows:

Parent.h:

class Child;
class Parent{
public:
Parent(){
//Constructor
}

Parent.cpp:

#include Child.h

//declare a child ie. Child chld = new Child(this);

Child.h:

class Parent;
class Child{
public:
Child(){
//Constructor
}

Child.cpp:

#include Parent.h

Parent prnt;

//point Tell your child that it's parent is of type "Parent" in it's constructor
 Child::Child(TComponent *Owner){
prnt = (Parent *)Owner;
    }

You shouldn't get any conflicts with multiple things being declared in the .h files but you are perfectly able to declare classes without defining them and just define them later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文