C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Possible to #define an array?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
v65magnaebayer@hotmail.co
Guest





PostPosted: Sat Jan 29, 2005 4:54 am    Post subject: Possible to #define an array? Reply with quote



I'm translating some code from Java into C++, and I'd like to know if
it's possible to #define an array. Can't quite get the syntax down.

This is the Java:
private static final int AUTH_PLAINTEXT = 0;
private static final int AUTH_DIGEST = 1;
private static final int AUTH_SASL = 2;
private static final int NUM_AUTH_MECHANISMS = 3;
private static final String[] AUTH_MECHANISMS = new String[] {
"plaintext", "digest", "SASL" };


This is my C++:
enum AUTH_MECHS {
AUTH_PLAINTEXT = 0,
AUTH_DIGEST,
AUTH_SASL,
NUM_AUTH_MECHANISMS
};
#define AUTH_MECHANISMS[NUM_AUTH_MECHANISMS] {"plaintext", "digest"
}

That #define obviously doesn't work. I know I can just make a quick and
dirty inline method with a switch to do what I want, but if I can
#define the array I'd rather do that.

Thanks.

Charles.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ivan Vecerina
Guest





PostPosted: Sat Jan 29, 2005 9:33 am    Post subject: Re: Possible to #define an array? Reply with quote



<v65magnaebayer (AT) hotmail (DOT) com> wrote

Quote:
I'm translating some code from Java into C++, and I'd like to know if
it's possible to #define an array. Can't quite get the syntax down.

This is the Java:
private static final int AUTH_PLAINTEXT = 0;
private static final int AUTH_DIGEST = 1;
private static final int AUTH_SASL = 2;
private static final int NUM_AUTH_MECHANISMS = 3;
private static final String[] AUTH_MECHANISMS = new String[] {
"plaintext", "digest", "SASL" };


This is my C++:
enum AUTH_MECHS {
AUTH_PLAINTEXT = 0,
AUTH_DIGEST,
AUTH_SASL,
NUM_AUTH_MECHANISMS
};
#define AUTH_MECHANISMS[NUM_AUTH_MECHANISMS] {"plaintext", "digest"


Note that string literals ("...") automatically include a trailing NULL,
so adding an explicit termination is not necessary.


Quote:
}

That #define obviously doesn't work. I know I can just make a quick and
dirty inline method with a switch to do what I want, but if I can
#define the array I'd rather do that.


Try something like:
const char const* AUTH_MECHANISMS[NUM_AUTH_MECHANISMS] =
{ "plaintext", "digest", "SASL" };
or:
const std::string AUTH_MECHANISMS[NUM_AUTH_MECHANISMS] =
{ "plaintext", "digest", "SASL" };


hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Falk Tannhäuser
Guest





PostPosted: Sun Jan 30, 2005 4:04 am    Post subject: Re: Possible to #define an array? Reply with quote



[email]v65magnaebayer (AT) hotmail (DOT) com[/email] wrote:
Quote:
I'm translating some code from Java into C++, and I'd like to know if
it's possible to #define an array. Can't quite get the syntax down.
[snip]
This is my C++:
enum AUTH_MECHS {
AUTH_PLAINTEXT = 0,
AUTH_DIGEST,
AUTH_SASL,
NUM_AUTH_MECHANISMS
};
#define AUTH_MECHANISMS[NUM_AUTH_MECHANISMS] {"plaintext", "digest"
}

That should be

extern char const* const AuthMechanisms[NUM_AUTH_MECHANISMS];

as *declaration* in the header file, together with the enum.
Furthermore, you need to *define* this array in exactly one
compilation unit (.C or .cxx file):

char const* const AuthMechanisms[NUM_AUTH_MECHANISMS] =
{ "plaintext", "digest", "SASL" };

If these definitions are part of a class (which is possible in
C++, too - but unlike in Java, it is not mandatory), you would
put

class SomeClass
{
...
enum AUTH_MECHS
{
AUTH_PLAINTEXT,
AUTH_DIGEST,
AUTH_SASL,
NUM_AUTH_MECHANISMS
};

static char const* const AuthMechanisms[NUM_AUTH_MECHANISMS];
};

in the header file and

char const* const SomeClass::AuthMechanisms[NUM_AUTH_MECHANISMS] =
{ "plaintext", "digest", "SASL" };

in a .C file (typically the one where the other class members are
defined).

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.