0

I have a class field

char chars[4]

I want to init this field in the constructor.
My constructor need to get char chars[] as a parameter and in the constructor call I want to init my class fields.
Can I do it or I need to call copy?

2
  • 2
    Would you post some code showing what you want to do exactly in the constructor? If you are trying to use the initializer list to initialize the array, that is not possible. However, I can not understand your intention from the question. Commented Aug 8, 2012 at 14:30
  • 1
    In C++, you generally want to avoid built-in arrays, and use std::string, std::vector or std::array instead. Commented Aug 8, 2012 at 14:54

5 Answers 5

5

Use std::copy like this:

class MyClass
{
    char m_x[4];    
public:
    MyClass(const char (&x)[4])
    {
        std::copy(x, x + 4, m_x);
    }
};

You should really be explicit with your types here to enforce that you pass exactly 4 elements.

Sign up to request clarification or add additional context in comments.

8 Comments

Being explicit with the types here doesn't enforce anything at all. MyClass(const char x[4]) is the same as MyClass(const char* x).
It's not enforce but document the method by the signature, which is good
Edited so it does enforce. My bad.
Logically, you could actually accept a character array of any size greater than four.
Right; I went with the most literal interpretation of the original question. There's no way to know where the flexibility lies: in the passed-in type, or in the stored type. We don't even know what this variable is used for; most likely it's a string which makes most of the answers here worthless.
|
3

You need to call copy.

MyClass::MyClass(const char* param)
{
  std::copy(param, param + 4, chars);
}

This is slightly risky code since there is no guarantee that param has four characters to copy.

6 Comments

Is there any simple alternative. Is there a difference between define the param in the constructor as char* to define as char array? Thank you
For a parameter char* is exactly the same as char[], and const char * is exactly the same as const char[]. But only for parameters.
Can you please explain why only for char
No, not only for char, only for parameters. In a parameter int* is the same as int[] for instance. The reason is that you cannot pass an array as a parameter in C/C++. So the designers of C chose to use the array syntax to mean the same as a pointer for parameters. If you want to say char[] then do it like that. It won't make any difference.
what about passing a std::vector<char> by reference instead?
|
2

An alternative could be to use a vector<char> which can be initialised in the initialiser list:

class A
{
public:
    A(char* a): a_(a, a + strlen(a)) {}
private:
    std::vector<char> a_;
};

See demo at http://ideone.com/x9p6I .


This could be made into a template to support different types but would require the number of elements in the array to be provided to the constructor (as strlen() would not be applicable to an array of int). For example:

template <class T>
class A
{
public:
    A(const T* a, const size_t count): a_(a, a + count) {}
private:
    std::vector<T> a_;
};

See demo at http://ideone.com/hhoaC .

2 Comments

And if a is not null-terminated? Because if it is, he's better to use std::string, right?
@tenfour, correct. About to update this as what if an int array was mentioned in another comment.
-1

As I understood, you need to create Conversion constructors, it should looks like this: A::A(const char* string) {strncpy(chars, string, 4); }

Comments

-2

Yes. You need to copy the contents from the input parameter.

6 Comments

It disgusts me that people post such things as answers to a C++ question.
strcpy_s takes 3 arguments, not two! This does not compile! and it's not C++
@john It shouldn't have been. The OP didn't even try the answer before accepting it.
@EtiennedeMartel: I guess the OP really doesn't want to call std::copy for some reason.
I accept the answer because this is the best for char array. In these simple answers it's not matter if it compile because the answer is clear , so if there is compilation error you fix it by yourself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.