Returning two-dimensional array in C++

This is a discussion on Returning two-dimensional array in C++ within the C++ Programming forums, part of the General Programming Boards category; You should avoid using magic numbers . You mentioned that you want to return the array so that you can ...

  1. #31
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    You should avoid using magic numbers.

    You mentioned that you want to return the array so that you can check it; Well a much better way to do this would be to make a public method to do the checking for you, thus simplifying implementation code as well as putting the array checking in its proper place. This concept is sometimes called encapsulation or information hiding.
    eg:
    Code:
    ...
    public:
        int checkArray(); // Returns some status code
    ...

  2. #32
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Quote Originally Posted by Elysia View Post
    If you want Java approach, it's easy if you use std::vector. You can return std::vector, so essentially, you can return an array that way.
    Hi Elysia,
    This would work well and all, but it really isn't going to help them understand why you can't "return a 2d array" in C++.

    When people learn C++ they generally learn about how strings are represented in memory as null-terminated byte arrays before they learn to use the std::string class, right? Same idea here, IMO.

  3. #33
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    103
    Okay folks, thanks a lot for your time, and the wonderful replies.
    laserlight's solution worked fine, so that was all I needed, thanks a ton



    To Elysia: I'm sorry I couldnt adress you earlier, I was kinda busy.
    I am not anti-vector. Using vectors would surely pose no problem for me for the use I want, and it was an excelent suggestion.

    However, I would also like to know how to make it without vectors, in case I would ever need to do that. For example because of memory or speed concerns.
    And just out of curiosity. I've been trying for hours to get it to work, so I wanted to see the "correct" solution.

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> When people learn C++ they generally learn about how strings are represented in memory as
    >> null-terminated byte arrays before they learn to use the std::string class, right?

    If they are they aren't being taught in the most effective way. They should be taught strings first (and vectors).

  5. #35
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Quote Originally Posted by BMJ View Post
    When people learn C++ they generally learn about how strings are represented in memory as null-terminated byte arrays before they learn to use the std::string class, right? Same idea here, IMO.
    Oh, but was not under impression that it was to learn, but rather to get it working with a good solution.

    Quote Originally Posted by Drogin View Post

    To Elysia: I'm sorry I couldnt adress you earlier, I was kinda busy.
    I am not anti-vector. Using vectors would surely pose no problem for me for the use I want, and it was an excelent suggestion.

    However, I would also like to know how to make it without vectors, in case I would ever need to do that. For example because of memory or speed concerns.
    And just out of curiosity. I've been trying for hours to get it to work, so I wanted to see the "correct" solution.
    I see.
    But you could have mentioned it earlier.
    Anyway, no harm done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #36
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by drogin
    However, I would also like to know how to make it without vectors, in case I would ever need to do that. For example because of memory or speed concerns.
    And just out of curiosity. I've been trying for hours to get it to work, so I wanted to see the "correct" solution.
    you can do it with multiple layers of indirection.

  7. #37
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Quote Originally Posted by Daved View Post
    >> When people learn C++ they generally learn about how strings are represented in memory as
    >> null-terminated byte arrays before they learn to use the std::string class, right?

    If they are they aren't being taught in the most effective way. They should be taught strings first (and vectors).
    I suppose. The C++ classes I've assisted have always been for people with no prior programming experience, sometimes I forget that not everybody is in the same situation.

  8. #38
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> sometimes I forget that not everybody is in the same situation.

    Either way, they should be taught strings first, IMO. Stroustrup and many other C++ experts agree.

  9. #39

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    In my opinion, it would make more sense to use regular getter functions that provide more indirect access to the array, instead of turning a reference to the whole private array itself. This promotes better encapsulation.

    Code:
    class MyClass
    {
          public:
                int array[5][5];
          public:
                MyClass() { /* ... */ }
    
                int& getArray(std::size_t index_x, std::size_t index_y)
                { return array[index_x][index_y]; }
    
                /* And a const version: */
                const int& getArray(std::size_t index_x, std::size_t index_y) const
                { return array[index_x][index_y]; }
    };
    
    int main()
    {
          MyClass obj;
          const MyClass c_obj;
    
          obj.getArray(0,0) = 1;
          c_obj.getArray(0,0) = 1; // Error: assignment of read-only location
    }
    You can even use the getter functions to do bounds-checking when accessing the array, to make code safer.
    Last edited by rudyman; 06-18-2008 at 06:44 PM.

  10. #40
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by rudyman View Post
    In my opinion, it would make more sense to use regular getter functions that provide more indirect access to the array, instead of turning a reference to the whole private array itself. This promotes better encapsulation.
    But only marginally. Clients of the getters depend on the implementation detail of a 2d array. If and when that implementation changes, it breaks code that uses the getter.

  11. #41
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Quote Originally Posted by Daved View Post
    >> sometimes I forget that not everybody is in the same situation.

    Either way, they should be taught strings first, IMO. Stroustrup and many other C++ experts agree.
    Where does Stroustrup explain this because I'd genuinely love to have more insight.

    Thanks.

  12. #42
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Learning Standard C++ as a New Language

    >> Thanks.
    Your welcome.
    Last edited by Daved; 06-18-2008 at 08:43 PM.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. problem returning array from function(among others)
    By Calef13 in forum C++ Programming
    Replies: 30
    Last Post: 10-30-2006, 03:26 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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