简体   繁体   English

C++中的受保护变量

[英]Protected variables in C++

So, working with some classes and inheritance, I created an Entity class which inherits from an ImageShape class which inherits from an abstract Shape class.因此,使用一些类和继承,我创建了一个继承自 ImageShape 类的实体类,该类继承自抽象 Shape 类。 I also have a Wall class inheriting from a RectShape class, which inherits from the same abstact Shape class.我还有一个继承自 RectShape 类的 Wall 类,该类继承自同一个抽象 Shape 类。

I need to work around collisions, so I called Wall to be Entity's friend class and went on to create a function using Entity's protected x, y, newx, newy fields and Wall's protected x, y, width, height fields.我需要解决碰撞问题,因此我将 Wall 称为 Entity 的友元类,然后使用 Entity 的受保护 x、y、newx、newy 字段和 Wall 的受保护 x、y、宽度、高度字段继续创建函数。

when compiling, the compiler tells me Entity's variables are protected, but from my understanding I should be able to use them if Wall is friend of Entity;编译时,编译器告诉我 Entity 的变量是受保护的,但根据我的理解,如果 Wall 是 Entity 的朋友,我应该可以使用它们;

Here are my classes: 1. Entity class这是我的课程: 1. 实体课程

class Shape
{

public:
    virtual void show(point offset = point(), bool doStroke = false) = 0;
    virtual void setColor(SDL_Color col) {color = col;}
    virtual void setStrokeColor(SDL_Color col) {strokeColor = col;}

protected:
    SDL_Renderer *gRenderer;
    SDL_Color color;
    SDL_Color strokeColor;
};
class ImageShape : public Shape
{
public:
    ImageShape();
    ImageShape(SDL_Renderer *&iRenderer, int iX, int iY);

    bool loadFromFile(std::string path);
    void show(point offset = point(), bool doStroke = false);
    void free();


protected:
    double x, y;
    int width, height;
    SDL_Texture* mTexture;

    double angle;
    SDL_RendererFlip flip;
};
class Entity : public ImageShape
{
    friend class Wall;
public:
    Entity();
    Entity(SDL_Renderer *&gRenderer, int iX, int iY, double iSpeed, double iMaxSpeed, double iJumpPower);
    point getCenterCoords();
    void collisionWall(Wall wall);
    void spin();

protected:
    int newx, newy;
    double dx, newdx, dy, newdy;
    double speed, maxSpeed;
    int jumpPower;
    bool currentlyJumping;

};

2.Wall class 2.墙类

class RectShape : public Shape
{

public:
    RectShape();
    RectShape(SDL_Renderer *iRenderer, int iX, int iY, int iWidth, int iHeight, SDL_Color iColor, SDL_Color iStrokeColor = {0x00, 0x00, 0x00, 0x00});
    void show(point offset = point(), bool doStroke = false);

protected:
    double x, y;
    int width, height;
};
class Wall : public RectShape
{

public:
    Wall();
    Wall(SDL_Renderer *iRenderer, int iX, int iY, int iWidth, int iHeight, SDL_Color iColor, SDL_Color iStrokeColor = {0x00, 0x00, 0x00, 0x00});
    void setState(bool state) {Enabled = state;}
    bool getState() {return Enabled;}

private:
    bool Enabled;
};

3.This is the collision function 3.这是碰撞函数

void Entity::collisionWall(Wall wall)
{
    //Collision right
    if(   x >= wall.x + wall.width &&
       newx <= wall.x + wall.width &&
       newy + height >= wall.y     &&
       newy <= wall.y + wall.height  )
    {
         if(newdx > -6)
           newdx = 0;
         newdx = -newdx;
         newx = wall.x + wall.width;
    }

    //Collision left
    if(    x + width <= wall.x     &&
        newx + width >= wall.x     &&
        newy + height >= wall.y    &&
        newy <= wall.y + wall.height )
    {
          if(newdx < 6)
           newdx = 0;
         newdx = -newdx;
         newx = wall.x - width;
    }

    //Collision top
    if(   y + height <= wall.y   &&
         newy + height >= wall.y   &&
         newx + width >= wall.x    &&
         newx <= wall.x + wall.width )
    {
          newdy = 0;
          newy = wall.y - height;
          if(currentlyJumping)
            currentlyJumping = false;
    }

   //Collision bot
    if(    y >= wall.y + wall.height &&
        newy <= wall.y + wall.height &&
        newx + width >= wall.x       &&
        newx <= wall.x + wall.width    )
          {
           newdy = -newdy / 4;
           newy = wall.y + wall.height;
          }
}

also, here are part of the errors I get:另外,这里是我得到的部分错误:

||=== Build: Debug in The jump, again (compiler: GNU GCC Compiler) ===|
 again\shape.h||In member function 'void Entity::collisionWall(Wall)':|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|51|error: within this context|
 again\shape.h|32|error: 'int RectShape::width' is protected|
 again\Entity.cpp|51|error: within this context|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|52|error: within this context|
 again\shape.h|32|error: 'int RectShape::width' is protected|
 again\Entity.cpp|52|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|53|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|54|error: within this context|
 again\shape.h|32|error: 'int RectShape::height' is protected|
 again\Entity.cpp|54|error: within this context|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|59|error: within this context|
 again\shape.h|32|error: 'int RectShape::width' is protected|
 again\Entity.cpp|59|error: within this context|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|63|error: within this context|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|64|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|65|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|66|error: within this context|
 again\shape.h|32|error: 'int RectShape::height' is protected|
 again\Entity.cpp|66|error: within this context|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|71|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|75|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|76|error: within this context|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|77|error: within this context|
 again\shape.h|31|error: 'double RectShape::x' is protected|
 again\Entity.cpp|78|error: within this context|
 again\shape.h|32|error: 'int RectShape::width' is protected|
 again\Entity.cpp|78|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|81|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|87|error: within this context|
 again\shape.h|32|error: 'int RectShape::height' is protected|
 again\Entity.cpp|87|error: within this context|
 again\shape.h|31|error: 'double RectShape::y' is protected|
 again\Entity.cpp|88|error: within this context|
 again\shape.h|32|error: 'int RectShape::height' is protected|
 again\Entity.cpp|88|error: within this context|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build failed: 50 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|

Argh, I got it.啊,我明白了。 Friending Wall meant that Wall could access Entity's protected variables. Friending Wall 意味着 Wall 可以访问实体的受保护变量。 I needed to friend Entity in Wall instead!我需要在 Wall 中与 Entity 成为朋友!

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

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