 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
f Guest
|
Posted: Tue Oct 21, 2003 7:18 pm Post subject: const char** in class |
|
|
class MyClass{
static const char** My_String;
};
const char** MyClass::My_String = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
does not compile. How can I do it? I am using VC++ 6.0.
Thanks,
ff
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Oct 22, 2003 1:31 am Post subject: Re: const char** in class |
|
|
On 21 Oct 2003 15:18:30 -0400, [email]ffunus (AT) yahoo (DOT) com[/email] (f) wrote:
| Quote: | class MyClass{
static const char** My_String;
};
const char** MyClass::My_String = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
does not compile. How can I do it?
|
One way is
class MyClass{
static const char** My_String;
};
char const * data[] = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
const char** MyClass::My_String = data;
but the design is flawed , so I suggest instead correcting the
design.
E.g., do something like
class MyClass
{
private:
static char const * const myStrings[];
};
char const * const MyClass::myStrings[] =
{
"s1",
"s1",
"s3",
"s4",
"s5"
};
Even better, since this is private data you don't need to place it
in the class at all, just place it in the class' implementation file
using either 'static' (which AFAIK is deprecated for this usage, but
nice and clear) or an anonymous namespace (not so nice).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Erik Max Francis Guest
|
Posted: Wed Oct 22, 2003 3:56 pm Post subject: Re: const char** in class |
|
|
f wrote:
| Quote: |
class MyClass{
static const char** My_String;
};
const char** MyClass::My_String = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
does not compile. How can I do it? I am using VC++ 6.0.
|
That's a const char *[], not a const char **. If you really wanted a
pointer rather than an array, just create a const char *[] and
initialize it as you've done before, then take the address of the first
element.
--
Erik Max Francis && [email]max (AT) alcyone (DOT) com[/email] && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ God is love, but get it in writing.
__/ Gypsy Rose Lee
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Wed Oct 22, 2003 6:17 pm Post subject: Re: const char** in class |
|
|
"f" <ffunus (AT) yahoo (DOT) com> wrote
| Quote: | static const char** My_String;
|
try const char* My_String[5] instead of that.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Oct 22, 2003 6:20 pm Post subject: Re: const char** in class |
|
|
[email]ffunus (AT) yahoo (DOT) com[/email] (f) wrote in message
news:<8f4ce98a.0310202027.22d18f40 (AT) posting (DOT) google.com>...
| Quote: | class MyClass{
static const char** My_String;
};
const char** MyClass::My_String = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
does not compile.
|
Why should it?
class MyClass
{
static float const duh ;
} ;
int const* MyClass::duh = 42.3 ;
doesn't compile either.
The type declared in the class and the type used in the definition must
match.
It depends on what you want to do. If you want a pointer to a pointer,
you have to declare a pointer to a pointer (and initialize it) in both
places. If you want an array of pointers, you have to declare an array
of pointers in both places. (I suspect you want the latter; your
initialization is only legal for an array of pointers.)
The variable cannot be a pointer in one context, and an array in
another.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
rishi Guest
|
Posted: Wed Oct 22, 2003 6:21 pm Post subject: Re: const char** in class |
|
|
[email]ffunus (AT) yahoo (DOT) com[/email] (f) wrote in message news:<8f4ce98a.0310202027.22d18f40 (AT) posting (DOT) google.com>...
class MyClass{
static char const* My_String[];
};
char const* MyClass::My_String[]={"s1"..etc};
try a different class design strategy tho if you can .
| Quote: | class MyClass{
static const char** My_String;
};
const char** MyClass::My_String = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
does not compile. How can I do it? I am using VC++ 6.0.
Thanks,
ff
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Calcote Guest
|
Posted: Sun Oct 26, 2003 5:17 am Post subject: Re: const char** in class |
|
|
[email]ffunus (AT) yahoo (DOT) com[/email] (f) wrote in message news:<8f4ce98a.0310202027.22d18f40 (AT) posting (DOT) google.com>...
| Quote: | class MyClass{
static const char** My_String;
};
const char** MyClass::My_String = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
does not compile. How can I do it? I am using VC++ 6.0.
|
Answers given so far not very useful? I've found this to be the case,
as well. In a world where we should be instructors and helpers to one
another, we instead tend to try to make ourselves look smart by making
others look stupid.
Try this:
class MyClass{
static const char * const My_String[];
};
const char * const MyClass::My_String[] = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
I've also taken the liberty of adding another const to your
declaration (const char * const xyz[] . I'm guessing you wanted a
true const in your class definition. Forgive me if I'm mistaken here.
The second const will make your array of pointers const, as well as
the things pointed to. This gives the compiler a bit more latitude
with your declaration.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
DarkSpy Guest
|
Posted: Fri Oct 31, 2003 12:06 am Post subject: Re: const char** in class |
|
|
[email]jcalcote (AT) novell (DOT) com[/email] (John Calcote) wrote in message news:<abb28ce2.0310251701.e35589c (AT) posting (DOT) google.com>...
| Quote: | ffunus (AT) yahoo (DOT) com (f) wrote in message news:<8f4ce98a.0310202027.22d18f40 (AT) posting (DOT) google.com>...
...
Try this:
class MyClass{
static const char * const My_String[];
};
const char * const MyClass::My_String[] = {
"s1",
"s1",
"s3",
"s4",
"s5"
};
i wrote some other helper class to make the init things, but it is |
looks not graceful and efficient...:-)
struct Init
{
vector<char *> str;
Init(char * _str, ...)
{
va_list marker;
va_start( marker, _str );
char * count = _str;
while( strcmp(count, "end") )
{
str.push_back(count);
count = va_arg( marker, char * );
}
va_end(marker);
}
operator const char * * ()
{
char * * ret;
* ret = new char [4]; // be your string size
for(int i=0; i
{
ret[i] = new char[4];
strncpy(ret[i], str[i], strlen(str[i]));
ret[i][strlen(str[i])] = 0;
}
return (const char * *)ret;
}
};
class MyClass{
static const char * * My_String;
};
const char * * MyClass::My_String = Init("1","2","3","end");
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|