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

Recent Posts from This Journal

  • BP 1st year

    Basic Program is a reading program from UChicago. The information can be found here [1] and currently it is open for signing up for the next year…

  • 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…

  • Crime and Punishment

    As a step to improve the illiterate situation I joined the Basic Program this year. It had been in my mind for a couple years, but glad this time I…

  • 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