来自班级的访问列表,在父级中

发布于 2024-12-12 04:40:16 字数 655 浏览 0 评论 0原文

我是 C++ 新手,并且有使用其他编程语言的经验,但我有一个问题: 如何从子类、父类访问列表?

这是我的布局: 带有函数TutorialApp::update() 的TutorialApp.cpp 在该函数中,我想从另一个类访问列表 mParticles2y 该列表是在:ParticleController.cpp 中创建的,如下所示:

std::list<int> mParticles2y;

我尝试像这样访问该列表[在TutorialApp 中]: mParticleController.mParticles2y.size() [获取它的大小]

但这给了我这个错误:

在没有适当运算符的情况下调用类类型的对象

所以我真的不知道从这里去哪里...

PS:我使用 mParticleController 因为这是我的脚本中的状态:

ParticleController mParticleController;

我希望这是足够的信息。

PS:我不太确定这称为类或子类,我使用这些术语是因为我从 ActionScript 中了解它们[它以类似的方式与类一起工作]

I'm new to C++, and have experience with other programmas languages, but I have a question:
How can I access a list from a sub class, from the parent?

Here is my layout:
TutorialApp.cpp with function TutorialApp::update()
In that function I want to access the list mParticles2y from another class
that list is made in: ParticleController.cpp, like this:

std::list<int> mParticles2y;

I've tried accessing the list like this [in TutorialApp]:
mParticleController.mParticles2y.size() [to get it's size]

but that gives me this error:

call of an object of a class type without appropriate operator

So I dont really know where to go from here...

PS: I use mParticleController because that is state in my script:

ParticleController mParticleController;

I hope this is enough info.

PS: I'm not really sure this is called a class, or child, I use these terms because I know them from ActionScript [which works with classes in a similar way]

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

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

发布评论

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

评论(4

初心未许 2024-12-19 04:40:16

有错误的循环代码应该看起来更像这样。它可能无法准确编译,因为我目前无法轻松编译它。这不是解决问题的理想方法,但它对代码的影响最小。我会将此循环作为成员函数移至 ParticleController 类,该成员函数返回 true/false 以指示命中。这样封装起来会更好。

    for(std::list<int>::iterator yit = mParticleController.mParticles2y.begin(), 
       std::list<int>::iterator xit = mParticleController.mParticles2x.begin();
       yit != mParticleController.mParticles2y.end() && xit != mParticleController.mParticles2x.end();
yit++, xit++)
   {
    if(
        (coordy >= *it) && (coordy <= (*it) + 40) &&
        (coordx >= *xit) && (coordx <= (*xit) + 40) 
       )
    {   
       mParticleController.removeTargetedParticles(i); //remove blokje
             score += 1; //score verhogen
    }

}

Your code for the loop that has the errors should look more like this. It may not compile exactly since I can't compile it easily at the moment. This isn't the ideal way to go about it, but it is the minimal impact to your code. I would move this loop to the ParticleController class as a member function that returned true/false to indicate a hit. It would be better encapsulated that way.

    for(std::list<int>::iterator yit = mParticleController.mParticles2y.begin(), 
       std::list<int>::iterator xit = mParticleController.mParticles2x.begin();
       yit != mParticleController.mParticles2y.end() && xit != mParticleController.mParticles2x.end();
yit++, xit++)
   {
    if(
        (coordy >= *it) && (coordy <= (*it) + 40) &&
        (coordx >= *xit) && (coordx <= (*xit) + 40) 
       )
    {   
       mParticleController.removeTargetedParticles(i); //remove blokje
             score += 1; //score verhogen
    }

}

痴者 2024-12-19 04:40:16

好的,所以这是一个盲目的尝试,因为你的问题虽然非常冗长,但缺乏重现该问题所需的代码。

您只能访问其他对象的公共成员(数据或函数)。也就是说,为了访问 mParticleController.mParticles2ymParticles2y 必须是任何类型的公共成员 mParticleController 是。

当然,公共数据成员不受欢迎,这是有充分理由的。一个类应该代表一个抽象,如果你有一个粒子控制器,它应该实现控制粒子所需的一切,而不是把它的内容公之于众,让每个人筛选并获取他们需要的东西。

这称为封装,是面向对象范式的基石之一。

Ok, so this is a shot in the dark, since your question, while very wordy, is short on code necessary to reproduce the issue.

You can only access public members (data or functions) of other objects. That is, in order to access mParticleController.mParticles2y, mParticles2y must be a public member of whatever type mParticleController is of.

Of course, public data member are frowned upon, and for good reasons. A class should represent an abstraction, and if you have a particle controller, it should implement everything necessary to control particles, rather than spilling its guts out in the public for everyone to sift through and take what they need.

This is called encapsulation, and one of the cornerstones of the object oriented paradigm.

不必你懂 2024-12-19 04:40:16

size是一个方法,需要写size()

size is a method, you need to write size().

瑕疵 2024-12-19 04:40:16

您提供的信息有些含糊,但看起来您可能正在尝试从另一个类(TutorialApp)访问一个类(ParticleController)的私有状态(列表)。

我假设以下代码结构(请注意,我没有尝试编译它,所以它可能不太正确):

#include <list>

class ParticleController
{
    public:
        ParticleController() {}

        std::list<int> &getParticles2y() const
        {
            return mParticles2y;
        }

    private:
        std::list<int> mParticles2y;
}

class TutorialApp
{
public:
    void update()
    {
        // ...
        ParticleController mParticleController;
        //std::list<int> particles2y = mParticleController.mParticles2y; // error - accessing private member of another class
        std::list<int> &particles2y = mParticleController.getParticles2y(); // OK
    }
}

The information you provide is somewhat ambiguous, but it looks like you may be attempting to access the private state (the list) of one class (the ParticleController) from another class (The TutorialApp).

I'm assuming the following code structure (note that I haven't tried to compile this so it might not be quite right):

#include <list>

class ParticleController
{
    public:
        ParticleController() {}

        std::list<int> &getParticles2y() const
        {
            return mParticles2y;
        }

    private:
        std::list<int> mParticles2y;
}

class TutorialApp
{
public:
    void update()
    {
        // ...
        ParticleController mParticleController;
        //std::list<int> particles2y = mParticleController.mParticles2y; // error - accessing private member of another class
        std::list<int> &particles2y = mParticleController.getParticles2y(); // OK
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文