-1

I'm trying to make a class that contains a pointer, which is the core of a dynamic array. How to overload the operator >> so I can cin >> to the pointer without know how many characters are going to put? I was trying:

#include <iostream>
class MyString
{
private:
    char* str;
    size_t length;

public:
    //some code here

    friend std::istream& operator>>(std::istream& is, const MyString& other)
    {
        is >> other.str;
        return is;
    }
}

and this happened when I try to cin a string: HEAP CORRUPTION DETECTED

Is there a way to read a string, assign it to my string, and won't use the std::string header? (I was trying to make a string class that doesn't depend on std::string)

Alex Cai
  • 35
  • 4
  • A naive implementation would use `is.get()` to retrieve one character at a time, and append it to `str`, resizing if needed. And stop when `std::isspace(chr)` is true, of course. For that, you need to dynamically allocate memory and keep track of how much space you are using / have available. – Botje Sep 01 '21 at 09:32
  • The `str` member in your class needs to be initialised so it points at something (e.g. dynamically allocated memory) BEFORE reading to it (e.g. `is >> other.str`). As shown, the `str` is uninitialised, so `is >> other.str` gives undefined behaviour. Normally, dynamic memory allocation for initialisation purposes is done in a constructor - if the dynamically allocated memory needs to be resized, it is necessary to write code that (1) allocates a new memory block (2) copies data from the old memory block to the new memory block (3) releases the old memory block. – Peter Sep 01 '21 at 09:32
  • Note also that a pointer is not an array. The member `str` is a pointer, and that pointer is uninitialised (so doesn't point at anything in particular). A key consideration, if you want to write code that mimics an array, is to understand that there are VERY REAL differences between pointers and arrays (even though they can be used interchangeably in SOME CONTEXTS, they are not the same thing, so are not always interchangeable). – Peter Sep 01 '21 at 09:35

2 Answers2

0

A naive implementation would use is.get() to retrieve one character at a time, and append it to other, resizing if needed. And stop when std::isspace(chr) is true, of course. For that, you need to dynamically allocate memory and keep track of how much space you are using / have available in the implementation of MyString.

Here is the skeleton for that; you probably need to implement the append() method.

friend std::istream& operator>>(std::istream& is, MyString& other) {
   while (true) {
      int chr = is.get();
      if (is.eof() || std::isspace(chr)) {
          break;
      }
      other.append((char)chr);
   }
   return is;
}
Botje
  • 26,269
  • 3
  • 31
  • 41
0
#include <iostream>
#include <string.h>
class MyString
{
private:
    size_t length;
    char* str;
    void getInput(std::istream&is)
    {
      char c='\0';

      while(c!='\n')
      {
        is.get(c);
         length++;
        str=(char*)realloc(str, length);
        
        if(c=='\n')
        {
        str[length-1]='\0';
        }
        else{
          str[length-1]=c;
        }
      }
    }

public:
    MyString()
    {
      length=0;
      str=new char[1];
      str[0]='\0';
    }
    //some code here
    friend std::istream& operator>>(std::istream& is,  MyString& other)
    {
        std::cout<<"Enter String: ";
        other.getInput(is);
        return is;
    }

    void printString()
    {
      std::cout<<str<<std::endl;
    }

    ~MyString()
    {
      delete[]str;
      length=0;
    }
};

int main()
{
  MyString s;
  std::cin>>s;

  s.printString();
}
Abdullah Arshad
  • 156
  • 2
  • 6