C ++和指针 [英] C++ and pointer

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

问题描述

我可以完全避免在C ++代码中使用指针,这样代码就可以减少错误并且很容易调试吗?


谢谢。 />

John

解决方案

" John" <乔********* @ yahoo.com>在消息中写道

news:c3 ************************* @ posting.google.co m

我可以完全避免在C ++代码中使用指针,这样代码就可以减少错误并且易于调试吗?

谢谢。

John



完全避免指针不一定会产生更少的b $ b错误或易于调试的代码。


通过使用标准库(向量,列表等),您可以(并且应该)

大幅减少指针的使用。但是,需要指点

至少以下四种情况


1.如果你想要多态行为(你也可以使用引用,但是

引用需要在声明和标准容器时初始化

无法存储引用---无论如何,引用都无法解决内存

leak与指针相关的问题),

2.当两个类/结构相互引用并且你有一个问题时,每个类/结构需要另一个/结构声明它之前可以声明
本身,

3.当因为效率,调试或

等原因而重载operator new时,

4.与许多C库接口时。


您可以完全避免使用指针,但代价是复杂化

你的程序,产生更多错误并使你的程序不那么容易

调试。


使用智能指针& QUOT;可以避免与使用原始指针相关的大多数问题(特别是内存泄漏)。

-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发布)


John写道:

我可以完全避免在C ++代码中使用指针,这样代码可以减少错误并且易于调试吗?


如果你想。


或者你可以先编码测试,没有任何错误或者必须调试!


Ian


John发布:

我可以完全避免在C ++代码中使用指针,以便代码具有
更少的错误,并且易于调试?

谢谢。

约翰



你也可以花一辈子去生活坐在轮椅上,你不会贬值。由你决定。

我自己*喜欢*指针。一旦你掌握了b $ b,它们就是一个非常简单的概念。我假设你是人类,因此你不仅仅是聪明到足以解决这个问题:


int main(void)< br $>
{

int chocolate = 5;


int icecream = 7;

int * pDesert;

pDesert =&巧克力;


* pDesert = 2;

pDesert =& icecream;


* pDesert = 3;

//现在:

//巧克力== 2

//冰淇淋= = 3

}

-JKop


Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John

解决方案

"John" <jo*********@yahoo.com> wrote in message
news:c3*************************@posting.google.co m

Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John


It is not true that avoiding pointers entirely will necessarily produce less
bugs or code that is easy to debug.

By using the standard library (vectors, lists etc.), you can (and should)
drastically reduce the use of pointers. However, pointers are needed in at
least the following four circumstances

1. if you want polymorphic behaviour (you can also use references, but
references need to be initialised when declared and standard containers
cannot store references --- in any case, references don''t solve the memory
leak problem associated with pointers),
2. when two classes/structs refer to each other and you have a catch 22
problem where each class/struct needs the other to be declared before it can
itself be declared,
3. when overloading operator new for reasons of efficiency, debugging or
whatever,
4. when interfacing with many C libraries.

You can avoid the use of pointers entirely, but at the cost of complicating
your program, producing more bugs and making your program less easy to
debug.

Use of "smart pointers" can avoid most of the problems (in particular,
memory leaks) associated with the use of raw pointers.
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


John wrote:

Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?


If you want.

Or you could code test first and not have any bugs or have to debug!

Ian


John posted:

Can I completely avoid using pointer in C++ code, so that the code has
less bugs and is easy to debug?

Thanks.

John


You could also spend your life going around in a wheelchair, you won''t fall
down as much. Up to you.
I myself *like* pointers. They''re a very very simple concept once you''ve
grasped it. I assuming you''re human, and therefore you''re more than
intelligent enough to figure this stuff out:

int main(void)
{
int chocolate = 5;

int icecream = 7;
int* pDesert;
pDesert = &chocolate;

*pDesert = 2;
pDesert = &icecream;

*pDesert = 3;
//Now:
//chocolate == 2
//icecream == 3
}
-JKop


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

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