2011年11月14日 星期一

const的意義(const pointer, pointer to const, const pointer to a const, const function...)

#include
#include
using namespace std;
class A
{
private:
     int y;
public:
 A(){
  y=0;
 }
 int f1(int x) const // 此const代表此function不可以修改Data Member
 {
  x++;
  // y++; // Error! y為Data Member,因此不可以修改
  return y;
 }
 const int f2(int x) // 回傳值為const
 {
  x++;
  y++;
  return y;
 }
 int f3(int const x) // 參數為const
 {
  // x++; // Error! x為const,不可以修改
  y++;
  return y;
 }
 void f4(int const *x) // x is variable pointer to a constant integer,也可以寫成f4(const int *x)
 {
  // (*x)++; // Error! pointer x所指向的實體為const,因此不可以修改其值
  *x++; // 此行的意思是*(x++),先修改pointer x的值,在指向實體
 }
 void f5(int const &x) // x is variable reference to a constant integer
 {
  // x++; // Error! x就是一個const
 }
 void f6(int *const x) // x constant pointer to a variable integer
 {
  (*x)++;
  // *x++; // Error! Constant pointer x 不可以指向其他實體
 }
 const char * f7() // 回傳一個variable pointer to a constant char
 {
  return "ABC";
 }
 void f8(const int * const x) // x is constant pointer to a const integer
 {
  // (*x)++; // Error! x所指向的實體為const,因此不可以修改其值
  // *x++; // Error! Constant pointer x 不可以指向其他實體
 }
};
int main(){
 A a;
 int x=3;
 a.f1(x);
 a.f2(x);
 a.f3(x); // int x 會自動轉成const int x傳入
 a.f4(&x) ;
 a.f5(x) ;
 const char *s = a.f7(); // 必須使用一個const來接const
 // s[0]='0'; // Error! 無法對const 變數進行改變動作
 return 0;
}

Reference: http://csie-tw.blogspot.com/2010/03/c-constconst-pointer-pointer-to-const.html

2011年11月9日 星期三

How to use cpplint.py

First, you should install python

Second, download cpplint.py

Third, run below script in command line

cpplint.py –output=vs7 helloworld.cpp

Then you can get the result.

2011年11月4日 星期五

Struct for sockaddr,sockaddr_in,sockaddr_in6,addrinfo

struct sockaddr {
      unsigned short sa_family;        /* address family, AF_xxx */
      char sa_data[14];                /* 14 bytes of protocol address */
};

struct sockaddr_in {
      short int sin_family;            /* Address family AF_INET */
      unsigned short int sin_port;     /* Port number */
      struct in_addr sin_addr;         /* Internet address */
      unsigned char sin_zero[8];       /* Same size as struct sockaddr */
};
struct in_addr {
      unsigned long s_addr;            /* Internet address */
};

struct sockaddr_in6 {
      sa_family_t sin6_family;         /* AF_INET6 */
      in_port_t sin6_port;             /* transport layer port # */
      uint32_t sin6_flowinfo;          /* IPv6 traffic class & flow info */
      struct in6_addr sin6_addr;       /* IPv6 address */
      uint32_t sin6_scope_id;          /* set of interfaces for a scope */
};
struct in6_addr {
      uint8_t s6_addr[16];             /* IPv6 address */
};

struct addrinfo{
      int ai_flags;                    /* AI_PASSIVE,AI_CANONNAME,AI_NUMERICHOST */
      int ai_family;                   /* AF_INET,AF_INET6 */
      int ai_socktype;                 /* SOCK_STREAM,SOCK_DGRAM */
      int ai_protocol;                 /* IPPROTO_IP, IPPROTO_IPV4, IPPROTO_IPV6 */
      size_t ai_addrlen;               /* Length */
      char *ai_cannoname;              /* */
      struct sockaddr *ai_addr;        /* struct sockaddr */
      struct addrinfo *ai_next;        /* pNext */
};