2

Ex: Is this acceptable? It compiles and seems to work for me; so is this bad form?

.h file

class MyClass
{
  static char c[];
};

.cpp file

char MyClass::c[] = "abcde";

Or must I do this instead, for example?

.h file

class MyClass
{
  static char c[10];
};

.cpp file

char MyClass::c[10] = "abcde";

Is there a benefit to one technique over the other? I'm not sure if I'm missing something. I don't know what I don't know, ya know?

Update:

The original code I posted looked like this below. I edited it to make it as shown above since I didn't mean for the "private" aspect of it to be the point of discussion. In my real code (running on an Arduino), I am using .h and .cpp files and the static member is only intended to be accessed by the class. I guess I'm learning something new though too, as the answers regarding the below code seem to tell me that private static members are the same as public static members ie: they can both be modified by anything outside the class if static. That, I didn't know. WRONG, see the answer by Alok Save here. More on static member variables here. This line was especially helpful to me: "Because static member variables are not part of the individual objects, you must explicitly define the static member if you want to initialize it to a non-zero value...This initializer should be placed in the code file for the class (eg. Something.cpp). In the absense of an initializing line, C++ will initialize the value to 0."

class MyClass
{
  private:
  static char c[];
};

char MyClass::c[] = "abcde";

Or must I do this instead, for example?

class MyClass
{
  private:
  static char c[10];
};

char MyClass::c[10] = "abcde";
Community
  • 1
  • 1
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Did you mean to do `static char c[6]`? `char c[] = "abcde"` and `char c[10] = "abcde"` are not equivalent, because they create different sized arrays. – Joel Cornett Oct 03 '15 at 03:22
  • I think my confusion is where the memory is allocated when I don't specify the size. I'm afraid that if I do the first example with [], then the proper size won't be allocated and I'll be writing outside the array, since [] made the array size 0. I've never created a char array string in two separate steps like this before without explicitly setting a size. – Gabriel Staples Oct 03 '15 at 03:29
  • To answer you though, I don't know. `char c[] = "abcde"` will make c length 6 with null terminator at index 5. `char c[10] = "abcde"` will make c length 10, with null terminator at index 5. In either case I'm pretty sure the null terminator is there. – Gabriel Staples Oct 03 '15 at 03:31
  • 1
    Sorry, I removed the part the null terminators. The memory should be allocated statically somewhere. The only difference (aside from the 6 vs 10) is that the compiler will take care of figuring out what to put between the square brackets if you give it a string literal. – Joel Cornett Oct 03 '15 at 03:32
  • Ok...things are clearing up, so *where* does the compiler set the size? It seems to me that when I say `static char c[];` all the compiler knows is that c is a pointer to a char, right? Then, when I do `char MyClass::c[] = "abcde";`, the compiler says, ok, I will allocate 6 bytes for you *and* set the c pointer to point to the start of the memory location where I just allocated 6 bytes, is that right? – Gabriel Staples Oct 03 '15 at 03:38
  • 1
    Yes. It's equivalent to doing `class Foo { static char c[6]; };` and then later `char Foo:c[6] = "some string with 5 characters a null terminator";` – Joel Cornett Oct 03 '15 at 03:39
  • In other words, the first part creates a pointer called c, and the second part allocates memory for the char array (string) and does the equivalent of `c = char*-to-where-the-memory-was-just-allocated`? – Gabriel Staples Oct 03 '15 at 03:40

4 Answers4

2

The question seems to be about whether to explicitly write the size of the array, rather than to deduce it from the assignment. Consider this:

What if you need to change the string value that the array is initialized to? If you explicitly define the size, you will need to change it in 3 places. First in the class definition, second in the static variable assignment. And third, you will also end up changing the value of the string assigned. Not explicitly writing the array size allows you to make the change in only one place. Additionally, it eliminates the possibility of forgetting to add 1 for the null terminator at the end of the string.

