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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top