I have a pointer to array of objects. It looks like:
MyClass *myClass[ 10 ];
myClass[ 0 ] = new MyClass(); // init for each of myClass[0..9]
myClass[ 0 ]->field1 = "hello";
How can I pass "myClass" to a function by reference? I tried a few cases but it didn't work.
If you really must use an array, then
template<size_t N >
void foo(MyClass (&arr)[N] )
{
// Access arr[i], size is N
}
...
foo(myClass);
Otherwise, use an std::array
template<size_t N >
void foo(std::array<MyClass,N>& arr )
{
// Access arr[i], size is N or arr.size()
}
...
std::array<MyClass, 10> myClass = ....;
foo(myClass);
I would not call an array "myClass" though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.