SOLVED: I need help creating a simple C++ string class


Keywords:c++ 


Question: 

I'm writing a program where I create my own string class. The program is supposed to instruct the user to write a sentence, copy the input, and then display how many characters it contains.

Although it did build and compile (using Visual Studio Express 2013), it would copy the sentence the user is instructed to write down, not display the amount of characters, and then crash.

I'm guessing is has something to do with how the memory is dynamically allocated or how *character and _String is passed between functions. But some help on how to fix this or improve my code would be very helpful.

Header: String.h

class String
{
private:
    char *characters[50]; // 50 is just a placeholder to get it to work
    int size;
public:
    // Constructors and Destructors
    String();
    String(char*);
    ~String();

    // Other functions
    void stringAssign(char*);
    void printString(char*);
    int stringSize(char*) const;
};

Implementation: String.ccp

#include "String.h"
#include <iostream>
#include <cstring>

String::String()
{
    size = 0;
    * characters = 0;
}

String::String(char* _String)
{
    size = stringSize(*characters + 1);
    _String = new char[size];
}

String::~String()
{
    delete [] *characters;
}

void String::stringAssign(char* _String)
{
    _String = characters[size];
}

void String::printString(char* _String)
{
    for (int i = 0; _String[i] != '\0'; i++)
        std::cout << _String[i];
}

int String::stringSize(char* _String) const
{
    int size;
    size = strlen(*characters);
    return size;
}

Test: Main.ccp

#include "String.h"
#include <iostream>

int main()
{
    char string[50];
    String s;

    std::cout << "Enter a sentence that is less than 50 characters\n\n";
    std::cin.getline(string, 50);

    s.printString(string);
    std::cout << s.stringSize(string);

    return 0;
}

Update:

I got the program working. Literally all I did was replace the two *character arguments with _String in the implementation. I knew it had something to do with function passing, and I did get a little help for that. But one person gave no helpful input (and practically insulted me) and both said that there were "too many things to fix". Some elaboration on the "so many things" I had to fix would be nice.


1 Answer: 

In almost all functions, you do something with the argument of the function, _String, and not with the member field characters that is supposed to hold the string internally. So your functions do not change the string at all, but rather try to mess with something you passed them.

An example

struct Foo {
  char internal[42];
  void bar(char *external) { external[0] = 'X'; }
  void qux(char *external) { internal[0] = 'Z'; }
};
int main() {
  Foo foo;
  char something[6] = "Hello";
  foo.qux(something);  // Did something to the member variable foo.internal; variable something is unchanged.
  foo.bar(something);  // Now something reads "Xello", foo.internal was not changed.
}

Your code does almost as the function bar while it should work like qux.

There is indeed too much to fix in this code.

Just in case you were mistaken... Unlike some other languages, say Python or Smalltalk, C++ passes a pointer to the current object implicitly via this.