需要帮助编写有关引用/指针/实例的语法。 [英] need help on coding grammar about reference / pointer /instance.

查看:54
本文介绍了需要帮助编写有关引用/指针/实例的语法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


在编码方面,我想我需要一些关于如何编写会员

功能的基本帮助。


我已经阅读了常见问题解答,但在编码时我仍然对此感到困惑(参考/

指针/实例),所以我认为我需要一些模板。 />
对不起我在c ++的编码经验


假设我们有
类FooClass {


public:


/ *

foo()//这就是我想在下面问的问题

* /


私人:

string str;

}

我需要什么&模板 ;是:


1.使用外部字符串(字符串*,字符串&)替换类中的字符串内容

string * outp;

string outi;

string& outr;

怎么写这三个foo()?

2. 1外面会得到课堂内的字符串内容,但外面不能

改变它,(返回一个复制/返回const指针)

2. 2外面会得到类内的字符串内容,但外面可以更改

it,(返回一个点/参考)


3.外面可以获得在foo中处理的字符串()

string * foo(){

string * temp;

temp = processof(str);

return temp *

}

怎么样使用autoptr来取消内存泄漏?如果我忘记删除临时*外面?

你的帮助将挽救我的生命:-)


key9


继续尝试......

Hi All

On coding , I think I need some basic help about how to write member
function .

I''ve readed the FAQ, but I am still confuse about it when coding(reference /
pointer /instance) , so I think I need some "template".
Sorry for my coding experience in c++

Suppose we have

class FooClass{

public:

/*
foo() // this is what I want to ask below
*/

private:
string str;
}
What I need "template" is:

1. Use a outside string (string*,string&) to replace string content in class
string* outp;
string outi;
string& outr;
how to write these three foo()?
2. 1 outside will get string content inside of class, but outside can not
change it, (return a copy/return const pointer)
2. 2 outside will get string content inside of class, but outside can change
it, (return a point/reference)

3. outside can get string which have processed in foo()
string* foo(){
string* temp;
temp = processof(str);
return temp*
}
how to use autoptr to void mem leak? if I forgot delete temp* outside?
your help will save my life:-)

key9

keep trying ......

推荐答案

2006-05-27 10:06,key9写道:
On 2006-05-27 10:06, key9 wrote:
大家好

关于编码,我想我需要一些关于如何编写会员
功能的基本帮助。

我已经介绍了常见问题解答,但我是在编码时仍然会混淆它(引用/
指针/实例),所以我想我需要一些模板。
抱歉我的c ++编码经验


你在这里讨论模板时应该小心,因为那里有
是C ++中的模板,但它不是你想要的。

假设我们有

类FooClass {

公开:

/ *
foo()//这就是我想在下面提出的问题
* /

私人:
字符串str;
}

我需要什么&模板是:

1.使用外部字符串(string *,string&)替换类中的字符串内容
string * outp;
string outi;
string& outr;
如何写这三个foo()?


如果我理解你正确的要求是lika

这个:


//使用一个指针

FooClass :: foo(string * s)

{

srt = * s;

}


//使用引用

FooClass :: foo(字符串& s)

{

str = s;

}


//使用副本

FooClass :: foo(string s)

{

str = s;

}


只要有可能,最好使用参考而不是一个

指针,而且经常不使用副本。

2. 1在外面会得到类内的字符串内容,但外面不能改变它,(返回一个复制/返回const指针)
2. 2外面会得到类内的字符串内容,但外面可以改变它,(返回一个点/引用)

> 3.外面可以得到在foo中处理过的字符串()
string * foo(){
string * temp;
t emp = processof(str);
return temp *
}
如何使用autoptr来取消内存泄漏?如果我忘记删除外面的温度*?
Hi All

On coding , I think I need some basic help about how to write member
function .

I''ve readed the FAQ, but I am still confuse about it when coding(reference /
pointer /instance) , so I think I need some "template".
Sorry for my coding experience in c++
You should be careful when talking about templates in here since there
is something called templates in C++, but it''s not what you want.
Suppose we have

class FooClass{

public:

/*
foo() // this is what I want to ask below
*/

private:
string str;
}
What I need "template" is:

1. Use a outside string (string*,string&) to replace string content in class
string* outp;
string outi;
string& outr;
how to write these three foo()?
If I understand you correctly what you are asking for is something lika
this:

// Using a pointer
FooClass::foo(string* s)
{
srt = *s;
}

// Using a reference
FooClass::foo(string& s)
{
str = s;
}

// Using a copy
FooClass::foo(string s)
{
str = s;
}

Whenever possible it is preferable to use a reference instead of a
pointer, and often instead of using a copy too.
2. 1 outside will get string content inside of class, but outside can not
change it, (return a copy/return const pointer)
2. 2 outside will get string content inside of class, but outside can change
it, (return a point/reference)

3. outside can get string which have processed in foo()
string* foo(){
string* temp;
temp = processof(str);
return temp*
}
how to use autoptr to void mem leak? if I forgot delete temp* outside?




假设str已经有一些值,它可以像这样返回:


/ /使用指针

string * FooClass :: foo2()

{

return& str;

}


//使用引用

string& FooClass :: foo2()

{

返回str;

}


//使用一份副本

string FooClass :: foo2()

{

return str;

}


当返回一个指针或引用时,用户可以稍后更改str的vlue

而不调用foo(),因为它可以操作指针/

直接引用,因此返回副本通常是一个好主意(除非你想要这个行为,否则你需要b $ b。)


另请注意你应该永远不会返回一个指针或引用返回它的函数内部声明的对象
对象,因为对象将在函数返回后不存在
但指针/引用将

仍然指向它。


Erik Wikstr?m

-

我一直希望我的电脑和我的电话一样简单易用;我的愿望实现了,因为我再也无法想象如何使用我的电话了。 - Bjarne Stroustrup



Supposing str already has some value it can be returned like this:

// Using a pointer
string* FooClass::foo2()
{
return &str;
}

// Using a reference
string& FooClass::foo2()
{
return str;
}

// Using a copy
string FooClass::foo2()
{
return str;
}

When returning a pointer or reference the user can later change the vlue
of str without calling foo() since it can manipulate the pointer/
reference directly, thus it''s often a good idea to return a copy (unless
you want this behaviour).

Notice also that you should never return a pointer or reference to a
object declared inside the function returning it since the object will
no exist after the function has returned but the pointer/reference will
still point to it.

Erik Wikstr?m
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup


Erik Wikstr?m< Er *********** @ telia.com>写道:
Erik Wikstr?m <Er***********@telia.com> wrote:
如果我理解你的正确要求是什么lika
这个:

//使用指针
FooClass :: foo(string * s)
{
srt = * s;
}
//使用引用
FooClass :: foo( string& s)




//使用副本
FooClass :: foo(string s)
{
str = s;
}
只要有可能,最好使用引用而不是
指针,而且通常也不使用副本。
If I understand you correctly what you are asking for is something lika
this:

// Using a pointer
FooClass::foo(string* s)
{
srt = *s;
}

// Using a reference
FooClass::foo(string& s)
{
str = s;
}

// Using a copy
FooClass::foo(string s)
{
str = s;
} Whenever possible it is preferable to use a reference instead of a
pointer, and often instead of using a copy too.




好​​的,这是C ++风格的一点我不明白。 (其中一个很多,我希望。)为什么一个人会使用一个参数参数到一个

函数,除非有人想让函数修改指示对象?

我经常在人们的代码中看到这个。复制论证

效率低吗? (似乎不太可能。)


谢谢,


史蒂夫



Okay, this is a point of C++ style I don''t understand. (One of
many, I expect.) Why would one use a reference argument to a
function, unless one wanted the function to modify the referent?
I frequently see this in people''s code. Is a copy argument
less efficient? (Seems unlikely to me.)

thanks,

Steve


史蒂夫Pope写道:
好的,这是C ++风格的一点,我不明白。 (其中一个,我希望。)为什么一个人会使用一个参考参数到一个
函数,除非有人希望函数修改指示物?
我经常在人们看到这个''代码。复制论点效率低吗? (似乎不太可能。)
Okay, this is a point of C++ style I don''t understand. (One of
many, I expect.) Why would one use a reference argument to a
function, unless one wanted the function to modify the referent?
I frequently see this in people''s code. Is a copy argument
less efficient? (Seems unlikely to me.)




规则是:当你不需要时,不要复制。大多数时候,

你想通过const引用传递对象。

Jonathan



The rule is: don''t copy when you don''t have to. Most of the times,
you''ll want to pass objects by const reference.
Jonathan


这篇关于需要帮助编写有关引用/指针/实例的语法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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