char and constructors in c++

Asked

Viewed 98 times

0

I need a constructor for a class that has char vectors as attributes, like this one:

class Anthem {
    private:
        int Id;
        char Name[50];
        char Country[50];
        int Year;
        char Composer[30];
        char Historic[200];
    public:
        Anthem(int id, char name[50], char country[50], int year, char 
composer[30], char historic[200]);
        ~Anthem();
}

But I don’t know how it should look in the builder actually, I did so:

Anthem::Anthem(int id, char* name, char* country, int year, char* composer, 
char* historic) { // @suppress("Class members should be properly initialized")
    Id = id;
    Name = name;
    Country = country;
    Year = year;
    Composer = composer;
    Historic = historic;
}

But not right, as I should do?

  • 2

    Why not simplify and use std::string since it is c++ ?

  • 1

    @Isac ah, because can you simplify it? : P

1 answer

0

You need to use

Name[] = name;
assert( strlen( Name) < sizeof( name) );
strcpy( name, Name );

instead of

Name = name;

Behold here;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.