initializing an array in a class object
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: initializing an array in a class object

  1. #1
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    initializing an array in a class object

    I am trying to initialize an array (julian days by month) when an instance of a class object is created. I have no idea how to do this. have tried the standard approach of initialization as done in global or local variable declarations within the class:

    class myclass
    {
    long int juliandays[11] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

    public
    myclass();
    }

    and that gives me a compiler error. Prefixing the variable with static (since only one copy is really needed) gave the same error.

    The only way I have been able to initialize that variable is by brute force entry of each element in the constructor function. There MUST be a better way ! (I'd hate to have to initialize an array of 100 elements in that manner.)

    Does anyone know the correct syntax to accomplish this seemingly trivial task?

    Thanks,

    Brian

  2. #2
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: initializing an array in a class object

    You probably want 1) const and 2) static in this case.

    See this article on how to initialize the variable properly:
    http://www.devx.com/tips/Tip/5602

    http://www.google.com/search?hs=uCG&...2B&btnG=Search
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: initializing an array in a class object

    When you make a variable static - you need to define it outside the class in one of the source code (.cpp) files. It is this, that you did not do and hence seeing an error. Try doing that if you really think only one copy of it is needed for all your objects. Also make it a const if you think it is not going to change. You can also try the option of enums if you can look for a name for all these static consts.

    As for initializing arrays in using constructor initialization list and the limitations, have a look at this beautiful thread : Constructors and arrays. Hope this helps. Regards.

  4. #4
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Re: initializing an array in a class object

    Hi!

    Tha "static" approach was just a desparate attempt. I searched the site for initialization of arrays in a class object with various wordings on the search and could not find anything. My C++ book has nothing, either.

    The thread you refer to describes initialization of arrays of objects but not of an array within an object.

    One way is to define my constructor as
    myclass(juliandays);

    and then upon creation of the instance (maybe some syntax errors here)

    myclass x(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);

    but that's a worse choice because I want to bury the initialization in the class creation so I DON'T have to remember the long list of values.

    Maybe there is no way to intialize an array within a class.

    Brian

  5. #5
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: initializing an array in a class object

    Brian,
    did you take a moment to look at the article I posted? It does static const initialization? It can be very easily carried over into arrays.

    Here's an MSDN article showing as such:
    http://msdn.microsoft.com/library/de...m/specl_32.asp
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  6. #6
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Re: initializing an array in a class object

    Hi Eli,

    Just took a look.

    It seems that if I write

    myclass
    {
    public:
    static const long int juliandays[11];
    myclass();
    };

    const long int myclass::juliandays[11] = {list of 11 values};

    will do the trick.

    I will give it a try!

    Thanks (It was a lot harder than I thought it would be; wonder why you can't just do it in the variable declaration?)

    Brian

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: initializing an array in a class object

    Quote Originally Posted by Gyannea
    The thread you refer to describes initialization of arrays of objects but not of an array within an object.
    I am sure you did not have a good look at the thread I mentioned. Regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center