Something trivial. Type alias and const modifier

I found something interesting when I was writing a simple homework.

struct Node
{
    string key;
    int value;
    
    Node *next;
    
    Node(const string &theKey, int theValue):key(theKey), value(theValue), next(nullptr){}
    
    Node(const string &theKey, int theValue, Node *n):key(theKey), value(theValue), next(n){}
};

typedef Node * ListType;

ListType listFind(const ListType &list, const string &target)
{
    if(!list) return NULL;
    ListType cur = list;
    while(cur)
    {
        if(cur->key == target)
            return cur;
        cur = cur->next;
    }
    return NULL;
}

bool listInsert(ListType &list, const ListType &target)
{
    if(!listFind(list, target->key))
    {
        target->next = list;</span>
        list = target;
        return true;
    }
    return false;
}

void listPrint(const ListType &list, ostream &out)
{
    ListType cur = list;
    while (cur)
    {
        out << cur->key << ":" << cur->value << ' ';
        cur = cur->next;
    }
    out << endl;
}

int main()
{
    Node *A = new Node("Melody", 100);
    listInsert(A, new Node("Luo", 99));
    listPrint(A, cout);
    return 0;
}

Please take care of this sentence:

target->next = list;

Nothing special at first glance. Yet have you noticed the parameter type:

const ListType &target

Yes, it is modified by a 'const'. How are we able to change the content anyway?

Well, the reason is that, 'const ListType &target' is different from 'const Node *&target'. The first one is a 'top-level const', i.e. the pointer itself is const. The latter means that the content pointed by the pointer is const, aka a 'low-level const'.

Typically we are not allowed to pass a rvalue to a non-const parameter, because after all it's allowed and very likely not our desire to change the content of a rvalue. That's why we need a const modifier. Yet a type alias breaks the rule to some extent, for here we are able to change the content anyway. That's the reason why I don't prefer type alias.

One more thing.

int *a = new int(0); const int *b = a;  //OK

const int *&c = a; //WRONG, the compiler complains that 

on-const lvalue reference to type

      'const int *' cannot bind to a value of unrelated type

      'int *'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值