Location of C + + const placement

Source: Internet
Author: User

The standard int const A is exactly equivalent to the const int A. Because of this, there will be a lot of different styles, the same "* is the type or variable?" ", such as char* P and Char *p, they are equivalent.

Is it using the const t& T or T const& t?

Do you use int* A or int *a?

When I read the "C + + templates Chinese version", it specifically mentions the use of the definition of the Int const, the function parameter definition is also this write

T AddValue (t const& x) {

//

}

If it is of type string, it is written like this, String const& str

I feel weird about this, especially if most of the C + + I see is not defined in this way. For example, I see V8 's source code is generally defined as:

Static BOOL Enableagent (const charint port);

Static void SendCommand (constint length,
clientdata* client_data = NULL);

/**
* Allocates a new string from either UTF-8 encoded or ASCII data.
* The second parameter ' length ' gives the buffer length.
* If The data is UTF-8 encoded, the caller must
* Be careful to supply the length parameter.
* IF It is not given, the function calls
* ' strlen ' to determine the buffer length, it might be
* Wrong if ' data ' contains a null character.
   */
StaticLocal<string> New (ConstChar* Data,intlength =-1);

/** allocates a new string from UTF16 data.*/
StaticLocal<string> New (Constuint16_t* data,intlength =-1);

/** creates a symbol. Returns one if it exists already.*/
StaticLocal<string> Newsymbol (ConstChar* Data,intlength =-1);

I see Aboutspeaker Teacher's Muduo source code is also the above style:

//https://github.com/chenshuo/muduo/blob/master/muduo/net/socket.cc
voidSocket::bindaddress (Constinetaddress& addr)
{
Sockets::bindordie (Sockfd_, Addr.getsockaddrinet ());
}

voidSocket::listen ()
{
Sockets::listenordie (Sockfd_);
}

intSocket::accept (inetaddress* peeraddr)
{
structSockaddr_in addr;
Bzero (&AMP;ADDR,sizeofaddr);
intCONNFD = Sockets::accept (Sockfd_, &addr);
if(CONNFD >= 0)
{
Peeraddr->setsockaddrinet (addr);
}
returnCONNFD;
}

And I see Lua's source code (c-language), which is defined as:

StaticintHandle_luainit (lua_state *l) {
ConstChar*init = getenv (lua_init);
if(init = = NULL)return0;/ * Status OK * /
Elseif(Init[0] = =' @ ')
returnDofile (L, init+1);
Else
returnDostring (L, Init,"="Lua_init);
}


structSmain {
intargc
Char**ARGV;
intStatus
};


StaticintPmain (lua_state *l) {
structSmain *s = (structSmain *) Lua_touserdata (L, 1);
Char**ARGV = s->argv;
intScript
inthas_i = 0, Has_v = 0, has_e = 0;
Globall = L;
if(Argv[0] && argv[0][0]) progname = argv[0];
LUA_GC (L, lua_gcstop, 0);/ * Stop collector during initialization * /
Lual_openlibs (L);/ * Open Libraries * /
LUA_GC (L, Lua_gcrestart, 0);
S->status = Handle_luainit (L);
if(S->status! = 0)return0;
Script = Collectargs (argv, &has_i, &has_v, &has_e);
if(Script < 0) {/ * Invalid args? * /
Print_usage ();
S->status = 1;
return0;
}
if(Has_v) print_version ();
S->status = Runargs (L, argv, (script > 0)? script:s->argc);
if(S->status! = 0)return0;
if(script)
S->status = Handle_script (L, argv, script);
if(S->status! = 0)return0;
if(has_i)
Dotty (L);
Elseif(script = = 0 &&!has_e &&!has_v) {
if(Lua_stdin_is_tty ()) {
Print_version ();
Dotty (L);
}
ElseDofile (L, NULL);/ * Executes stdin as a file * /
}
return0;
}

The above is obviously a different style of C and C + +. Later through the search, found in the Wiki encyclopedia has some detailed description of the const:

Https://en.wikipedia.org/wiki/Const_ (computer_programming)

But with the type or the pointer variable, I find that there are two different forms of the same CPP in Facebook:

Https://github.com/facebook/hhvm/blob/master/hphp/compiler/builtin_symbols.cpp

Cocos2d-x's official source code also has this problem:

Https://github.com/cocos2d/cocos2d-x/blob/v3/cocos/2d/CCLayer.cpp

C in the customary int *ptr, indicating that *ptr is an int type, PTR is a pointer to the specified int

int const *PTR Description *ptr is a constant, a constant of type int

int * Const PTR PTR is a constant, a constant of a pointer type (int *)

C + + Associates A * with a type, such as int* A, where the C + + style is present. In C + + in order to avoid doubts, although the const int* p and int const* p equivalent, but in order to prevent the int * Const p too similar, I think it is more appropriate to use the const int* p (semantically int const expression more suitable, Indicates that the const is a qualified int). int* const p to define pointer constants.

The meaning of the expression: * (pointer) and const (constant qualifier) who first read before, * symbolizes the address, the const symbolizes the content.

const INT * p constant pointer, p is pointer, pointer to constant.

Constant const in the previous * pointer after, const-qualified *, so cannot be modified with *p = XX, but can be modified by P = &b to modify the value of the pointer variable.

int* const p Pointer constant, p is a pointer, is a pointer to a constant, the address is not allowed to be modified, so P = &b is illegal, but you can change the value it points to *p = B;

P is always the pointer, assign a value to the pointer using the & address operator, if Const before p, it is a (constant) pointer, its value is not changeable

Recently in the "C + + programming thought" in a separate chapter to write a const, just look at a bit on the Lenovo before the "C + + template Chinese" after some confusion. Now that it's clear, it feels more relaxed. It is said that C and C + + in the processing of const is still different, here I have not fully understood, and so on completely obviously may write another article.

Location of C + + const placement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.