Default values for array arguments

前端 未结 6 829
孤独总比滥情好
孤独总比滥情好 2021-01-04 06:53

Just playing around a little with C++. What I really want to do is to be able to setup a function with default values defined for an array or pointer argument. To keep thing

6条回答
  •  梦毁少年i
    2021-01-04 07:20

    Your colleagues are wrong or maybe you misunderstood.

    The first clue to understanding is that you cannot have an array as a function parameter in C or C++. The reasons are historical. So when you write void experimentA(char a[3] ...) the compiler automatically converts it to a pointer, i.e. void experimentA(char* a ...). So the real question is why "abc" is a suitable default value for a and { 'a', 'b', 'c' } is not. The reason is as the compiler explains, "abc" is an expression and { 'a', 'b', 'c' } is not (its an initialiser). There are some places in C++ where you can use an initialiser and some where you can't. Default value for a parameter just happens to be one of the places you can't.

提交回复
热议问题