Thread: Useless conversion from string to char*

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Useless conversion from string to char*

    Good afternoon. I tried to translate the warning from portuguese to english.

    edit: I figured out

    Code:
     
    thames@semaht ~/C++ $ g++ -g -Wall inheritance.cpp -o inheritance
    inheritance.cpp: Na função membro ‘void Derived1Level1::f(char*)’:
    inheritance.cpp:32:21: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:33:21: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp: Na função membro ‘void Derived2Level1::f(char*)’:
    inheritance.cpp:48:23: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp: Na função membro ‘void DerivedLevel2::f(char*)’:
    inheritance.cpp:59:21: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:60:37: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:61:32: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp: Na função ‘int main()’:
    inheritance.cpp:72:17: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:75:19: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:77:19: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:78:19: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:81:18: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    inheritance.cpp:83:9: warning: conversão obsoleta de constante string para ‘char*’ [-Wwrite-strings]
    thames@semaht ~/C++ $
    Code:
     
    #include <iostream> 
    
    using namespace std;
    
    class BaseClass { 
        
      public: 
        BaseClass() {}
        void f(char *s = "unknown")
        { 
          cout << "Function f() in Baseclass called from " << s << endl; 
          h();      
        } 
      protected: 
        void g(char *s = "unknown") 
        { 
          cout << "Function g() in BaseClass called from " << s << endl;           
        } 
      private: 
         void h()
         { 
            cout << "Function h() in BaseClass\n";       
         } 
    };      
    
    class Derived1Level1: public virtual BaseClass
    { 
       public: 
         void f(char *s = "unknown")
         { 
            cout << "Function f() in Derived1Level1 called from " << s << endl;
            g("Derived1Level1");
            h("Derived1Level1");  
         }
         
         void h(char *s = "unknown")
         { 
            cout << "Function h() in Derived1Level1 called from " << s << endl;     
         }          
    }; 
    
    class Derived2Level1: public virtual BaseClass 
    { 
        public: 
          void f(char *s = "unknown")
          { 
              cout << "Function f() in Derived1Level1 called from " << s << endl; 
              g("Derived2Level1");
              // h() // error: BaseClass::h() is not accessible      
          } 
    }; 
    
    class DerivedLevel2: public Derived1Level1, public Derived2Level1
    { 
        public: 
          void f(char *s = "unknown")
          { 
             cout << "Function f() in DerivedLevel2 called from " << s << endl;
             g("DerivedLevel2");
             Derived1Level1::h("DerivedLevel2");
             BaseClass::f("DerivedLevel2");  
          }             
    };
    
    int main(void) 
    { 
      BaseClass bc; 
      Derived1Level1 d1l1; 
      Derived2Level1 d2l1; 
      DerivedLevel2  dl2;
      
      bc.f("main(1)");
      // bc.g(); // error:  BaseClass::g() is not accessible
      // bc.h(); // error: BaseClass::h() is not accessible
      d1l1.f("main(2)");
      // d1l1.g(); 
      d1l1.h("main(3)");
      d2l1.f("main(4)");
      // d2l1.g();
      // d2l1.h();
      dl2.f("main(5)");
      // dl2.g();
      dl2.h();
      
      return 0;     
    }
    Last edited by thames; 12-11-2012 at 10:17 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you using character strings instead of std::string?

    In the following snippet:
    Code:
          void f(char *s = "unknown")
          {
             cout << "Function f() in DerivedLevel2 called from " << s << endl;
             g("DerivedLevel2");
             Derived1Level1::h("DerivedLevel2");
             BaseClass::f("DerivedLevel2"); 
          }  
    
    int main()
    {
        bc.f("main(1)");
    In main you are calling this function with a constant character string, so your function should be setup to take a const character string.

    Jim

  3. #3
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    many thanks Jim for the reply. I'm following the 2nd edition of Data Structures and Algorithms in C++ and it was coded like that.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Considering the book is using C string literals and without const, no less, I would suggest you be careful with that book. It might be a bad book.
    Since I can find no good review on the book (much less so without an author name), the only advice I can give on that part is that you share some examples from different parts of the book so we can say if it's a good book or not. Learning bad examples can be a pain if/when you have to unlearn them later (and would suck big time for you).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    many thanks for the help Elysia. The book is this one: Data Structures and Algorithms in C++: Adam Drozdek: 9780534491826: Amazon.com: Books Data Structures and Algorithms in C++ 2nd edition by Adam Drozdek. Could you recommend me a c++ or C data structures book? thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help....String to Unsigned char conversion
    By whistle in forum C++ Programming
    Replies: 12
    Last Post: 10-28-2005, 01:55 PM
  2. char string to long conversion
    By rastan in forum C Programming
    Replies: 10
    Last Post: 11-06-2003, 07:28 PM
  3. conversion from string to char
    By erik_markman in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 02:10 PM
  4. string conversion to char*
    By NLarusso82 in forum C++ Programming
    Replies: 7
    Last Post: 02-27-2003, 07:52 AM
  5. String -> unsigned char array conversion....
    By SublicK in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2001, 12:18 PM

Tags for this Thread