Re: sizeof() headaches
From: Lance (chen_weizhen_at_yahoo.com.sg)
Date: 12/23/03
- Next message: Paul Hsieh: "Re: extract string from file"
- Previous message: James Connell: "Re: sizeof() headaches"
- In reply to: Val: "sizeof() headaches"
- Next in thread: Guy Harrison: "Re: sizeof() headaches"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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)?
>
>
- Next message: Paul Hsieh: "Re: extract string from file"
- Previous message: James Connell: "Re: sizeof() headaches"
- In reply to: Val: "sizeof() headaches"
- Next in thread: Guy Harrison: "Re: sizeof() headaches"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|