Error : const copy constructor invoking non const methods

I have modified the copy constructor as constant in order to fix the compilation error.Again the compiler throws an error,because calling a non-const method from a const method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

String.h

class String
{
        private:
                char *_text;
                friend class String_Iterator;
        public:
                // ctors and dtor
                explicit String ();                     
                explicit String (char *);
                explicit String (int );
                String (String const & );              
                ~String();

                /////////////////
                // conversions:
                /////////////////
                //  to C-like type char *
                operator char *() {return _text;}

                /////////////////
                // overloads:
                /////////////////
                // assignment String = String
                String &operator=(String &);
}

String.c

String::String()
{
        _text = new char[1024];
}
String::String(char *ch)
{
        if (ch == NULL )
        {
                // if input char* is empty - allocate short buffer
                // and set it to ""
                _text = new char[2];
                strcpy(_text, "");
        }
        else
        {

                _text = new char[strlen(ch) + 1];

                if(_text)
                         strcpy(_text, ch);
                else
                {
                        _text = new char[2];
                        strcpy(_text, "");
                }
        }

}
String::String(int iLen)
{
        _text = new char[iLen+1];
}
String::String(String const& string)//modified copy cons as const
{
       _text = new char[string.length() + 1];

       strcpy(_text,string);
}
String::~String()
{
        delete[] _text;
}
String &String::operator=(String &ptrStr)
{

        delete[] _text;
        _text = new char[ptrStr.length() + 1];

        strcpy(_text, ptrStr);
        return *this;
}
String &String::operator=(char *ch)
{
        delete[] _text;
        _text = new char[strlen(ch) + 1];

        strcpy(_text, ch);
        return *this;
}




Error:
String.c: In copy constructor 'String::String(const String&)':
String.c:49: error: passing 'const String' as 'this' argument of 'int String::length()' discards qualifiers
String.c:51: error: passing 'const String' as 'this' argument of 'String::operator char*()' discards qualifiers
String.c:51: error: invalid conversion from 'char*' to 'const char*'
String.c:51: error: initializing argument 2 of 'char* strcpy(char*, const char*)'
make: *** [all] Error
Please check an dhelp me to resolve the issue ..
Thanks for looking into this
I see a problem at line 68.
 
strcpy(_text,string);


You define a cast at line 21 which returns a char *, which is mutable.
This is what is being passed to strcpy. This violates the const declaration of the parameter. You need to define a second cast.
1
2
operator const char *() const 
{ return _text; }

This should fix the problem.

Thank You! your solution fixes the problem
Topic archived. No new replies allowed.