Size of array object passed to function

后端 未结 4 683
臣服心动
臣服心动 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

    Answering my own question:

    Why I'm posting this answer

    I was new to C++ at the time, and other questions have been marked as a duplicate of this. As such, I'm guessing that other questions like this are from C++ newbies. This answer will be for the benefit of those newbies.

    My answer

    Unlike in higher-level languages like Python or JAVA, the length of an array is not stored with the contents of the array, so there is no way to access it.

    As such, it must be passed as a separate parameter. It may not make much sense in higher-level languages, but it's necessary in both C and C++.

    Also, unlike in Python and JAVA, an array is not an object. To fully understand C/C++ arrays, one first must understand pointers.

提交回复
热议问题