-7

Could someone explain me a few lines?

1) char* Buffer; What are we doing with this? Declaring a pointer of type char? Why do we not initialize it?

2) MyString(const char* InitialInput) So this contructor(method) takes some string, turns it into a constant, basically, and assigns it to an address? Why constant and why pointer? Why can't we just write char InitialInput?

3) const char* InitialInput Why is there a derefernce operator as well as constant here? As I understand this is a method? What's wrong with just writing char GetString()?

Changing some of these to the way I "want" results in deprecated conversion from string constant to 'char*' Not sure what this mean...

#include <iostream>
#include <string.h>
using namespace std;

class MyString 
{
private:
    char* Buffer;

public:
    // constructor
    MyString(const char* InitialInput)
    {
        if(InitialInput != NULL)
        {
            Buffer = new char [strlen(InitialInput) + 1];
            strcpy(Buffer, InitialInput);
        }
        else
            Buffer = NULL;
    }
    // destructor 
    ~MyString()
    {
        cout << "Invoking destructor, clearing up" << endl;
        if (Buffer != NULL)
            delete[] Buffer;
    }

    int GetLength()
    {
        return strlen(Buffer);
    }

    const char* GetString()
    {
        return Buffer;
    }
};

int main()
{
    MyString SayHello("I am saying hello to you!");
    cout << "String buffer in MyString is " << SayHello.GetLength();
    cout << " charecters long" << endl;

    cout << "Buffer contains: " << SayHello.GetString() << endl;
}
Max
  • 243
  • 2
  • 4
  • 9
  • 7
    You need to learn the basics of the language. – Barmar Dec 31 '14 at 10:52
  • 1
    Here, [get a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Hiura Dec 31 '14 at 10:53
  • 2
    As @Barmar suggests, get a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start with chapter one - you can't learn to program by guess-work – Paul R Dec 31 '14 at 10:53
  • What basics exactly? It's from http://www.amazon.com/Sams-Teach-Yourself-Hour-Edition/dp/0672335670 and I've worked through pointers, using const with pointers, and dynamically allocating memory, and the author threw this example without explaining some stuff in a chapter on Destructors. For instance I know what * means, but it hasn't been explained why it was used in a class like this char* Buffer;... I'm pretty sure I've worked well thru the book until this chapter. I don't know maybe it's not a good book? Or maybe this wasn't yet supposed to be explained? – Max Dec 31 '14 at 11:04

1 Answers1

0

A char* is typically used to point at the first element in an array of chars, where one of the chars in the array is \0 and denotes the end of a string. We refer to such char*s as C-style strings. That is, this type represents a string of characters, whereas a single char represents only a single character.

A const char* is similar (still a C-style string), but says that we do not wish to modify the chars that are being pointed at (we only need to look at them).

char* Buffer; is a member of the MyString class. Yes, it is not being initialised, but it is being assigned to in the constructor of the class. This occurs before any other use of the member, so it's safe.

The constructor takes a const char* because it needs a string of characters to fill the Buffer with. GetString returns a const char* because it returns the string of characters from the Buffer.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Hum, thanks a lot. I know about most of these facts separately, but just couldn't fit in my head. For instance I didn't know you should still write const char* for a return method, because that just seemed unnatural, since we're not declaring anything here and just returning. But I think all of this more or less makes sense now. Thanks again. Don't know why people are so mad :) – Max Dec 31 '14 at 11:15
  • @Max `const char*` is a type. You can use it anywhere a type is expected. A return type is a type. An argument type is a type. – Joseph Mansfield Dec 31 '14 at 11:45