-1

I am trying to make a get method in a class medicine to get the description. As I know of that in C++, there is no data type "String" like in Java, therefore I need to make an array of char to save the description. I couldn't find a way to make a method with return type "array of chars", I use a pointer like the following.

class Medicine {
public:
    char description[100] = "testing";
    char *getDescription() {
        char *p_description;
        char *p_subDescription = (char*)malloc(100 * sizeof(char));
        strcpy(p_subDescription, description);
        p_description = p_subDescription;
        free(p_subDescription);
        return p_description;
    }
};

I have a question: is there any other way I can do to make the code shorter? I feel like this is too much work just for a method to get a string.

Thank,

Someonewhohaveacat
  • 83
  • 1
  • 3
  • 11
  • 3
    C++ **does** have a `string` class. – Federico klez Culloca Jun 05 '17 at 09:59
  • 1
    Don't use malloc/free and strcpy in C++! –  Jun 05 '17 at 10:00
  • 2
    @manni66, and don't return a pointer to a location you just `free`d, why we are at it – Federico klez Culloca Jun 05 '17 at 10:01
  • 2
    [`std::string`](http://en.cppreference.com/w/cpp/string) - this kind of pointer jiggling is tricky, and you shouldn't do it if you don't have to. – BoBTFish Jun 05 '17 at 10:01
  • Possible duplicate of [this](https://stackoverflow.com/questions/1563897/c-static-constant-string-class-member). Is this what you wanted? – Bartek Banachewicz Jun 05 '17 at 10:08
  • I am sorry for this dumb question. I have learnt c++ for 2 weeks. And can I know the reason why shouldn't I use malloc/free and strcpy in C++ @manni66? Thank you @BartekBanachewicz, I did not find it before. Thanks you for spending time for this. – Someonewhohaveacat Jun 05 '17 at 10:16
  • 1
    @Someonewhohaveacat these are the old C functions. For strings use std::string. If you really have to alocate memory yourself, use the C++ functions new/delete + new[]/delete/[]. If you learnt malloc for C++ then your learning material is very bad! –  Jun 05 '17 at 11:08

3 Answers3

2

If you want this to be static (class-specific), the easiest way I can think of is a static, public property:

class Medicine {
public:
    static const std::string description;
};
const std::string Medicine::description = "testing";
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
2

You have to decided on your flavour of C++. If you want "C with classes", C++ very close to C, then you should be passing strings about as char *s to memory allocated with malloc. Your code is nearly correct (however you free memory then return it), but most people write a little function called strdup (in fact it's often provided, which means ironically it's best to call it mystrdup() to avoid collisions)

char *mystrdup(char *str)
{
   char *answer = malloc(strlen(st) +1);
   if(answer)
      strcpy(answer, str);
   return answer;
}

If you are writing a more modern version of C++, use the std::string class. It isn't especially efficient and it's widely criticised for being over-desinged, but it's standard. It does memory management for you, and it's easy to create one.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18
0

You can use std::string like this

class Medicine {
public:
  char description[100] = "testing";
    std::string getDescription() {
    return description;
  }
};
Yuki
  • 3,857
  • 5
  • 25
  • 43