Yuan Yijun (bbbush) wrote,
Yuan Yijun
bbbush

  • Mood:

const 和 typedef

今天学到的东西,就是 typedef 一个指针之后,再前置 const,const 的位置会是指针后面,也就是声明了常指针

// a.h
#pragma once
typedef const wchar_t* LPCWSTR;
const LPCWSTR a[] = { L"asdfasdf" };

// a.cpp
#include "a.h"

// b.cpp
#include "a.h"
int main(){ return 0;}

这里因为 LPCWSTR 没有被当做一个指针,因此按照规则 const 可以前置也可以后置。如果按照前置理解,const const wchar_t* 是没有意义的,因此在 gcc-c++ 和 m$-c++ 里面都按后置理解,也就是 const wchar_t* const。如果编译器知道 LPCWSTR 是一个指针,那么按照规则就不能修改 const 的位置了,可惜编译器不关心。

周叶铨同学,好久没有你的消息了。
Subscribe

  • 2020

    Last time I wrote a review was in 2018, about 2017. Or maybe I wrote something elsewhere, maybe on Twitter? It was hard to write something freely…

  • #thotcon badge?

    [root@m4700 ~]# esptool write_flash 0x00000 /home/yuan/tracking/thotcon0x9/tc0x9.bin esptool.py v2.3.1 Connecting.... Detecting chip type...…

  • Portfolio weighted averages

    Last week I got an assignment to make a small Java library for some often used portfolio calculations. A portfolio is a collection of holdings, each…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments