关于函数的返回类型! [英] About the return type of the function !

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

问题描述

兄弟,

i面临的问题如下:


class poly

{

私人:

char name [20];

int capacity;

public:

poly( char [],int);

char * gettername();


};


poly :: poly(char a [],int cap)

{

int counter = 0;

counter = strlen(a);

for(int i = 0; i< counter; i ++)

{

name [i] = a [i];

}


}

char * poly :: gettername()

{

//这里我想返回存储在char

数组中的名称(私有数据)。这是最好的方法吗?这样做..并解释

这样做的原因。

}


int main()

{


返回0;

}

Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}

推荐答案

Aff @ n写道:
Aff@n wrote:

兄弟,

i面临的问题如下:


class poly

{

private:

char name [20];
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];



为什么使用固定长度的char缓冲区而不是std :: string?

Why are you using fixed length char buffer instead of std::string?


int capacity;

public:

poly(char [],int);

char * gettername();


};


poly :: poly(char a [],int cap)

{

int counter = 0;

counter = strlen(a);
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);



strlen()返回一个size_t,这样可以使计数器显示为

类型。

strlen() returns a size_t, it would be bettert to make counter that
type.


for(int i = 0; i< counter; i ++)

{

name [i] = a [i];

}
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}



这无法复制空终止符。如果你的意图是拥有C风格的字符串,那可能不是你想要的b / b $

This fails to copy the null terminator. That''s probably not what you
want if your intent is to have C-style strings.


}

char * poly :: gettername()

{

//这里我要返回存储在char中的名称

数组(私有数据).which是最好的方法吗?
}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?



没有最好的办法。将它私有化是什么意思

提供了一个访问者?


如果不知道你的总体设计目标是什么,那就不可能了回答问题。

暂定回复是转储缓冲区,使用std :: string,根据需要返回

副本或者将需要访问会员的代码

移到课堂上。


Brian

There is no best way. What''s the point of having it private it you
provide an accessor?

Without knowing what your overall design goals are, it''s impossible to
answer the question.
A tentative reply is dump the buffer, use std::string, either return a
copy as needed or move the code that needs the access to the member
into the class.


Brian



Aff @ n写道:

Aff@n wrote:

兄弟,

i面临的问题是按照:


class poly

{

private:

char name [20];

int capacity;

public:

poly(char [],int);

char * gettername() ;


};


poly :: poly(char a [],int cap)

{

int counter = 0;

counter = strlen(a);

for(int i = 0; i< counter; i ++)

{

name [i] = a [i];

}


}

char * poly :: gettername()

{

//这里我要返回存储在char

数组中的名称(私人数据)。这是最好的方法吗?并解释了做这种事情的原因。

}


int main()

{


返回0;

}
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}



todo最好的第一个使另一个不会破坏的对象

封装,

char * poly :: gettername()

{

char * temp;

temp = new char [20];

strcpy(temp,name);

返回temp; //这不会破坏内存直到被破坏所以你需要小心使用数据。

}

todo the best 1st make an other object which will not break
encapsulation,
char* poly::gettername()
{
char *temp;
temp = new char [20];
strcpy(temp,name);
return temp;// this will not destroy the memory untill destroyed so u
have to be carefull usning dat.
}


" Aff @ n" < af ******** @ gmail.com写信息

新闻:11 ********************** @ k70g2000cwa.googlegr oups.com ...
"Aff@n" <af********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...

兄弟,

i面临的问题如下:
< br $>
class poly

{

private:

char name [20];

int capacity;

public:

poly(char [],int);

char * gettername();


};


poly :: poly(char a [],int cap)

{

int counter = 0;

counter = strlen(a);

for(int i = 0; i< counter; i ++)

{

name [i] = a [i];

}


}

char * poly :: gettername()

{

//这里我要返回存储在char

数组中的名称(私有数据) )。这是最好的方法吗?这样做..并解释了做这种事情的原因。

}


int main()

{

返回0;

}
Brothers ,
i am facing a problem which is as follow:

class poly
{
private:
char name[20];
int capacity;
public:
poly(char [], int );
char* gettername();

};

poly::poly(char a[],int cap)
{
int counter=0;
counter=strlen(a);
for(int i=0;i<counter;i++)
{
name[i]=a[i];
}

}
char* poly::gettername()
{
// here i want to return the name which is stored in the char
array(private data) .which is best way ?to do so ..and explain the
reason of doing such thing .

}

int main()
{

return 0;
}



这是std ::的问题之一字符串是设计的。只需将

命名为std :: string并返回它,你的问题就会消失。


class poly



私人:

std :: string name;

int capacity;

public:

poly(std :: string&,int);

std :: string gettername();

};


poly :: poly(std :: string a,int cap)

{

name = a;

}


std :: string poly :: gettername()

{

返回名称;

}


你有没有理由不使用std :: string?你甚至可以使用

c风格的字符串来初始化它.IE


poly MyInstance(" This Name",10);

This is one of the problems for which std::string was designed. Just make
name a std::string and return it and your problem goes away.

class poly
(
private:
std::string name;
int capacity;
public:
poly( std::string&, int );
std::string gettername();
};

poly::poly( std::string a, int cap )
{
name = a;
}

std::string poly::gettername ()
{
return name;
}

Is there any reason you are not using std::string? And you can even use
c-style strings to initialize it. I.E.

poly MyInstance( "This Name", 10 );


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

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