Re: sizeof() headaches

From: Jeff Schwab (jeffplus_at_comcast.net)
Date: 12/23/03


Date: Mon, 22 Dec 2003 23:08:15 -0500

Jack Klein wrote:
> On Tue, 23 Dec 2003 01:06:51 +0100, "Val" <valmont_gaming@hotmail.com>
> wrote in alt.comp.lang.learn.c-c++:
>
>
>>What do you mean?
>>I don't want to pass the array size to the class. Because later there will
>>be a private member function to determine the size.
>>The size is going to be used in a loop for a sorting routine by the way, but
>>that aside.
>>So how do I do such a thin coding wise, assuming the default code presented
>>in my first post.
>
>
> You CAN'T do such a thing "coding wise". When you pass an array to a
> function, it is converted to a pointer to its first element. There is
> no size information passed, unless you pass it manually.
>
> Pass the size as another parameter, and use it to initialize a private
> member that can be queried by the private member function.
>

Them's some strong words... It would be one thing to say you can't do
it, but to say you CAN'T, well...

#include <iostream>
#include <string>

using namespace std;

class A
{
public:

      template< typename Array >
      void SetArray(Array& array)
      {
          sArray = array;

          Size = sizeof array / sizeof *array;

          cout << Size << endl;
      }

private:

      string* sArray;
      size_t Size;
};

int main()
{

      string SomeStringArray[ 7 ] =
          { "word1", "word2", "word3", "word4",
            "word5", "word6", "word7" };

      A Obj1;
      Obj1.SetArray(SomeStringArray);
}



Relevant Pages

  • Re: sizeof() headaches
    ... > I don't want to pass the array size to the class. ... > in my first post. ... You CAN'T do such a thing "coding wise". ... member that can be queried by the private member function. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: sizeof() headaches
    ... |>>I don't want to pass the array size to the class. ... |>>in my first post. ... |> You CAN'T do such a thing "coding wise". ... |> member that can be queried by the private member function. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: sizeof() headaches
    ... I don't want to pass the array size to the class. ... be a private member function to determine the size. ... do I HAVE to use a different data structure? ... >> stick to the string type. ...
    (alt.comp.lang.learn.c-cpp)