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;
}