Re: sizeof() headaches

From: Lance (chen_weizhen_at_yahoo.com.sg)
Date: 12/23/03


Date: Tue, 23 Dec 2003 10:48:16 +0800

You can try using vector... Below is a modification example using vector:
//--------------------------------------------------------------------------
-
#include <string>
#include <iostream>
#include <vector>
using namespace std;

class A
{
public:
 void SetArray(vector<string> *arr)
 {
  strArr = arr;
  arrSize = strArr->size();
  cout<<"My Private Size= "<<arrSize<<endl;
 }
private:
 vector<string> *strArr;
 int arrSize;
};

void main(void)
{
 vector<string> vstr;
 vstr.push_back("first");
 vstr.push_back("second");
 vstr.push_back("third");
 vstr.push_back("fourth");
 vstr.push_back("fifth");

 A StrStore;
 StrStore.SetArray(&vstr);
}
//--------------------------------------------------------------------------
-
"Val" <valmont_gaming@hotmail.com> wrote in message
news:bs7pt9$5r9$1@reader10.wxs.nl...
> Observe this little code:
>
> #include <string>
> using namespace std;
> class A
> {
> public:
> void SetArray(string* array)
> {
> sArray = array;
> Size = sizeof array / sizeof *array; // does not work! "sizeof array"
> returns 4.... should be 7*16.
> }
> private:
> string* sArray;
> size_t Size;
> };
>
> int main()
> {
> string SomeStringArray[7] = {"word1","word2","word3",
> "word4","word5","word6","word7"};
>
> A Obj1;
> Obj1.SetArray(SomeStringArray);
>
> return 0;
> }
>
> Why isn't my sizeof operator not working like this?
> I need this to implement a private helper method wich calculates the
number
> of elements in sArray, buy using the sizeof operator if possible.
> What are my options (wich are reasonable)?
>
>



Relevant Pages

  • Re: Three simple questions
    ... using namespace std; ... struct Y { ... void initialize; ...
    (microsoft.public.vc.language)
  • Re: STL <list> declaration in header file
    ... > void getI; ... > using namespace std; ... You could get away with having these two lines in the source file *before* ...
    (comp.lang.cpp)
  • Three simple questions
    ... using namespace std; ... struct Y { ... void initialize; ...
    (microsoft.public.vc.language)
  • Re: The Sort Excercise
    ... using namespace std; ... void GeneralSort; ... class AscendSort: public SortStrategy ...
    (comp.object)
  • Re: Data Object Collection Inheritance
    ... > private System.Windows.Forms.ListBox listBox1;> private System.Windows.Forms.Button cmdID;> private System.Windows.Forms.Button cmdText; ... > private void InitializeComponent() ... > // TODO: Add BaseDataCollection.IsReadOnly getter implementation> return false; ... > public sealed class SubClassCollection: ...
    (microsoft.public.dotnet.languages.csharp)