Concatenation

I created a function for concatenating c-string objects and it seems to be working. However, I get a green squiggly line from visual studio and when I place the cursor over it, a little window pops up and says, "Avoid unnamed objects with custom construction and destruction." What does this mean and what should I do to fix it?

1
2
3
4
5
6
7
8
9
10
 const MyString operator + (const MyString& left, const MyString& right)
	{
		MyString hold;

		hold = new char[strlen(left.ptr) + strlen(right.ptr) + 1]; // Green line under this whole statement.
		strcpy(hold.ptr, left.ptr);
		strcat(hold.ptr, right.ptr);

		return hold;
	}
You assign a char* to hold that is a MyString. The MyString is not a pointer. Does it have:
1
2
3
MyString::operator= ( char* rhs ) {
  ptr = rhs; // What if ptr did alreay point to somewhere?
}


Why doesn't the MyString have a constructor:
1
2
3
4
MyString::MyString( size_t N )
 : ptr( new char[N] )
{
}

to do:
MyString hold( strlen(left.ptr) + strlen(right.ptr) + 1 );


The point of the warning is that you allocate dynamic memory explicitly in a function, but apparently transfer the responsibility about deallocation to a class object. It is like you would do homework as a pair with someone: one writing code and other being responsible for the code working correctly.

That could work.


I presume that you have written custom default constructor, copy constructor, copy assignment, destructor (and perhaps move constructor and move assignment) members for your MyString?
Yes I do have other functions. Sorry I didn't post them as I didn't know what was useful to see or just clutters up the screen. I tried changing the MyString Parameters out in the + function for char or char* and that seemed to make it worse by giving red lines under ptr.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
namespace cs_mystring
{
	class MyString
	{
	public:
		MyString();
		MyString(const char* inString);
		MyString(const MyString& right);
		~MyString();
		friend std::ostream& operator << (std::ostream& out, const MyString& right);
		friend std::istream& operator >> (std::istream& strm, MyString& right);
		char& operator [] (int index);
		char operator [] (int index) const;
		friend bool operator < (const MyString& left, const MyString& right);
		friend bool operator <= (const MyString& left, const MyString& right);
		friend bool operator > (const MyString& left, const MyString& right);
		friend bool operator >= (const MyString& left, const MyString& right);
		friend bool operator == (const MyString& left, const MyString& right);
		friend bool operator != (const MyString& left, const MyString& right);
		MyString operator = (const MyString& right);
		size_t length()const;
		void read(std::istream& readString, char stopRead);
		friend const MyString operator + (const MyString& left, const MyString& right);
		

	private:
		char* ptr;
		static const int MAX_INPUT_SIZE = 127;
	};
}




// in main
// test works on
MyString s1("hello "), s2("world");
	
	cout << s1 + s2 << endl << endl;

}





MyString::MyString()
	{
		ptr = new char[1];
		strcpy(ptr, "");
	}




MyString::MyString(const char* inString)
	{
		ptr = new char[strlen(inString) + 1];
		strcpy(ptr, inString);
	}




MyString::~MyString()
	{
		delete[] ptr;
	}




MyString MyString::operator = (const MyString & right)
	{
		if (this != &right) {
			delete[] ptr;
			ptr = new char[strlen(right.ptr) + 1];
			strcpy(ptr, right.ptr);
		}
		return *this;
	}

Topic archived. No new replies allowed.