strcpy类对象 [英] strcpy of a class object

查看:74
本文介绍了strcpy类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么操作员可以重载以便能够执行strcpy(pStr,

myObj)?


示例:


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(){

返回strlen(str);

}


char * str;

}

void main()

{

MyClass1 mc(这是一个测试);

const int size = mc.GetStrLen();

char * pStr = new char [size];


//我不想改变这一行!!

//应该在课堂上进行更改

strcpy(pStr,mc);

}

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);
}

推荐答案

该课应该是:


class MyClass {

公开:

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){

返回0;

}

返回strlen(str);

}

};

leonk ... @ gmail.com写道:
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);
}
};
leonk...@gmail.com wrote:
我需要做什么操作才能重载才能做strcpy (pStr,
myObj)?

示例:

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

int GetStrLen(){
返回strlen(str);
}

char * str;
}
void main()
{MyClass1 mc(这是一个测试);
const int size = mc.GetStrLen();
char * pStr = new char [size];

//我不想改变这一行!!
//应该改变在课堂上
strcpy(pStr,mc);
}
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);
}






* le ****** @ gmail.com




90%的新手错误是由于缺乏系统缩进造成的。


由于使用原始指针而导致10%。


修复和该问题(实际上是一堆)是固定的。


-

答:因为它弄乱了人们通常阅读文本的顺序。 />
问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么?电子邮件?



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.

--
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?


le******@gmail.com 写道:

我需要重载哪些运算符才能执行strcpy(pStr,
myObj)?

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




好​​吧。

strcpy采用什么作为它的第二个参数类型?

显然你需要一个转换
MyClass对象为该类型。


strFpy上的RTFM()

哦。并缩进你的代码


-

Karl Heinz Buchegger
kb ****** @ gascad.at


这篇关于strcpy类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