472,340 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,340 software developers and data experts.

std::string copy constructor fails

Hi,

I have a class that does not seem to work.
I cannot see the problem, and the "fix" I have found does not help me
understand what the problem was

I know I don't need a copy constructor but the class might grow later and I
prefer having my copy constructor.
This also explains why I have 2 similar function "ClearAll() and NullAll()"
they might be used later in case I have pointers.

I use the class in a vector declared
std::vector< CMyClass > m_vMyClass;
and all I do is push_back(...), erase(...) and operator[...].

// this is the original class.
class CMyClass
{
private:
std::string m_sID;
std::string m_sName;
public:
void NullAll()
{
m_sID = ""; //<== Problem 1
m_sName = ""; //<== Problem 2
}

void ClearAll()
{
m_sID = ""; //<== Problem 3
m_sName = ""; //<== Problem 4
}

~CMyClass()
{
ClearAll();
}
CMyClass()
{
NullAll();
}
CMyClass( const CMyClass&mc )
{
NullAll();
*this = mc;
}
const CMyClass& operator=(const CMyClass&mc)
{
if( this != &mc )
{
NullAll();
ClearAll();

m_sID = mc.m_sID; //<== Problem 5
m_sName = mc.m_sName; //<== Problem 6
}
return *this;
}

}

///////////////////////////////////////////////////

All the problems where causing some sort of memory assertions.

My 'Fixes' for problem 1, 2, 3 and 4 were
m_sID ="" to
m_sID.erase() and

m_sName ="" to
m_sName.erase();

And the fix for problem 5 and 6 were
m_sID = mc.m_sID to
m_sID = std::string( mc.m_sID.c_str() ); and

m_sName = mc.m_sName to
m_sName = std::string( mc.m_sName.c_str() );

It works but I am not sure why the original code was causing a memory error.

Many thanks in advance.

Simon.
Dec 7 '05 #1
1 5136

Simon wrote:
Hi,

I have a class that does not seem to work.

I know I don't need a copy constructor but the class might grow later and I
prefer having my copy constructor.
This also explains why I have 2 similar function "ClearAll() and NullAll()"
they might be used later in case I have pointers.
That's probably a bad idea. You shouldn't think about pointer members
being 0
in a high-level class. More likely, you should think about optional
members
being not present. Also, it you use smart pointers for your members,
you get
these functions for free on the smart pointer class.
I use the class in a vector declared
std::vector< CMyClass > m_vMyClass;
and all I do is push_back(...), erase(...) and operator[...].
Ok - that seems like it can only fail if erasing an invalid iterator,
or
indexing out of bounds. Both are likely to corrupt memory.
// this is the original class.
class CMyClass
{
private:
std::string m_sID;
std::string m_sName;
public:
void NullAll()
{
m_sID = ""; //<== Problem 1
m_sName = ""; //<== Problem 2
} ....
All the problems where causing some sort of memory assertions.


Not because of those lines, but because your memory check mechanism
detected earlier corruption at that point. Fixes to those lines just
hide the bug.

HTH,
Michiel Salters

Dec 7 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alexandros Frantzis | last post by:
Hello, I am trying to read a big file into a string. AFAIK std::string can read in words or "lines" from a stream. So one option is to continuously...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) ...
8
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: ...
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly...
6
by: Erik | last post by:
Hello, For many years ago I implemented my own string buffer class, which works fine except assignments - it copies the char* buffer instead of...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
2
by: suman.nandan | last post by:
Hi Experts, In the following code (sorry for using C printf in the code !) : ---------------------------------------------- #include <string>...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.