Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7507 > unrolled thread

array initiation - methodology for converting C code to C++

Started byRuben Safir <ruben@mrbrklyn.com>
First post2011-07-06 03:12 +0000
Last post2011-07-06 02:56 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  array initiation - methodology for converting C code to C++ Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 03:12 +0000
    Re: array initiation - methodology for converting C code to C++ 东 苏 <sudongndsl@gmail.com> - 2011-07-06 01:10 -0700
      Re: array initiation - methodology for converting C code to C++ Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 15:00 +0000
    Re: array initiation - methodology for converting C code to C++ Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 02:56 -0700

#7507 — array initiation - methodology for converting C code to C++

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-07-06 03:12 +0000
Subjectarray initiation - methodology for converting C code to C++
Message-ID<iv0jqi$alg$2@reader1.panix.com>
I had some C style code that essentially had

const char * choices[] = {"Choice A", "Choice B", "Choice C", "Choice D"};

Now this is obviously a problem in a class framework as it can not be 
initialized in the class definition.

So I tried some variations so I didn't have to alter the internal code 
that used the array.  Someone told me to make it a static array, but 
something happened which I didn't expect.  I tried

screens.h

 class menu_window
  {
    public:

      /* ====================  LIFECYCLE ======================================= */
      menu_window ();                             /* constructor      */
      menu_window (int height, int width, int row, int col);
      menu_window ( const menu_window &other );   /* copy constructor */
      ~menu_window ();                            /* destructor       */

      WINDOW *menu_win;
      static char const *choices[5];
      int height;
      int width;
....

}


screens.cpp

Screens::menu_window::menu_window (int pheight, int pwidth, int prow, int pcol): height(pheight), width(pwidth), highlight(1), choice(0) , n_choices(sizeof(choices)/ sizeof(choices[0])) {
   row_(prow); 
   col_(pcol);
   choices[0] = "Add a question ";
   choices[1] = "Start the Quiz ";
   choices[2] = "c ";
   choices[3] = "d ";
   choices[4] = "Exit ";
   menu_win = newwin(height, width, row_(), col_() );
   keypad(menu_win, TRUE);
   mvprintw(0,0, "Use arrow keys to go up and down, Press enter to select choice");
   refresh();
   print_menu(menu_win, highlight);


}			       /* constructor      */



and this failed as a static.  When I removed static, it works fine.  Why?

[toc] | [next] | [standalone]


#7515

From东 苏 <sudongndsl@gmail.com>
Date2011-07-06 01:10 -0700
Message-ID<5062d7c4-f7db-4749-99c0-ff87b2ef5eee@h12g2000pro.googlegroups.com>
In reply to#7507
On 7月6日, 上午11时12分, Ruben Safir <ru...@mrbrklyn.com> wrote:
> I had some C style code that essentially had
>
> const char * choices[] = {"Choice A", "Choice B", "Choice C", "Choice D"};
>
> Now this is obviously a problem in a class framework as it can not be
> initialized in the class definition.
>
> So I tried some variations so I didn't have to alter the internal code
> that used the array.  Someone told me to make it a static array, but
> something happened which I didn't expect.  I tried
>
> screens.h
>
>  class menu_window
>   {
>     public:
>
>       /* ====================  LIFECYCLE ======================================= */
>       menu_window ();                             /* constructor      */
>       menu_window (int height, int width, int row, int col);
>       menu_window ( const menu_window &other );   /* copy constructor */
>       ~menu_window ();                            /* destructor       */
>
>       WINDOW *menu_win;
>       static char const *choices[5];
>       int height;
>       int width;
> ....
>
> }
>
> screens.cpp
>
> Screens::menu_window::menu_window (int pheight, int pwidth, int prow, int pcol): height(pheight), width(pwidth), highlight(1), choice(0) , n_choices(sizeof(choices)/ sizeof(choices[0])) {
>    row_(prow);
>    col_(pcol);
>    choices[0] = "Add a question ";
>    choices[1] = "Start the Quiz ";
>    choices[2] = "c ";
>    choices[3] = "d ";
>    choices[4] = "Exit ";
>    menu_win = newwin(height, width, row_(), col_() );
>    keypad(menu_win, TRUE);
>    mvprintw(0,0, "Use arrow keys to go up and down, Press enter to select choice");
>    refresh();
>    print_menu(menu_win, highlight);
>
> }                              /* constructor      */
>
> and this failed as a static.  When I removed static, it works fine.  Why?

static memeber variable should like this Screens::choices[0] = "Add a
question ";

[toc] | [prev] | [next] | [standalone]


#7530

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-07-06 15:00 +0000
Message-ID<iv1tan$mbj$1@reader1.panix.com>
In reply to#7515
On Wed, 06 Jul 2011 01:10:55 -0700, 东 苏 wrote:


> static memeber variable should like this Screens::choices[0] = "Add a
> question ";



as opposed to what I wrote and didn't compile?

[toc] | [prev] | [next] | [standalone]


#7521

FromMichael DOUBEZ <michael.doubez@free.fr>
Date2011-07-06 02:56 -0700
Message-ID<453f3d6d-dd59-475f-ab2b-c91467b257f1@v10g2000yqn.googlegroups.com>
In reply to#7507
On Jul 6, 5:12 am, Ruben Safir <ru...@mrbrklyn.com> wrote:
> I had some C style code that essentially had
>
> const char * choices[] = {"Choice A", "Choice B", "Choice C", "Choice D"};
>
> Now this is obviously a problem in a class framework as it can not be
> initialized in the class definition.

No but it can be afterward:
class menu_window
{

  static char const * const choices[];
};

char const * const menu_window::choices[] = {"Choice A", "Choice B",
"Choice C", "Choice D"};

> So I tried some variations so I didn't have to alter the internal code
> that used the array.  Someone told me to make it a static array, but
> something happened which I didn't expect.  I tried
>
> screens.h
>
>  class menu_window
>   {
>     public:
>
>       /* ====================  LIFECYCLE ======================================= */
>       menu_window ();                             /* constructor      */
>       menu_window (int height, int width, int row, int col);
>       menu_window ( const menu_window &other );   /* copy constructor */
>       ~menu_window ();                            /* destructor       */
>
>       WINDOW *menu_win;
>       static char const *choices[5];
>       int height;
>       int width;
> ....
>
> }
>
> screens.cpp
>
> Screens::menu_window::menu_window (int pheight, int pwidth, int prow, int pcol): height(pheight), width(pwidth), highlight(1), choice(0) , n_choices(sizeof(choices)/ sizeof(choices[0])) {
>    row_(prow);
>    col_(pcol);
>    choices[0] = "Add a question ";
>    choices[1] = "Start the Quiz ";
>    choices[2] = "c ";
>    choices[3] = "d ";
>    choices[4] = "Exit ";
>    menu_win = newwin(height, width, row_(), col_() );
>    keypad(menu_win, TRUE);
>    mvprintw(0,0, "Use arrow keys to go up and down, Press enter to select choice");
>    refresh();
>    print_menu(menu_win, highlight);
>
> }                              /* constructor      */
>
> and this failed as a static.  When I removed static, it works fine.  Why?

Did it fail at link time ? If so, it is because you have declared it
but not defined it.

--
Michael

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web