Size of array object passed to function

后端 未结 4 685
臣服心动
臣服心动 2020-12-22 15:21

I am running the following code in C++:

class Tile {
    //class definition
};

bool myFunction(Tile* Tiles) {
    sizeof(Tiles);
    sizeof(Tiles[0]);
    /         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-22 16:01

    Arrays naturally decays to pointers, and when they do that all size information is lost. The most common solution is to pass along the number of elements in the array as an argument. It's also possible to use templates to deduce the array size.

    Or use std::array (or std::vector, depending on situation) instead, which is the solution I recommend.

提交回复
热议问题