Where in the class definition should these be put


T

Tony Johansson

Hello!

I have these two statements
typedef const char *enum_txt[];
and
enum_txt phase_tab
={"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};

where in the class definition should these be put.

What I think is that this statement "typedef const char *enum_txt[];"
must be put in the header file outside of the class definition and this
statement "enum_txt phase_tab
={"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};"
must be put in the implementation file outside of any functionmethod.

//Tony
 
Ad

Advertisements

V

Victor Bazarov

Tony said:
I have these two statements
typedef const char *enum_txt[];
and
enum_txt phase_tab
={"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};

where in the class definition should these be put.

What I think is that this statement "typedef const char *enum_txt[];"
must be put in the header file outside of the class definition and this
statement "enum_txt phase_tab
={"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};"
must be put in the implementation file outside of any functionmethod.

It depends on the intention of the use of these. For example, does
the type 'enum_txt' have a life outside the class? Is it shared by
another class, maybe? Then yes, it should be at the namespace level.
If it's specific to your class, limit its scope by putting the typedef
into the definition as well. If you do so, declare it 'public' only
if it's used outside the class hierarchy. Declare it 'protected' if
derived classes may need to use it. Otherwise declare it 'private'.

As to the declaration of an object of type 'enum_txt', you need to look
at how it's used. If you use 'phase_tab' only in that class' member
functions, why should it reside outside the class? No sense to pollute
the namespace with unnecessary names. You can probably get away by
making that object ('phase_tab') a static data member. Of course, if
it is used by some other functions in a context not related to your
class, put it at the namespace level as well.

IOW, the question of the scope for a name cannot be answered without
information on the use of the name.

V
 
T

Tony Johansson

It's no problem to put this statement "typedef const char *enum_txt[];"
in the definition of the class but how do I put
this statement "enum_txt phase_tab =
{"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};"
is the class definition because when I do I get compile error.

//Tony

Victor Bazarov said:
Tony said:
I have these two statements
typedef const char *enum_txt[];
and
enum_txt phase_tab
={"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};

where in the class definition should these be put.

What I think is that this statement "typedef const char *enum_txt[];"
must be put in the header file outside of the class definition and this
statement "enum_txt phase_tab
={"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};"
must be put in the implementation file outside of any functionmethod.

It depends on the intention of the use of these. For example, does
the type 'enum_txt' have a life outside the class? Is it shared by
another class, maybe? Then yes, it should be at the namespace level.
If it's specific to your class, limit its scope by putting the typedef
into the definition as well. If you do so, declare it 'public' only
if it's used outside the class hierarchy. Declare it 'protected' if
derived classes may need to use it. Otherwise declare it 'private'.

As to the declaration of an object of type 'enum_txt', you need to look
at how it's used. If you use 'phase_tab' only in that class' member
functions, why should it reside outside the class? No sense to pollute
the namespace with unnecessary names. You can probably get away by
making that object ('phase_tab') a static data member. Of course, if
it is used by some other functions in a context not related to your
class, put it at the namespace level as well.

IOW, the question of the scope for a name cannot be answered without
information on the use of the name.

V
 
Ad

Advertisements

V

Victor Bazarov

Tony said:
It's no problem to put this statement "typedef const char *enum_txt[];"
in the definition of the class but how do I put
this statement "enum_txt phase_tab =
{"IDLE","LAUNCHING","LAUNCHED","ROLLING","BOOSTERRELEASE","SPACE"};"
is the class definition because when I do I get compile error.

(a) Don't top-post.

(b) Don't put it in directly. Declare 'phase_tab' "static" and define
it (like any other static data member) at the namespace level. Read
your favourite C++ book about static data members. Pay attention
to the fact that 'enum_txt' is also a member if you declare it in the
class definition.

class HasTypeAndStatic { // class definition
typedef double *myDoublePtr; // type declaration
static myDoublePtr ptr; // data member declaration
};
...
HasTypeAndStatic::myDoublePtr HasTypeAndStatic::ptr = 0; // definition

V
 

Top