Re: More a C++ Q then vs... but why not?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alf P. Steinbach (alfps_at_start.no)
Date: 07/07/04


Date: Wed, 07 Jul 2004 21:38:13 GMT


* Dead RAM:
> 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";
> };

You can't have an array of indeterminate size as a member.

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

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.

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


Relevant Pages

  • Problems returning an int Array pointer
    ... get a pointer to the appropriate integer list. ... the appropriate int array to an external int array (in contrast to returning ...
    (microsoft.public.dotnet.languages.vc)
  • Re: operator overloading .. more
    ... >> will be 25 ints in a contiguous block of memory. ... Well, first we get a pointer, dynmat ... Then, we allocate an array of five pointers int, and point dynmat to it. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: default parameter value
    ... > temp(); ... it just treated the array like it was declared a pointer. ...
    (comp.lang.cpp)
  • Re: Question about arrays in objects
    ... > I want to create a 2d array as an object member. ... > then put the array in the constructor and use the pointer? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)