Hello everyone, i have not posted here in a while. But i have a question about a string class I'm creating. Every time i make a string class this always happens, and i cannot figure out why. Maybe its something i didn't include or something i have to add. But i hope someone here will be able to answer my question.

class StRiNg
	{
	public:
		StRiNg()
		{
			sTrLeN = 0;
			sTr = new char[1];
			sTr[0] = '\0';
		}
		StRiNg(char* s)
		{
			sTrLeN = (int)strlen(s);
			sTr = new char[(sTrLeN+1)];
			for(int i = 0; i < sTrLeN; i++)
				sTr[i] = s[i];
			sTr[sTrLeN] = '\0';
		}
		~StRiNg()
		{
			delete sTr;
			sTrLeN = 0;
		}

		StRiNg operator=(char* s)
		{
			try
			{
				delete sTr;
				sTrLeN = (int)strlen(s);
				sTr = new char[(sTrLeN+1)];
				for(int i = 0; i < sTrLeN; i++)
					sTr[i] = s[i];
				sTr[sTrLeN] = '\0';
			}
			catch(...)
			{
				return *this;
			}
			return *this;
		}

		bool operator==(char* s)
		{
			try
			{
				if((int)strlen(s) != sTrLeN)
					throw 0;
				bool same = true;
				for(int i = 0; i < sTrLeN && same; i++)
				{
					if(sTr[i] != s[i])
						same = false;
				}
				if(!same)
					throw 0;
			}
			catch(...)
			{
				return false;
			}
			return true;
		}
		bool operator!=(char* s)
		{
			if(*this == s)
				return false;
			return true;
		}

		char operator[](int num)
		{
			try
			{
				if(num < 0 || num > sTrLeN)
					throw 0;
				return sTr[num];
			}
			catch(...)
			{
				return '\0';
			}
			return '\0';
		}
		char* GetString() { return sTr; }
		int GetLength() { return sTrLeN; }
	protected:
		char* sTr;
		int sTrLeN;
	};

That is the base of my class, of course i will add other functions.. but here is my problem:

int main()
{
    StRiNg s1 = "Hello";
    StRiNg s2 = "How are you?";
    cout << s1.GetString() << endl; // works fine
    cout << s2.GetString() << endl; // works fine
    // Here is where the problem starts:
    s1 = s2.GetString();
    cout << s1.GetString() << endl; // should be How are you but it is far from that, i get random characters...
    return 0;
}

I would have added an = operator for other strings but if it doesn't even work this way, i don't know whats wrong.
And last: if anyone knows, is there a way i could do like cout << s1 << endl; without having to put the .GetString() like make a default return value for the StRiNg or overload cout to allow that?

Thanks a lot for any help anyone might give me. (even if i don't get any XD)

Try outputting the value of sTr immediately before leaving the try block in the assignment operator.

Nope still same error. Thanks anyway.

1. Wrong assignment operator (default member by member assignment can't work with such objects - think why).
2. No correct copy constructor (default copy constructor can't copy such objects). That's why your program crashed.
3. Some other defects ;)

StRiNg::StRiNg(const char* s) // const !
{
    if (s == 0) s = ""; // you forgot to test nullptr
    // now make as usually
}
StRiNg::StRiNg(const StRiNg& s) // copy constructor
{
    // make new dynamic copy of s
}
StRiNg& StRiNg::operator=(const StRiNg& s)
{
    if (this != &s) {
       delete [] sTr;
       // now make new dynamic copy of s   
    }
    return *this;
}
// GetString, bye forever:
operator const char*() const { return sTr; }

No needs in operator=(const char*) now: you have const char* to StRiNg constructor.

std::ostream& operator <<(std::ostream& os, const StRiNg& s)
{
    return os << sTr;
}

Now you can print your StRiNgS aS uSuAlLy.

Thanks a lot, i will try all these out.
IF anything i will post back here.

and if anyone else finds something please also post it.

Well i did all the stuff you told me, still have a few problems tho -.-.
First of all, i still get crazy characters after i copy...
Second of all: i get an error when i try to add:

std::ostream& operator <<(std::ostream& os, const StRiNg& s)
      {
      return os << sTr;
      }

But that is fine because

operator const char*() const { return sTr; }

does the job anyway.

but another prob is that when i pass the parameter as a string (const StRiNg& s)
i cannot do s.GetLength(); but that may be because of the const char* operator.

So overall i'm good, only prob is i'm still getting crazy characters.

Why are you using such weird naming like: "StRiNg"
??

Why don't you just call it 'mystring', 'mystrlen', etc. or something ?
It will be much easier to type ...
Unless you don't care by everytime type an UPPERCASE character, and after it a lowercase character ...

It's your choice ...

Well i use StRiNg so i don't get confused with any other string or String class already created... I made a file class and called it FiLe.
plus i like the look to StRiNg XD but that doesn't have much to do w/ this. I'm marking this as solved because i found my answer already. Thanks for all your help.

Well i use StRiNg so i don't get confused with any other string or String class already created... I made a file class and called it FiLe.
plus i like the look to StRiNg XD but that doesn't have much to do w/ this. I'm marking this as solved because i found my answer already. Thanks for all your help.

OK, I understand your way to do it, as I said it's only a matter of choice ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.