问题描述:

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]

网友答案:

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
    }

}

网友答案:

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.

网友答案:

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

网友答案:

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
    }
}
相关阅读:
Top