Well... Don't know C++ as well as a pro... so here goes...
I want to do somthing like this:
-------------------------------------------------------------------
class someThing {
public:
// stuff, constructor, destructor, etc...
private:
const char myConstCharArrayInitializedAtObjectCreation[] =
"blah";
};
--------------------------------------------------------------------
After brute forcing my way through the errors I ended up with this:
-------------------------------------------------------------------
class someThing {
public:
// stuff, constructor, destructor, etc...
private:
const char* myConstCharArrayInitializedAtObjectCreation;
};
// more stuff
someThing::someThing()
: myConstCharArrayInitializedAtObjectCreation("blah")
{
// more stuff
}
-------------------------------------------------------------------
I'm mainly wondering, in the second (and error free) version, is it the
equivalent of the C statement:
const char ch[]="blah"
Umm... in other words, where is the "blah" data being stored and setup and
what not? in the data segment and then copied into the heep when the object
is created. And am I running into a situation were the const char wasn't
initialized properly and could potentialy lose the "blah" data (is the
memory pointed to by the const char* valid, or does it just happend to work
sometimes)?
Anyways, if you feel like rambling on about this for me i'd appriciate it ;)
andrew - 07 Jul 2004 22:08 GMT
This question is probably better asked in comp.c++.whawtever... but:
Consider using a static member for this. Initialize it in your class's .cpp
implementation file.
What you are doing seems unsafe to me. Your const char * ctor is being
passed the address of a string. The string itself is implicitly created on
the stack as you are declaring it right in the parameter list for a function
call. So it will go out of scope... or at least any other type would... but
a string may be a special case?? Since it is a string constant it must be
stored in the .exe's data segment somewhere. So is it being copied from
there to a temp. string which is created at time of ctor call? OR is the
direct address of the original string constant being passed? If so you get
lucky and it works.
> Well... Don't know C++ as well as a pro... so here goes...
>
[quoted text clipped - 45 lines]
>
> Anyways, if you feel like rambling on about this for me i'd appriciate it ;)
Alexander Grigoriev - 10 Jul 2004 15:46 GMT
String constants are static.
> This question is probably better asked in comp.c++.whawtever... but:
>
[quoted text clipped - 10 lines]
> direct address of the original string constant being passed? If so you get
> lucky and it works.
Alf P. Steinbach - 07 Jul 2004 22:38 GMT
* Dead RAM:
> Well... Don't know C++ as well as a pro... so here goes...
>
[quoted text clipped - 9 lines]
> "blah";
> };
You can't have an array of indeterminate size as a member.
> --------------------------------------------------------------------
>
[quoted text clipped - 16 lines]
> // more stuff
> }
This is OK.
> -------------------------------------------------------------------
>
> I'm mainly wondering, in the second (and error free) version, is it the
> equivalent of the C statement:
>
> const char ch[]="blah"
No it isn't. What you have in class someThing is a pointer. What
you have right above is a character array.
> Umm... in other words, where is the "blah" data being stored
The "blah" has static storage.
The pointer to "blah" is being stored in your object.
One per object.
> and setup
?
> And am I running into a situation were the const char wasn't
> initialized properly and could potentialy lose the "blah" data
Not in the code presented here.
> (is the
> memory pointed to by the const char* valid, or does it just happend to work
> sometimes)?
In the code presented it is valid.

Signature
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?