Clearly, this simplifies future code changes and does not sacrifice code clarity.

Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
  • This is helping me; so, *not* explicitly setting the size makes the code more functional, while still allocating memory properly for the array? – Gabriel Staples Oct 03 '15 at 03:32
  • 1
    Yes. Conceptually, the compiler will go through and add the appropriate array sizes in the code at compile time. – Joel Cornett Oct 03 '15 at 03:34
  • please see my answer to my own question, that I just posted to this question. Upvote it if it's correct. Comment if it's incorrect. Thanks. – Gabriel Staples Oct 03 '15 at 03:57
1

Private variable should only access within ,it's class scope. when you ,put private without static ,it is safe and any one cannot access except with in class. in this case , I thin any one can change this variable , when creating a class.because this is static. he should ,only crate new class , and he know your variable name, he can change ,it value.

private variable should be private and that value should not access with out class method inside. please , visit OOP concept. you can get better idea. visit java access modifiers, you can take better idea.

you get take understand , why we put access modifiers.I think you , get some about your Question.

I think this is bad.

uma
  • 1,477
  • 3
  • 32
  • 63
  • Sorry; bad example. In my real code I'm using a .h and .cpp file and this private variable is intended for use only within the class. The real question is about [] vs [10]. is [] ok? – Gabriel Staples Oct 03 '15 at 03:09
  • @Gabriel Staples oh..sorry... some time ,I miss understand your question.I am java developer and know best knowledge about C++. but [10] only you assign only 10 character string. but c[] can , no limit of your string length . isn't it? – uma Oct 03 '15 at 03:17
0

Considering the member is private, nothing outside of the class should be able to mutate it. So neither piece of code should be used. Also, instead of using c-style arrays with c++, I would recommend you use the standard library std::valarray and std::vector for numerical and object data resp. and std::string for textual data.

You can read more about them in their docs.

As for your example:

class Object {
private:
    static int value[] = nullptr;

public:
   inline void setValue(int* newValue) {
        value = newValue;
    }
   inline int getValue() {
        return value;
    }
}

Object first{};
int a[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
first.setValue(a)
std::cout << first.getValue(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Object second{};

int b[1] = [1];
first.setValue(b)
std::cout << second.getValue(); // [1]
Aidan Gomez
  • 8,167
  • 5
  • 28
  • 51
  • Sorry; bad example. In my real code I'm using a .h and .cpp file and this private variable is intended for use only within the class. The real question is about [] vs [10]. Is [] ok? Also, this is running on an 8-bit 16Mhz microcontroller. I'm not sure if std::valarray, std::vector, etc are available or wise to use on my device. I'll have to look into it. – Gabriel Staples Oct 03 '15 at 03:11
  • 1
    @GabrielStaples Yeah man, it's fine. All [] does is initialize a nullptr, while [10] will allocate 10 whatevers and point the variable there. – Aidan Gomez Oct 03 '15 at 03:15
  • Hey sorry about throwing you off with my bad question. I struggled a lot to even make sense of what I'm asking. – Gabriel Staples Oct 03 '15 at 03:41
0

PLEASE CORRECT ME IF I'M WRONG, BUT THIS IS THE ANSWER THAT CLARIFIES MY CONFUSION THE MOST. Refer to the code comments, marked by //<--. These are my answer. Is this correct?

.h file

class MyClass
{
  static char c[]; //<--this allocates memory for a pointer, 
                   //and makes it a null pointer since it doesn't
                   //point anywhere yet
};

.cpp file

char MyClass::c[] = "abcde"; //<--this allocates 6 bytes 
  //(5 chars + null terminator) for the string, AND now 
  //sets the above pointer to point to the starting byte
  //of this newly allocated memory.
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 1
    These are *compile-time* deductions. In runtime, there should not be a point in which `c[]` is set to `nullptr`. The compiler should have already run through the entire source code at compile-time and figured out for itself the appropriate addresses to set for the correct variables. – Joel Cornett Oct 03 '15 at 04:01
  • Got it. I think my mistakes are all insightful; so I'll leave this "answer" as-is. This question was one of those rare times where I didn't know what my question really was until we got a discussion going. – Gabriel Staples Oct 03 '15 at 04:03