Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88221

Re: best way to pass a multiple dimension variable to a method

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: best way to pass a multiple dimension variable to a method
Date 2022-12-22 15:03 +0100
Organization A noiseless patient Spider
Message-ID <to1o46$1csmp$1@dont-email.me> (permalink)
References <tnvqqq$1466q$1@dont-email.me> <to00no$14ojn$1@dont-email.me> <32309bc9-1ff9-48c9-b404-849b285644e3n@googlegroups.com>

Show all headers | View raw


On 22/12/2022 03:03, Öö Tiib wrote:
> On Thursday, 22 December 2022 at 00:18:46 UTC+2, Lynn McGuire wrote:
>> On 12/21/2022 2:37 PM, Lynn McGuire wrote:
>>> What is the best way to pass a multiple dimension variable to a method
>>> in C++ ?
>>>
>>> For instance, in C you could do the following:
>>>
>>>      double xyz [5] [2] [14];
>>>      ...
>>>      amethod (xyz, ...);
>>>
>>> Thanks,
>>> Lynn
>> I am looking at
>> https://www.educba.com/3d-arrays-in-c-plus-plus/
>> and
>>
>> https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function
> 
> If the xyz is raw fixed dimension array then passing by reference is best as it
> is not losing any dimension information.
> void amethod(double (&xyz)[5][2][14], ...)
> 

I would suggest that any time you have fixed size arrays, you are better 
off using std::array<>.  Then the size is clearly fixed in the type, and 
there are no circumstances in which it "disappears" or the array decays 
into a pointer.  (As Ben pointed out in another thread, it's not always 
easy to tell when this happens.)  And you have container-style methods 
for the array if you want them, while all the time keeping the 
efficiency of a plain C array.

It can be a little ugly to use std::array's of std::arrays, so a 
wrapping class might help.  Or a template using :

template <class T, size_t x, size_t y, size_t z>
using array3d = std::array<std::array<std::array<T, z>, y>, x>;

array3d<double, 5, 2, 14> xyz;

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-21 14:37 -0600
  Re: best way to pass a multiple dimension variable to a method Richard Damon <Richard@Damon-Family.org> - 2022-12-21 17:10 -0500
  Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-21 16:18 -0600
    Re: best way to pass a multiple dimension variable to a method Öö Tiib <ootiib@hot.ee> - 2022-12-21 18:03 -0800
      Re: best way to pass a multiple dimension variable to a method David Brown <david.brown@hesbynett.no> - 2022-12-22 15:03 +0100
    Re: best way to pass a multiple dimension variable to a method Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-22 03:02 +0000
      Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-22 02:01 -0600
        Re: best way to pass a multiple dimension variable to a method Öö Tiib <ootiib@hot.ee> - 2022-12-22 00:25 -0800
        Re: best way to pass a multiple dimension variable to a method Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-22 12:13 +0000
  Re: best way to pass a multiple dimension variable to a method Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-22 09:07 +0200
  Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-22 22:10 -0600
  Re: best way to pass a multiple dimension variable to a method Lynn McGuire <lynnmcguire5@gmail.com> - 2022-12-22 22:23 -0600

csiph-web