Messing around with pass by reference, const and multidimensional vectors

This is a discussion on Messing around with pass by reference, const and multidimensional vectors within the C++ Programming forums, part of the General Programming Boards category; I have a pretty big piece of code that I isolated in a header + implementation. This code is now ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85

    Messing around with pass by reference, const and multidimensional vectors

    I have a pretty big piece of code that I isolated in a header + implementation. This code is now a function

    The whole code gets the data passed mostly by reference. The problem is that at some point, the function operates on a two dimensional vector of short int type, and this is what i came up with after several attemps:

    function x ( vector<vector < short int > >& pwm_sequence );

    Is this the right way to pass it?
    Also, what possible flaws this passing might bring? If I want it as a const, where do i pass the const?

    Thanks.
    Last edited by _izua_; 08-14-2007 at 01:49 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Uh..... So....... you need to have a vector that is filled with vectors that is filled with short ints?

    Anyway, put the const before the data type. Hence:

    Code:
    function x ( const vector<vector < short int > >& pwm_sequence );
    Since it's a reference, you can't change which variable the reference is referencing, hence you don't need to worry about the second const type (ie. it's not a pointer, so this is fine).

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually, the const keyword (and other type modifiers, such as volatile and mutable) can go before or after the data type, just as data type keywords can go in any order. That is, these are all equivalent:
    Code:
    const long int *x;
    long int const *x;
    int long const *x;
    long const int *x;
    However, standard practice is to put const and other similar keywords first, followed by the data type.

    Also note that const before an asterisk (*) or ampersand (&) in a pointer or a reference is not the same as a const afterwards. Before an asterisk, it makes a pointer-to-const (i.e., you can't modify the actual data that the pointer references); afterwards, and you get a constant pointer (i.e., you can't reassign the pointer to something else). The first form is most commonly used:
    Code:
    const int *x;
    There's rarely a need to use a constant pointer:
    Code:
    int *const x;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Also, what possible flaws this passing might bring?

    Passing it by reference is generally considered the proper way to do it. The reference should be const if you don't intend to modify the vector, which documents to the user of the function and to the compiler of that intention. Otherwise it should not be const. The only flaws are if you don't specify const but modify the data, or if you really need a copy of the data (which is rare), in which case you would not pass by reference.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21