简体   繁体   English

C ++ struct初始化

[英]C++ struct initialization

The following is an abridged version of my Sprite class: 以下是我的Sprite类的删节版本:

class Sprite
{
    struct SpriteState {
        Vector3 position;
        int width, height;
        double rotation, scaling;
    };
    std::map<int, SpriteState> stateVector;
}

I'd like to create a SpriteState object through a member function that looks something along the lines of the following: 我想通过一个成员函数创建一个SpriteState对象,该函数看起来像下面的行:

SpriteState newSpriteState(
        Vector3 position = stateVector.rbegin()->second.position, 
        int width = = stateVector.rbegin()->second.width, 
        int height = = stateVector.rbegin()->second.height, 
        double rotation = stateVector.rbegin()->second.rotation, 
        double scaling = stateVector.rbegin()->second.scaling)
    { SpriteState a; a.position = position; a.width = width; a.height = height; a.rotation = rotation; a.scaling = scaling; return a; }

I get the following error: 我收到以下错误:

a nonstatic member reference must be relative to a specific object 非静态成员引用必须与特定对象相关

The basic idea behind the class itself is to store the various states of the sprite as it changes so that I can easily recover to a previous state if needed. 类本身背后的基本思想是在精灵变化时存储精灵的各种状态,这样我就可以在需要时轻松恢复到以前的状态。

However, in most cases the Sprite is only updated with new position values while width, height, rotation, and scaling stays pretty much the same - which means I only change the position value while popping up and saving again references from the last state saved for the other values. 但是,在大多数情况下,Sprite仅使用新的位置值进行更新,而宽度,高度,旋转和缩放保持几乎相同 - 这意味着我只会在弹出时更改位置值并再次保存上次保存的状态的引用其他价值观。

I'd hence like to be able to set default values for the function so that I don't have to laboriously write the same values repeatedly. 因此,我希望能够为函数设置默认值,这样我就不必费力地重复写入相同的值。

Any possible ideas on implementation? 任何可能的实施想法?

Overload it: 超载它:

SpriteState newSpriteState(Vector3 position)
{
    return newSpriteState(
            position,
            stateVector.rbegin()->second.width, 
            stateVector.rbegin()->second.height, 
            stateVector.rbegin()->second.rotation, 
            stateVector.rbegin()->second.scaling)      
}

Default arguments are evaluated in the context of the caller, so if you need to access a member of your class to get the 'default' value, you can't use default arguments. 默认参数在调用者的上下文中计算,因此如果您需要访问类的成员以获取“default”值,则不能使用默认参数。

On the other hand, you can use overloading to get the same effect: 另一方面,您可以使用重载来获得相同的效果:

SpriteState newSpriteState(
    Vector3 position,
    int width,
    int height,
    double rotation,
    double scaling)
{ 
    SpriteState a; 
    a.position = position; 
    a.width = width; 
    a.height = height; 
    a.rotation = rotation; 
    a.scaling = scaling; 
    return a; 
}

SpriteState newSpriteState(
    Vector3 position)
{
    return newSpriteState(position,
                          stateVector.rbegin()->second.width, 
                          stateVector.rbegin()->second.height, 
                          stateVector.rbegin()->second.rotation, 
                          stateVector.rbegin()->second.scaling);
}
/* ... And similar for additional non-default values. */

You should make a copy of the SpriteState, and then modify it: 您应该复制SpriteState,然后修改它:

SpriteState newSpriteState(stateVector.rbegin()->second);
newSpriteState.width = someNewWidth;
return newSpriteState;

Every struct and class have by default a copy constructor of the following form: 默认情况下,每个结构和类都具有以下形式的复制构造函数:

ClassName(const ClassName&);

Which by default copies the data in the class/struct. 默认情况下,它会复制类/结构中的数据。

Member function can't call class members. 成员函数无法调用类成员。 You could archive it by doing: 你可以通过这样做来存档它:

SpriteState newSpriteState(SpriteState sprite_state)        
{ 
  SpriteState a; 
  a.position = position; 
  a.width = width; 
  a.height = height; 
  a.rotation = rotation; 
  a.scaling = scaling; 
  return a; 
}

Then you call this function in another member function : 然后在另一个member function调用此member function

newSpriteState(stateVector.rbegin()->second);

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

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