c ++ class&班级名称

时间:2014-08-10 18:34:46

标签: c++ openframeworks

我正在尝试扩展我在c ++中的知识,并试图在openFrameworks中找出以下用于碰撞检测的函数,并注意到我以前从未见过的一些奇怪的模式,这里是代码:

    void addForce(float targetX, float targetY, float radius, float scale) {
    std::deque<Tree*> toProcess;
    toProcess.push_back(this);
    float xd, yd, length, effect;
    float sqradius = radius * radius;
    while(!toProcess.empty()) {
        Tree& curTree = *(toProcess.front());
        toProcess.pop_front();
        if(targetX > curTree.minX - radius && targetX < curTree.maxX + radius &&
           targetY > curTree.minY - radius && targetY < curTree.maxY + radius) {
            if(curTree.nParticles) {
                for(int i = 0; i < curTree.nParticles; i++) {
                    Particle& curParticle = *(curTree.particles[i]); //IS IT PASSING A REFERENCE TO A POINTER OF A METHOD CALLED .particles[i]????????
                    xd = curParticle.x - targetX;
                    yd = curParticle.y - targetY;
                    if(xd != 0 && yd != 0) {
                        length = xd * xd + yd * yd;
                        if(length < sqradius) {
                            length = sqrtf(length);
                            xd /= length;
                            yd /= length;
                            effect = 1 - (length / radius);
                            effect *= scale;
                            curParticle.xf += effect * xd;
                            curParticle.yf += effect * yd;
                        }
                    }
                }
            } else if(curTree.hasChildren) {
                toProcess.push_back(curTree.nw);
                toProcess.push_back(curTree.ne);
                toProcess.push_back(curTree.sw);
                toProcess.push_back(curTree.se);
            }
        }
    }
}

如您所见:

Particle& curParticle = *(curTree.particles[i]);

将引用(?)传递给类(?)的指针。

你也可以在这里看到它:

Tree& curTree = *(toProcess.front());

这里是否将curTree解除引用到deque的前面(?)

如果某些C ++专家可以向我解释这一点,我将非常感激。在此先感谢:)

1 个答案:

答案 0 :(得分:1)

您只需要查看对象声明即可回答您的问题:

std::deque<Tree*> toProcess;

所以,toProcess是一个指针的副词。现在,请参阅front()的文档:http://en.cppreference.com/w/cpp/container/deque/front

成员函数返回对第一个项目的引用。在您的情况下,它是对Tree指针的引用,该指针可以复制到容器的value_type中。请参阅deque定义的typedef / aliases的文档:http://en.cppreference.com/w/cpp/container/deque

在C ++ 98中:

Tree *front = toProcess.front();

在C ++ 11中:

auto front  = toProcess.front();

注意:

  • 堆栈变量在任何一种情况下都包含一个指针
  • 指针的值从引用复制到指针
  • 在占用机器寄存器时复制指针很便宜(而且很常见)