Jesse Upchurch
Jesse Upchurch

Reputation: 91

String literal to char is deprecated

Is there any way to get rid of the 3 warnings: "Conversion from string literal to 'char *' is deprecated"

these are my shape constructors. They are derived classes from a shapes base class.

I'm getting the warning on these 3 lines.

right_triangle right_triangle("RIGHT-TRIANGLE-1", 5.99, 11.99);
square         square        ("SQUARE-1", 11.99);
rectangle      rectangle     ("RECTANGLE-1", 11.99, 5.99);

Since all 3 classes do basically the same thing, I will use the right_triangle object as an example. In the constructor, everything about the shape is created.

Here is the class.

class right_triangle : public shapes
{
   char  *p_name;
   float base,
         height,
         hypotenuse;
public:
   void  show_shape     ();
         right_triangle (char name[17], float base, float height);
         ~right_triangle()                          {}

};

Here is the constructor.

//**********************************************************************
//*                     Right triangle constructor                     *
//**********************************************************************
right_triangle::right_triangle(char name[17], float rt_base, float rt_height)
{
   // Print constructor lines
   cout << "\n\n\nCreating right triangle shape";
   cout << "\n     with base = " << rt_base
        << " and height = "      << rt_height;

   // Cause pointer to point to dinamically allocated memory
   if((p_name = (char *)malloc(strlen(name)+1)) == NULL)
     fatal_error(1);
   else
   {
   strncpy(p_name, name, strlen(name)+1);
   base       = rt_base;
   height     = rt_height;
   set_total_sides (3);
   set_unique_sides(3);
   hypotenuse = hypot(base, height);
   set_area        (0.5f * base * height);
   set_perimeter   (base + height + hypotenuse);
   }

}

Is there any way to get rid of these warnings? I'm using the array of char because of the strcpy that I have to get the name of the shape. Any help or advice would be appreciated, thanks.

Upvotes: 0

Views: 1916

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

Simply change the declaration of the constructor

right_triangle (char name[17], float base, float height);

to

right_triangle( const char name[17], float base, float height );

In C++ string literals has type of const char [].

Take into account that these declarations are equivalent and declare the same function

right_triangle( const char name[17], float base, float height );
right_triangle( const char name[], float base, float height );
right_triangle( const char *name, float base, float height );

Also use operator new instead of C function malloc

   p_name = new char[strlen( name ) + 1];
   strcpy( p_name, name );

ALso the destructor is invalid

 ~right_triangle() {}

It has to free the allocated memory for p_name.

 ~right_triangle() { delete [] p_name; }

Also either define the copy constructor and the copy assignment operators as deleted or define them explicitly.

Upvotes: 2

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153820

First off, note that in a function declaration char name[17] is just a fancy spelling for char*. Second, the type of string literals is char const[N] with a suitable N. These arrays happily convert to char const* but not to char* as the latter looses constness (and with C++11 this conversion isn't supported at all according to the standard although probably some compilers will continue to allow the conversion to char*).

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Stop storing strings as C-strings. Use std::string.

If you really needed a C-string, you'd store a const char* (literals cannot be modified). But you don't.

Upvotes: 5

Related Questions