Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
防止在C+中以错误的顺序将值传递给函数/构造函数+; 我在JavaScript中学习C++和我的第一语言。在向函数传递大量参数时,防止bug的一种常见模式是传递对象。这样做的好处是,顺序无关紧要,值总是按照预期的方式读取_C++_Cmake - Fatal编程技术网

防止在C+中以错误的顺序将值传递给函数/构造函数+; 我在JavaScript中学习C++和我的第一语言。在向函数传递大量参数时,防止bug的一种常见模式是传递对象。这样做的好处是,顺序无关紧要,值总是按照预期的方式读取

防止在C+中以错误的顺序将值传递给函数/构造函数+; 我在JavaScript中学习C++和我的第一语言。在向函数传递大量参数时,防止bug的一种常见模式是传递对象。这样做的好处是,顺序无关紧要,值总是按照预期的方式读取,c++,cmake,C++,Cmake,例如: function checkStuff({param1, param3, param2}) {} checkStuff({param2, param3, param1}) 我发现自己在将参数传递给类构造函数时不断地混合参数的顺序。当然,当类型不匹配时,编译器和IDE会尖叫——当我的构造函数定义如下所示时,问题就开始了: Sprite(const std::string &textureFile, int animationSpeed, int frameWidth,

例如:

function checkStuff({param1, param3, param2}) {}

checkStuff({param2, param3, param1})
我发现自己在将参数传递给类构造函数时不断地混合参数的顺序。当然,当类型不匹配时,编译器和IDE会尖叫——当我的构造函数定义如下所示时,问题就开始了:

 Sprite(const std::string &textureFile, int animationSpeed, int frameWidth,
     int frameHeight, int width, int height, int scale)

在这个
add\u compile\u options(-Wall-Wextra-Wpedantic)
的基础上,是否有一个模式或另一个选项可以传递给cmake以防止混淆

更多的结构是解决问题的一种方法,也是对语言友好的一种方法。可以将动画速度、帧宽度、帧高度等组合到单个结构中。也许理想情况下,这种语言在这方面不会太尴尬,但我确实发现,一般来说,它很有用,不需要尝试将12个以上的参数传递到单个函数中

在我看来,
struct
也不错。。。或者使用对象传输多个参数字段时,使用所有公共字段(只是C++中的风格)。有时我在C++中遇到了这种过敏(就像人们想把私有实现树节点转换成奇特的类,有单独的公共/私有成员,当它们只是一个基本的C型结构),我认为如果你只是在C++中传输数据,就不应该有。 你知道,公共还是私人。区分“public”的全部目的是保护好代码块不受不可预测的用例的影响,以保持不变量。你保卫你的“王国”。你不必把一切都变成一个“王国”(不需要给农民装备板甲)。如果需要,只需使用类似C的结构来帮助实现“私有”实现细节“C++骑士是你设计的最大众化、广泛可访问的东西。就像你自己的私人部分一样。我的意思是我不想站在火车上露出裤裆,但我需要时不时地洗个澡,这就要求我赤身裸体。

我通常使用这种方法来防止错误的顺序,并更清楚地说明意图。可能会发现它有点冗长,但如果使用得当,它会使代码更具可读性,更不容易出错。你可以阅读这篇文章以获得更详细的解释

我只是分享一个简单的演示,如何为
Sprite
类显式表示构造函数参数的顺序和意义

#include <string>

template <typename T, typename param>
class strong_type
{
public:
     explicit strong_type ( T const& value ) : m_value ( value ) {}
     explicit strong_type ( T&& value = T{} ) : m_value ( std::move ( value ) ) {}
     T& get()
     {
          return m_value;
     }
     T const& get() const
     {
          return m_value;
     }
private:
     T m_value;
};

using animation_speed = strong_type<int , struct AnimationSpeedTag>;
using frame_width = strong_type<int , struct FrameWidthTag>;
using frame_height = strong_type<int , struct FrameHeightTag>;
using width = strong_type<int , struct WidthTag>;
using height = strong_type<int , struct HeightTag>;
using scale = strong_type<int , struct ScaleTag>;

class Sprite
{
    public:

    Sprite( const std::string &texture_file , animation_speed s , frame_width fw ,
        frame_height fh , width w , height h , scale sc ) 
        :   m_texture_file { texture_file }
        ,   m_animation_speed { s.get() }
        ,   m_frame_width { fw.get() }
        ,   m_frame_height { fh.get() }
        ,   m_width { w.get() }
        ,   m_height { h.get() }
        ,   m_scale { sc.get() }
        {   }

    private:

        // There is no harm to hold the values as primitive types
        // Just take it as `strong type`
        std::string m_texture_file;
        int m_animation_speed {};
        int m_frame_width {};
        int m_frame_height {};
        int m_width {};
        int m_height {};
        int m_scale {};
};

int main() {

    // instead of
    // Sprite my_sprite { "sprites/char.dds" , 12 , 640 , 480 , 320 , 240 , 1 };

    Sprite my_sprite { "sprites/char.dds" , animation_speed { 12 } , 
                    frame_width { 640 } , frame_height { 480 } ,
                    width { 320 } , height { 240 } , scale { 1 } };
}
#包括
模板
类强_型
{
公众:
显式强_类型(T const&value):m_值(value){}
显式强_类型(T&&value=T{}):m_值(std::move(value)){}
T&get()
{
返回m_值;
}
T const&get()const
{
返回m_值;
}
私人:
T m_值;
};
使用动画\u速度=强\u类型;
使用帧宽度=强类型;
使用框架高度=强类型;
使用宽度=强_类型;
使用高度=强_型;
使用比例=强_型;
雪碧
{
公众:
精灵(常量标准::字符串和纹理文件,动画速度s,帧宽度fw,
框架高度fh,宽度w,高度h,比例sc)
:m_纹理_文件{纹理_文件}
,m_animation_speed{s.get()}
,m_frame_width{fw.get()}
,m_frame_height{fh.get()}
,m_width{w.get()}
,m_height{h.get()}
,m_scale{sc.get()}
{   }
私人:
//将值作为基元类型保存不会有任何伤害
//就当它是“强壮型”吧`
std::字符串m_纹理_文件;
INTM_动画_速度{};
int m_帧宽度{};
int m_帧高度{};
int m_width{};
int m_高度{};
int m_标度{};
};
int main(){
//而不是
//雪碧我的雪碧{“sprites/char.dds”,1264048032032401};
精灵我的精灵{“精灵/char.dds”,动画速度{12},
框宽{640},框高{480},
宽度{320},高度{240},比例{1};
}

另一种方法是传递包含所需参数的结构,但我不建议这样做。因为事实上,它并没有解决问题,只是把它向前推进了一步。struct中的参数可能仍然未赋值

示范:

#include <string>

struct sprite_parameters
{
    std::string texture_file;
    int animation_speed {};
    int frame_width {};
    int frame_height {};
    int width {};
    int height {};
    int scale {};
};

class Sprite
{
    public:

    Sprite( const sprite_parameters& params ) 
        :   m_texture_file { params.texture_file }
        ,   m_animation_speed { params.animation_speed }
        ,   m_frame_width { params.frame_width }
        ,   m_frame_height { params.frame_height }
        ,   m_width { params.width }
        ,   m_height { params.height }
        ,   m_scale { params.scale }
        {   }

    private:

        std::string m_texture_file;
        int m_animation_speed {};
        int m_frame_width {};
        int m_frame_height {};
        int m_width {};
        int m_height {};
        int m_scale {};
};

int main() {

    // instead of
    // Sprite my_sprite { "sprites/char.dds" , 12 , 640 , 480 , 320 , 240 , 1 };
    sprite_parameters params;
    params.texture_file = "sprites/char.dds";
    params.animation_speed = 150;
    params.frame_width = 320;
    params.frame_height = 240;
    params.width = 640;
    params.height = 480;
    // params.scale = 1; OK, I forgot this, what will happen now. 
    // It is just ugly obscure bug.
    Sprite my_sprite { params };
}
#包括
结构sprite_参数
{
字符串纹理文件;
int动画速度{};
int帧_宽度{};
int帧_高度{};
整数宽度{};
整数高度{};
整数标度{};
};
雪碧
{
公众:
精灵(常量精灵参数和参数)
:m_texture_file{params.texture_file}
,m_animation_speed{params.animation_speed}
,m_frame_width{params.frame_width}
,m_frame_height{params.frame_height}
,m_width{params.width}
,m_height{params.height}
,m_scale{params.scale}
{   }
私人:
std::字符串m_纹理_文件;
INTM_动画_速度{};
int m_帧宽度{};
int m_帧高度{};
int m_width{};
int m_高度{};
int m_标度{};
};
int main(){
//而不是
//雪碧我的雪碧{“sprites/char.dds”,1264048032032401};
sprite_参数参数;
params.texture_file=“sprites/char.dds”;
params.animation_speed=150;
参数帧宽度=320;
参数框架高度=240;
参数宽度=640;
参数高度=480;
//params.scale=1;好吧,我忘了这个,现在会发生什么。
//这只是一个丑陋的模糊的错误。
精灵我的精灵{params};
}