strcpy of a class object

L

leonkatz

What operators do I have to overload to be able to do a strcpy(pStr,
myObj) ?

Example:

class MyClass {
public:
MyClass(char* pStr){
if(pStr == NULL) {
str = NULL;
} else {
const int len = strlen(pStr);
str = (char*)malloc(len);
strcpy(str, pStr);
}
};

int GetStrLen() {
return strlen(str);
}

char* str;
}
void main()
{
MyClass1 mc("this is a test");
const int size = mc.GetStrLen();
char *pStr = new char[size];

// I do not want to change this line !!
// Change should be made in the class
strcpy(pStr, mc);
}
 
L

leonkatz

The class was supposed to be:

class MyClass {
public:
MyClass(char* pStr){
if(pStr == NULL) {
str = NULL;
} else {
const int len = strlen(pStr);
str = (char*)malloc(len);
strcpy(str, pStr);
}
}

int GetStrLen() {
if(str == NULL) {
return 0;
}
return strlen(str);
}
};
 
A

Alf P. Steinbach

* (e-mail address removed):
90% of novice errors are due to lack of systematic indentation.

10% due to using raw pointers.

Fix that and "the" problem (actually a bunch of them) is fixed.
 
K

Karl Heinz Buchegger

What operators do I have to overload to be able to do a strcpy(pStr,
myObj) ?

Well.
What does strcpy take as it's second argument type?
Obviously you will need an operator that converts a
MyClass object to that type.

RTFM on strcpy()


Oh. And indent your code
 
I

Ivan Vecerina

What operators do I have to overload to be able to do a strcpy(pStr,
myObj) ?
What you could do:
- is overload the strcpy function itself
... but using a differently named function
would be safer and less error prone.
- add an implicit 'const char*' conversion operator to MyClass
... but a named member function would be better

Neither option is good. And given the errors in the
short sample code you posted, I would strongly recommend
that you start by using and studying std::string ,
which is part of the C++ language.
Example:

class MyClass {
public:
MyClass(char* pStr){
if(pStr == NULL) {
str = NULL;
} else {
const int len = strlen(pStr);
str = (char*)malloc(len);
You need to allocate *len+1* here,
or undefined behavior will result.
strcpy(str, pStr);
}
}; extraneous ';' here

int GetStrLen() {
return strlen(str);
}

char* str;
}
missing ';' here.
void main()
{
MyClass1 mc("this is a test");
const int size = mc.GetStrLen();
char *pStr = new char[size];
again, size+1 has to be allocated.
// I do not want to change this line !!
// Change should be made in the class
strcpy(pStr, mc);
}


hth,
Ivan
 
S

Serge Paccalin

Le mardi 18 janvier 2005 à 07:04, Alf P. Steinbach a écrit dans
comp.lang.c++ :
90% of novice errors are due to lack of systematic indentation.

And 90% of lack of indentation in Usenet posts is caused by Google
web-to-news interface messing it up, rather than the poster not typing
it.

--
___________ 2005-01-18 12:39:02
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
A

Alf P. Steinbach

* Serge Paccalin:
Le mardi 18 janvier 2005 à 07:04, Alf P. Steinbach a écrit dans
comp.lang.c++ :


And 90% of lack of indentation in Usenet posts is caused by Google
web-to-news interface messing it up, rather than the poster not typing
it.

It's hard to believe that programmers preferentially use Google.

Especially now, after Google stopped being our friend (beta groups
UI, millions broken links, searchability, millions broken links,
character set, millions broken links, unstability, millions broken
links, censorship, millions broken links, China...).

For the few newbies who post code: DO NOT USE GOOGLE TO POST CODE.
 
R

Russoue

What is the preferred way to view Usenet posts?
* Serge Paccalin:

It's hard to believe that programmers preferentially use Google.

Especially now, after Google stopped being our friend (beta groups
UI, millions broken links, searchability, millions broken links,
character set, millions broken links, unstability, millions broken
links, censorship, millions broken links, China...).

For the few newbies who post code: DO NOT USE GOOGLE TO POST CODE.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
D

Dietmar Kuehl

Russoue said:
What is the preferred way to view Usenet posts?

Well, that's easy to answer: A "real" newsreader program. There are
many newsreaders around as NNTP is a very simple protocol. The
format of news articles is quite similar to the mail format and
essentially trivial to process.

The tricky part is that these are often not available everywhere.
For example, I can't use a newsreader from here and thus I go
through Google news which indeed sucks. However, the indentation
problem can reasonably be addressed by prepending each line of
code with some character, e.g. a pipe ('|'). If anybody has a
good alternative, I would like to hear about it...
 
R

Ron Natalie

Serge said:
And 90% of lack of indentation in Usenet posts is caused by Google
web-to-news interface messing it up, rather than the poster not typing
it.

Actually, that's probably only about 40% of it. The other 50% is outlooks
misformatting of the messages as well.
 
A

Andrew Koenig

What operators do I have to overload to be able to do a strcpy(pStr,
myObj) ?

Don't do it. Don't even try.

Strcpy is intended to copy null-terminated character arrays. Your class is
not a null-terminated character array. Why do you want to pretend it is?
 
A

Alex Vinokur

Dietmar Kuehl said:
Well, that's easy to answer: A "real" newsreader program. There are
many newsreaders around as NNTP is a very simple protocol. The
format of news articles is quite similar to the mail format and
essentially trivial to process.

The tricky part is that these are often not available everywhere.
For example, I can't use a newsreader from here and thus I go
through Google news which indeed sucks. However, the indentation
problem can reasonably be addressed by prepending each line of
code with some character, e.g. a pipe ('|'). If anybody has a
good alternative, I would like to hear about it...
[snip]

Maybe via
http://www.groupsrv.com/computers/viewforum.php?f=109
http://www.talkaboutprogramming.com/group/comp.lang.c++
http://www.codecomments.com/forum272/

P.S. I didn't test indentation in postings via those sites.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top