Re: Arrays!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"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


.



Relevant Pages

  • no default constructor -- bad form?
    ... question (which a C++ question, not a windows question), but the class ... and to make an sense the class must know what the member is. ... But this puts me in the position of having no default constructor, ... I have to initialize x_ to something meaningful when ...
    (comp.lang.cpp)
  • Re: Function lookup tables?
    ... Ignore any typos the source is good except howto initialize the function ... > I want to initialize the function lookup table array to my member ... > function calls of the class inside of the constructor for the class. ... > What is the correct code to initialize the function pointer array ...
    (comp.lang.cpp)
  • Re: Question about arrays in objects
    ... >>I want to create a 2d array as an object member. ... > handled in the constructor, you just can't use a plain old array. ... > that is where using a pointer would come in. ...
    (alt.comp.lang.learn.c-cpp)
  • Member Initialization Gotcha!
    ... This only took a few hours to figure out after I finally went back to MSDN and saw this warning regarding member initialization: ... So you would think that two protected const DWORD members shown here would be constructed in its declared order: ... Well, never mind the above uses D1 to initialize D2, the above is drastically reduce code for illustration. ... It showed up when the original constructor: ...
    (microsoft.public.vc.language)
  • Re: cannot have instance field initializers in structs
    ... implement my own default constructor or to manually initialize field ... it would take to allocate my struct. ... if it takes an hour to initialize this ... > array of references and therefore the allocation is always a known thing. ...
    (microsoft.public.dotnet.languages.csharp)