Initializing null-terminated char array inside class

I'm just starting to learn classes and I want to initialize null-terminated char array inside a class, but I'm doing it wrong:

1
2
3
4
5
6
7
8
9
10
11
//assume the class is in a header
class SomeClass {
char test[10] = {'\0'};
public:
void SetTest(char ch, int i) {
test[i] = ch;
}
char GetTest(int i) {
return test[i];
}
};


I want to do something like this in cpp file:

1
2
3
while(i<5) { someObj.SetTest('x', i); i++; }
i=0;
while(someObj.GetTest(i)) { cout << someObj.GetTest(i); i++; }


What would be the right way? I don't want to use a loop to assign nulls to whole array or something similar.
You need a constructor for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class SomeClass
{
    char test[10];

public:

    SomeClass()
    {
        for (int i=0; i<10; i++)
            test[i]='\0';
    }

    void SetTest(char ch, int i)
    {
        test[i] = ch;
    }

    char GetTest(int i)
    {
        return test[i];
    }

};

Though, I suggest using std::string instead of char arrays:

http://cplusplus.com/reference/string/string/
Well I actually want to avoid that, but if there's no other way... I'm not using char array for text (words and such), but for array of single chars, which have no direct relation to each other.
Is it generally bad to use char arrays? If yes, then why?

Is there any known reason why you can't assign values directly in a class? I don't see any reason myself, since nothing has "physical" existence until you create an object, but I know there must be a good one.
Does assigning a value to a whole array with a loop uses same amount of resources as assigning it the way I wrote above? Is there actually any big difference between the two in the "core"?

Sorry for so many questions, but the more I know the better.

P.S. Would using a vector of chars instead of char array create a null-terminated vector automatically?
Is it generally bad to use char arrays? If yes, then why?

If the intent is a string, then it is generally much easier to use std::string than char arrays.

Is there any known reason why you can't assign values directly in a class?

The short and simple answer is that the standard does not allow it.

Does assigning a value to a whole array with a loop uses same amount of resources as assigning it the way I wrote above?

Yours is not legal C++ in that context, so the question cannot be answered meaningfully.

Would using a vector of chars instead of char array create a null-terminated vector automatically?
std::vector<char> v( 10 ); creates a vector of 10 characters, all defaulted to \0. However std::string
is a better alternative to std::vector<char> if the intent is to hold a string.


Thanks!

I'm using char arrays only when I have an array of unrelated characters. If it's text, then I use a string.

What I meant by 2nd question is if initializing null-terminated char array this way (in general):

char test[10] = {'\0'}

...is any different (memory, CPU, stability-wise) than this:

1
2
char test[10];
for (int i; i <10; i++) test[i] = '\0';
You can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class SomeClass
{
	char test[10];
public:
	SomeClass()
	: test() // zero init array
	{
	}

	void SetTest(char ch, int i)
	{
		test[i] = ch;
	}
	char GetTest(int i)
	{
		return test[i];
	}
};


But I would consider using a string or a vector.
Assuming you initialize i to 0... Memory and stability wise, no, they are identical.

CPU wise is harder to say. It depends on your compiler and optimizer. I would
not concern myself with it though, as the difference is not likely to be noticeable
even if you ran the code hundreds of thousands of times.

Topic archived. No new replies allowed.