PC-LINT检查的编译问题

1.

class A

{

    char name[10];

    public:

    const char* getA() const     //一定要有const,因为name是一个数组名即常量指针

    {

        return name;

    }    

}

2.

class TServiceTypeConfigItem
{
public:

    char ServiceType[MaxServiceTypesLength+1];
    char serviceTypeIp[MaxIPAddressLength+1];  
    TServiceTypeConfigItem()
    {
        memset(ServiceType, 0, sizeof(ServiceType));
        memset(serviceTypeIp,0,sizeof(serviceTypeIp));
    }

public:
    const char* getServiceType() const { return ServiceType;}
    bool setServiceType(PCSTR ServiceType)
    {
        safeCopyStr(this->ServiceType,ServiceType,MaxServiceTypesLength);
        return true;  
    }
    const char* getServiceTypeIp() const { return serviceTypeIp;}
    bool setServiceTypeIp(PCSTR serviceTypeIp)
    {
        safeCopyStr(this->serviceTypeIp,serviceTypeIp,MaxIPAddressLength);
  return true;
    }

};

 

class xxx

{

    TServiceTypeConfigItem m_ServiceTypeConfigItem[MAX_IPTOSERVICETYPE_NUM];//是一个数组类型

public:

    const TServiceTypeConfigItem*  getServiceTypeConfigItem(int index) const//这里必须是const,因为是数组名加偏移
    {
        if ((index < 0) || (index > m_IpToServiceTypeNum))
        {return NULL;}

        return m_ServiceTypeConfigItem+ index;
    }


}

const_cast(getServiceTypeConfigItem(i))->getServiceType()//这里返回的是const型的,再调用getgetServiceType函数就错了,因为const型对像或者指针不能调用非const型函数,所以要去掉const属性。

 

你可能感兴趣的:(pc-lint,pc-lint)