1

I am writing my own operating system in C++, the problem though is that the regular string.h file doesn't work properly, so I need to create my own string class (which I want to do anyway - string.h working or not) but I am having trouble figuring out how to write it so I can use it in the following way:

string a = "Hello";
int i = a.length();

or even

int i = a.length;

Currently my string.h looks kind of like this:

typedef char *string;
class String {     //same name of typedef and class means compilation error
    char *value;
    int length(string value);

But using that implementation it would have to be used like:

String a = new String;
a.value = "Hello World";

I am sorry if this is the wrong website for such "theoretical" questions, but please direct me to another more suitable place for this question if this is the case.

Andro
  • 2,232
  • 1
  • 27
  • 40
  • 1
    Did you try writing so? If yes, what specific trouble are you facing with? – MikeCAT May 04 '21 at 12:16
  • `string a = "Hello";` is unrelated to the header `string.h`. `string.h` is a C header for C strings. The C++ version for C strings is `cstring`. `std::string` is declared in `string`. –  May 04 '21 at 12:16
  • 5
    Don't use "string.h" its C not C++. You need to use and `std::string`. – JHBonarius May 04 '21 at 12:17
  • Are you asking about [implicit conversions](https://en.cppreference.com/w/cpp/language/implicit_conversion)? Btw you should really learn C++ deeply before even starting to write an operating system. You may for example learn that there are literally thousands things more interesting to do. – freakish May 04 '21 at 12:20
  • Your code works for me: https://wandbox.org/permlink/C58WwiUnGcAgE8YQ –  May 04 '21 at 12:20
  • @jabaa `string` is not `std::string` – 463035818_is_not_an_ai May 04 '21 at 12:20
  • @largest_prime_is_463035818 I wrote my comment before the edit. But my comment is still true. OP's `string` is unrelated to `string.h` (if it's the standard `string.h`). –  May 04 '21 at 12:21
  • Did you read a good [C++ programming book](https://www.stroustrup.com/programming.html) - on paper? – Basile Starynkevitch May 04 '21 at 12:22
  • @jabaa original post: "so I need to create my own string class". Yes, probably they should be using `std::string` and call it a day, but lets not confuse them when thats not what they ask for – 463035818_is_not_an_ai May 04 '21 at 12:23
  • It looks like you have lots of misconceptions about what classes/structs/methods are in C++, I'm unsure how to answer your question as it is right now. Please consider reading a good book or taking a course in C++ which covers the basics. – yeputons May 04 '21 at 12:23
  • You shouldn't create a class `string` or a file `string.h` to avoid confusion. Both already exist. –  May 04 '21 at 12:24
  • `String a = new String;` is wrong unless a constructor like `String(String* s)` is defined. `a.value = "Hello World";` is wrong because a string literal (converted to `const char*`) is assigned to `char*` variable. – MikeCAT May 04 '21 at 12:25
  • @jabaa sorry, me again. The class that exists is called `std::string`. Its completely fine to declare a `string` in a different namespace, thats what namespaces are good for (introducing all names from `std` via a using is to be blamed for potiential confusion) – 463035818_is_not_an_ai May 04 '21 at 12:28
  • You may be interested in [Haiku](https://www.haiku-os.org/), which is an open source operating system written in C++. – Eljay May 04 '21 at 12:49
  • @largest_prime_is_463035818 It's not wrong but a line like `string a = "Hello";` will cause confusion if you don't tell that this is a user defined type. –  May 04 '21 at 13:12
  • @jabaa depends on how much you are used to `using ...`. I am used to code where `string` and `std::string` are not the same. – 463035818_is_not_an_ai May 04 '21 at 13:16
  • @largest_prime_is_463035818 Yes, in my teams `using namespace std;` is not allowed an I know that `string` and `std::string` are probably not the same but on Stackoverflow `string` is almost always `std::string` especially for beginners. I remember my first year of C++. My teacher taught me that `using namespace std;` is a necessary line without explanation. –  May 04 '21 at 13:25
  • 1
    @jabaa you cannot blame OP for not following bad practice ;) – 463035818_is_not_an_ai May 04 '21 at 13:26

2 Answers2

1

Most of the time we use the standard library string implementation. That can be included with the following line:

#include <string> 

And then use it in the following fashion:

std::string who = "me"; 
int len = who.size();

But since you are learning, you seem passionate about implementing your own string implementation. Which is a very good practice. So I quickly grabbed a header file for string implementation online:

mystring.h

class mystring
{
    char *pcString; // pointer to a char array
    int iCapacity; // capacity; strings grow
public: // note the public here
    // some constructors
    mystring(void);
    mystring(int);
    mystring(const char*);
    mystring(const string&);
    // some ops
    mystring& string::operator=(const string &strInstance);
    mystring& string::operator+(const string &strInstance);
    // your custom length
    int length() const; 
};

Extra: Code review stack exchange stdstring-implementation

Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34
0
string a = "Hello";

That will not work in C++11 and later, because you have declared string as a typedef for a non-const char* pointer, but the string literal "Hello" is a const char[6] that decays into a const char* pointer. You can't assign a const char* to a non-const char*.

However, prior to C++11, you could assign a string literal to a non-const char* pointer, but that practice was discouraged as it promoted the possibility of modifying read-only data. Which is why it is no longer allowed in C++11 and later.

You likely meant to use your String class instead:

String a = "Hello";

But that will not work either, because your String class lacks a constructor that accepts a const char* as input.

int i = a.length();
int i = a.length;

Neither of those will work, because a is declared as a char* pointer, so it has no methods or data members. But, even if you had used your String class instead, a.length() would still not work because length() is implicitly private and it takes a char* as input, which you are not passing in.

typedef char *string;
class String {     //same name of typedef and class means compilation error

They don't have the same name. C++ is case-sensitive, so string and String are different names.

But using that implementation it would have to be used like:

String a = new String;
a.value = "Hello World";

Wrong. If implemented properly, it can be used like this:

String a = "Hello World";

In order for that to work, your String class would need to look more like this:

class String {
private:
    char *value;

public:
    String(const char* str);
    ~String();
    int length() const;
};

And then you can implement it like this:

#include <cstring>

String::String(const char* str)
    : value(NULL)
{
    if (str) {
        value = new char[std::strlen(str) + 1];
        std::strcpy(value, str);
    }
}

String::~String()
{
    delete[] value;
}

int String::length() const
{
    return (value) ? std::strlen(value) : 0;
}

Now this will work, as expected:

String a = "Hello";
int i = a.length();

Demo

Obviously, this is just an example, there are better ways to implement this. Like caching the length value in a data member. And implementing a copy constructor and a copy assignment operator, and a move constructor and a move assignment operator, per the Rule of 3/5/0.

Now, that being said, C++ already has a standard std::string class that handles all of these details for you. Implementing a custom String class is fine as an exercise in learning how to implement classes, but in real production code, use std::string instead, eg:

#include <string>

std::string a = "Hello";
size_t i = a.length(); // or a.size()
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770