Thread: Couple of Questions About Classes

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Couple of Questions About Classes

    Hi,

    I'm working through the book C++ Without Fear, and I'm trying to better understand some stuff in one of the examples. Basically, I have a couple of questions that relate to the following [modified] example code from the book:

    1. Lines 24-26 (marked by [Q1 relevant] in the code). If line 26 is commented out, when main runs, it will display c correctly as a series of spaces. But why should this be necessary?

    In the book, Brian Overland says that you can use a line like "operator int() {return atoi(ptr);}" to convert an object's type "whenever such a conversion would supply the only way to legally evaluate an expression".

    I don't understand why it makes a difference in this case. The variable c is just a series of spaces. Why should the program decide to activate the operator conversion function?

    2. Line 53-4 (marked by [Q2 relevant] in the code). If I uncomment line 54, the program crashes when it's run. How come?

    Code:
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    
    class String
    {
    private:
        char *ptr;
    public:
        String();
        String(int n);
        String(const char *s);
        String(const String &src);
        ~String();
        
        String& operator=(const String &src)
            {cpy(src.ptr); return *this;}
    
        String& operator=(const char *s)
            { cpy(s); return *this;}
    
        int operator==(const String &other);
        operator char*() {return ptr;}
        // [Q1 relevant] If the following line is commented out, c displays correctly in main.
        operator int() {return atoi(ptr);}
    
        void cpy(const char *s);
    };
    
    int main()
    {
        String a("3");
        cout << a << endl;
        String c(3);
        cout << "This is c: [" << c << "]";
        return 0;
    }
    
    // ------------------------
    // STRING CLASS FUNCTIONS
    
    String::String()
    {
        ptr = new char[1];
        ptr[0] = '\0';
    }
    
    String::String(int n) {
        // [Q2 relevant] If the following line is uncommented, the program crashes.
        // delete [] ptr;
        int i;
        ptr = new char[n + 1];
        for (i = 0; i < n; i++)
            ptr[i] = ' ';
        ptr[i] = '\0';
    }
    
    String::String(const char *s)
    {
        int n = strlen(s);
        ptr = new char[n + 1];
        strcpy(ptr, s);
    }
    
    String::String(const String &src)
    {
        int n = strlen(src.ptr);
        ptr = new char[n + 1];
        strcpy(ptr, src.ptr);
    }
    
    String::~String()
    {
        delete [] ptr;
    }
    
    int String:: operator==(const String &other)
    {
        return (strcmp(ptr, other.ptr) == 0);
    }
    
    // cpy -- Copy string function
    //
    void String::cpy(const char *s)
    {
        delete [] ptr;
        int n = strlen(s);
        ptr = new char[n + 1];
        strcpy(ptr, s);
    }
    Thanks for your help.
    Last edited by bengreenwood; 05-20-2009 at 11:38 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by bengreenwood
    2. Line 53-4 (marked by [Q2 relevant] in the code). If I uncomment line 54, the program crashes when it's run. How come?
    Code:
    String::String(int n) {
        // [Q2 relevant] If the following line is uncommented, the program crashes.
        // delete [] ptr;
    That's because ptr hasn't been initialized to point anywhere yet (and you should not attempt a delete on an uninitialized pointer). Since this is a constructor, a delete is pointless, but if you really wanted it there for some purpose, you could get things working with that line uncommented if you first initialized ptr to 0 (perhaps in the initializer list):
    Code:
    String::String(int n) : ptr(0) {
        delete [] ptr;
    Last edited by hk_mp5kpdw; 05-20-2009 at 12:32 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Thanks.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    // [Q1 relevant] If the following line is commented out, c displays correctly in main.
    If the following line is not commented out I get a compile error. The class itself doesn't overload operator<<, but there are two equally good conversions to choose from when you try to output it.

    Conversion operators themselves are somewhat problematic. They make it harder to trace what is going on in the code - are there any implicit conversions? - and they can make unexpected and otherwise wrong code compilable. There is a reason std::string provides a method called c_str to get a constant char*, and not operator const char*.

    An example of how conversion operator makes code compilable where it might not be desired. Streams provide operator void*, so they can be tested in boolean contexts, but this also makes this code acceptable:

    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << std::cout; //outputs address of cout, e.g 0x4463c4
    }
    (This may be not desirable, since it allows attempts to cout for example a stringstream - perhaps expecting it to output the contents of the stringstream, which won't happen.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. a couple of questions about System.String
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-17-2009, 02:48 PM
  3. A couple of Basic questions
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 09-26-2007, 04:06 PM
  4. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  5. A couple questions
    By punkrockguy318 in forum Tech Board
    Replies: 4
    Last Post: 01-12-2004, 10:52 PM