Re: Arrays!
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Tue, 23 Aug 2005 23:09:30 -0400
"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B2EC54AC-099F-4906-8B67-1A968A511880@xxxxxxxxxxxxx
> Here is what I am trying to test myself with. I would like to
> innitialize a two dimensional array in my constructor of my class.
> However I would like to have access to the data in the array from
> anywhetre within my accessor functions.
> However, I find myself innitalizing it twice. Once in the private
> section of my class and once in the constructor.
>
> #include <iostream>
> using namespace std;
>
>
> class TABLES
> {
> public:
> TABLES();
> ~TABLES(){}
> int GetT2Value(int CSize); const //{return T2Value;}
> int GetT13Value(int AmpOfCond) const {return T13Value;}
>
> private:
> int T2Value;
> int T13Value;
> int T2[28][2];
>
> };
>
> TABLES::TABLES()
> {
> int T2[28][2] = {{14,15},{12,20},{10,30},{8,45},{6,65},
> {4,85},{3,105},{2,120},{1,140},{0,155},{20,185},{30,210},
> {40,235},{250,265},{300,295},{350,325},{400,345},{500,395},
> {600,455},{700,490},{750,500},{800,515},{900,555},{1000,585},
> {1250,645},{1500,700},{1750,735},{2000,775}};
> };
In the constructor, you declare and initialize a local variable named
T2 that has abolutely nothing in common with the member variable
TABLES::T2. In your accessor functions, you will find that TABLES::T2
remains uninitialized.
There is no syntax in C++ to initialize an array that is a member
variable of some class. You can give it initial values with a series of
assignments:
TABLES::TABLES()
{
T2[0][0] = 14;
T2[0][1] = 15;
// ...
};
Or, you can have a static array with initial values, and initialize your
member by copying:
namespace
{
const int T2Init[28][2] = {{14, 15}, ...};
}
TABLE::TABLE()
{
memcpy(T2, T2Init, sizeof(T2));
}
In fact, it appears that your code uses T2 as read-only - you never seem
to assign new values to its elements. If this is the case, you can have
T2 as a static member of the class (so that all instances of TABLES
share a single copy of it), then you can initialize it with the usual
syntax:
class TABLES
{
static const int T2[28][2];
};
const int TABLES::T2[28][2] = {{14, 15}, ...};
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- References:
- Arrays!
- From: Robby
- Arrays!
- Prev by Date: Arrays!
- Next by Date: Re: VerQueryValue returning NULL for JNICALL
- Previous by thread: Arrays!
- Next by thread: Re: Arrays!
- Index(es):
Relevant Pages
|