简体   繁体   English

结构内的类

[英]Class within structure

I want to define the object of a class within a structure and access function members of the class. 我想在结构中定义类的对象并访问该类的函数成员。 Is it possible to achieve this? 有可能实现这一目标吗?

With the following code I am getting a segmentation fault at ps_test->AttachToInput(2145); 通过以下代码,我在ps_test->AttachToInput(2145);处遇到了分段错误ps_test->AttachToInput(2145); . I can't figure out the reason, everything looks correct to me: 我不知道原因,一切对我来说都是正确的:

class test
{
public:
    test();
    virtual ~test();
    int init_app(int argc, char* argv[]);   
    virtual void AttachToInput(int TypeNumber, int DeviceNo=0);
}

struct capture
{
    test h_app;
    gint port;
};

main()
{
    struct capture h_cap;
    test *ps_test = &h_cap.h_app;
    ps_test->AttachToInput(2145);
}

First of all, the only difference between a class and a struct in C++ is that a class' members are private by default and a struct's members are public by default. 首先,C ++中classstruct之间的唯一区别是,默认情况下,类的成员是private ,而默认情况下,结构的成员是public的。 Compiler-generated ctors and dtors are visible in both cases - unless otherwise stated by the programmer (eg they move the default ctor into a private section). 在两种情况下,编译器生成的ctor和dtor都是可见的-除非程序员另有说明(例如,它们将默认ctor移到private部分)。 Otherwise construction and destruction of instances of user-defined types marked class wouldn't be possible without explicit, public declaration - thus defying the very purpose of compiler-generated functions. 否则,如果没有显式的公共声明,就无法构造和销毁标记为class的用户定义类型的实例-因此,违背了编译器生成的函数的宗旨。

So basically, what you do in your example is merely composition of two user defined types which is perfectly legal. 所以基本上,你在你的例子做的仅仅是其中两个是完全合法的用户定义类型的组成 When you create an instance of capture , an instance of test is created as well. 创建capture实例时,也会创建一个test实例。

What you can't do is publicly access AttachToInput() from outside of test and derived types of test . 什么,你不能做的是公开访问AttachToInput()从外部test派生类型的test You need to declare the function public in order for this line to compile: 您需要将函数声明为public以便此行编译:

h_cap.h_app.AttachToInput(); // error: member function of `test` is protected

On another, unrelated note (but I came across it so I mention it), your class test holds a raw pointer to char . 在另一个不相关的注释上(但我碰到了它,所以我提到了它),您的class test包含指向char的原始指针。 Holding raw pointers is ok, if the lifetime of the entity that's being pointed is guaranteed to exceed the lifetime of the object that holds the pointer. 如果要保证所指向实体的生存期超过保存指针的对象的生存期,则可以保留原始指针。 Otherwise, it's very likely the object itself is responsible for the destruction of said entity. 否则,对象本身很可能负责破坏所述实体。 You need to be sure about who owns what and who's responsible for allocation and deallocation of stuff. 您需要确定谁拥有什么以及谁负责物品的分配和重新分配。

EDIT : It should be noted, that Alan Stokes proposed the same in the comment section while I wrote this answer. 编辑 :应该指出的是,在我撰写此答案时,艾伦·斯托克斯(Alan Stokes)在评论部分提出了相同的建议。 :) :)

EDIT2 : Slight oversight, implicit default access is also assumed for base classes depending on how the derived class is declared. EDIT2 :稍加疏忽,基类也假定隐式默认访问,具体取决于如何声明派生类。 See What are the differences between struct and class in C++? 请参见C ++中的struct和class有什么区别? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM