MISRA-C 2004 规则解读(241S-260S)


241 S:Start of class/struct/union/enum lower case. 建议结构体和联合体命名以大写字符开头。

222 S:Start of variable is upper case. 建议变量名以小写字符开头。

223 S:No prefix for global function. 作用域为全局的函数名建议以_func结尾以增加代码的可读性。

224 S:Start of enumeration is upper case. 建议常量和枚举类型的变量以小写字符开头。

225 S:Inappropriate file extension. C的源文件类型为.c,C++源文件类型为.cpp。

226 S:Bit field is not octal, hex or suffix u.位字段建议是八进制、十进制或无符号型:

enum {won=1,too=2};

struct tag{
    UINT dec:2;     /* not compliant */
    UINT oct:02;
    UINT hex:0x3;
    UINT enumr:won; /* not compliant */
    UINT decu:3u;
};

227 S:Numeric bit operand is not octal,hex or u. 位操作建议是八进制、十进制或无符号型:

void static_227 (void)
{
    UINT x=1, y;
    y = x & 20;   /* not compliant */
    y = x & 20u;
    y = x & 020;
    y = x & 0x20;
}

228 S:Bracket mismatch ( or { in macro definition. 宏定义中缺少( 或者{:

/* Missing right parenthesis (!) in this macro definition of a 'common' symbolic constant */ 

#define COMMON_SYM_CONSTANT ((2U * 3U) + 2U

229 S:Bracket mismatch ) or } in macro definition. 宏定义中缺少) 或者 }:

/* Missing left parenthesis (!) in this macro definition of a 'common' symbolic constant */ 

#define COMMON_SYM_CONSTANT 2U * 3U + 2U)

230 S:No copy constructor defined for class. 尽管有默认的构造函数,建议自己实现类的构造函数。

231 S:No assignment operator defined for class. 同230S,建议自己实现构造函数并对变量初始化。

232 S:No destructor defined for class.同230S,建议自己实现析构函数。

233 S:No copy constructor for class with pointers.同230S,建议自己实现拷贝构造函数。

234 S:No assignment operator for class with pointers.同231S。

235 S:No destructor for class with pointers. 同232S。

236 S:New used in class without assignment op. 类中使用new分配空间时,建议定义拷贝赋值运算符:

class Person
{
  public:
    Person();
    explicit Person(const UINT_32 personNum);
    explicit Person(const Person &person);
    ~Person();
  protected:
  private:
    UINT_32 personalNumber;
    UINT_32 *skillsList;
};

Person::Person(const UINT_32 personNum)
{
  personalNumber = personNum;
  skillsList = new UINT_32[10];  /* not compliant */
}


static void static_236(void)
{
  Person p1(10);
  Person q1(10);
  q1 = p1;  /* invokes (compiler provided) copy assignment constructor */
} 

237 S:Assignment operator parameter not const. 拷贝赋值运算符被用来将一个对象中的值拷贝到同类型的另一个对象中,该对象应当为const类型,不被改变:

class Person
{
  public:
    Person();
    explicit Person(const Uint_32 personNum);
    explicit Person(const Person &person);
    Person & operator=(Person &person);  // should be a const parameter 
    ~Person();
  protected:
  private:
    Uint_32 personalNumber;
};

Person &Person::operator=(Person &person)
{
  if (this != &person)
  {
    // Assignment code 
  }
  return *this;
}


void foo(void)
{
  Person p1(10);
  Person q1(10);
  q1 = p1;
}

238 S:Number of templates exceeds N.建议减少使用模版:

template < class T >
class tplate1
{
  public:
    tplate1();
  protected:
  private:
    Uint_32 value1;
};

template < class T >
class tplate2
{
  public:
    tplate2();
  protected:
  private:
    Uint_32 value2;
};

template < class T >
class tplate3
{
  public:
    tplate3();
  protected:
  private:
    Uint_32 value3;
};

template < class T >
class tplate4
{
  public:
    tplate4();
  protected:
  private:
    Uint_32 value4;
};

template < class T >
class tplate5
{
  public:
    tplate5();
  protected:
  private:
    Uint_32 value5;
};

template < class T >
class tplate6
{
  public:
    tplate6();
  protected:
  private:
    Uint_32 value6;
};

template < class T >
class tplate7        // This declaration causes a violation when
{                    // limit configured to 6. 
  public:
    tplate7();
  protected:
  private:
    Uint_32 value7;
};


void foo(void)
{
  // ...
}

239 S:New used in class without copy constructor. 如果类中使用new分配空间,同时建议定义拷贝构造函数:

class Person
{
  public:
    Person();
    explicit Person(const UINT_32 personNum);
    Person & operator=(const Person &person);
    ~Person();
  protected:
  private:
    UINT_32 personalNumber;
    UINT_32 *skillsList;
};

Person::Person(const UINT_32 personNum)
{
  personalNumber = personNum;
  skillsList = new UINT_32[10];  /* not compliant */
}


static void static_239(void)
{
  Person p1(10);
  Person q1(p1);  /* invokes (compiler provided) copy constructor */
}

240 S:Use of dynamic_cast. 建议减少使用动态类型分配:

class Base
{
  public:
    Base();
    explicit Base(const Base &base);
    Base& operator=(const Base &base);
    virtual ~Base();
  protected:
  private:
};

class Derived : public Base
{
  public:
    Derived();
    explicit Derived(const Derived &derived);
    Derived& operator=(const Derived &derived);
    virtual ~Derived();
  protected:
  private:
};

class DifferentDerived : public Base
{
  public:
    DifferentDerived();
    explicit DifferentDerived(const DifferentDerived &dderived);
    DifferentDerived& operator=(const DifferentDerived &dderived);
    virtual ~DifferentDerived();
  protected:
  private:
};


static void static_240(void)
{
  Derived *d1 = new Derived;
  DifferentDerived *dd1 = dynamic_cast <DifferentDerived *> (d1); /* not compliant */
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值