参考或指针? [英] References or pointers?

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

问题描述

我做了这个例子:


#include< iostream>


class Beer {

public :

啤酒(){

std :: cout<< 制作啤酒\ n;

num = 1;

}


Beer(int n):num (n){}


int getBeer(){

返回数字;

}


void remove(){

num--;

}


private:

int num;

};


class Brew {

public:

/ *简单* /

无效饮料啤酒(啤酒b){

std :: cout<< 喝酒 << b.getBeer()<< "啤酒的实例!\ n" ;;

b.remove();

}


void checkBeer(啤酒b) {

std :: cout<< b.getBeer()<< "剩下的啤酒实例!\ n" ;;

}


/ *有参考资料。请注意,'&''放在类似

*指针运算符之类的类型之后。 * /

void drinkBeerRef(Beer& b){

std :: cout<< 喝酒 << b.getBeer()<< "参考啤酒!\ n" ;;

b.remove();

}


void checkBeerRef(Beer& b) {

std :: cout<< b.getBeer()<< "参考啤酒剩下!\ n" ;;

}


/ *带指针* /

无效饮料啤酒(啤酒* b ){

std :: cout<< 喝酒 << b-> getBeer()<< "指针啤酒!\ n" ;;

b->删除();

}


void checkBeer(啤酒* b){

std :: cout<< b-> getBeer()<< "指针啤酒离开!\ n" ;;

}

};

int main(){


/ *(0)调用默认构造函数。 * /

啤酒b1;


/ *(1)制作啤酒厂,供您品尝! checkBeer不会打印

*正确的值。 * /

啤酒b2(10); //制作10瓶啤酒。

Brew brew;

brew.drinkBeer(b2);

brew.checkBeer(b2);


std :: cout<< std :: endl;

/ *(2)使用参考* /

啤酒b3(10); //制作10瓶啤酒。

brew.drinkBeerRef(b3);

brew.checkBeerRef(b3);


std: :cout<< std :: endl;


/ *(3)使用指针* /


/ *单个对象包含10个啤酒。 * /

啤酒* b4 =新啤酒(10);


/ *或:

*

*啤酒* b4 =新啤酒[10];

*

*使用默认构造函数。 10个包含1个啤酒的物品。

*

* * /


brew.drinkBeer(b4);

brew.checkBeer(b4);


返回0;

}

如何将参数作为参考传递不同于传递它作为

指针(除了也给出了正确的结果)?


当你使用''new''数据被分配时堆中和更改时

a函数将在函数返回后保持更改。但这也是

也是引用的情况......'啤酒b3''在堆上分配''b3''?

解决方案



" desktop" < ff*@sss.comha scritto nel messaggio

news:fb ********** @ news.net.uni-c.dk ...
< blockquote class =post_quotes>
>我做了这个例子:



....


如何将参数作为参考传递不同于将其作为

指针传递(除了也给出正确的结果)?



您可以将引用视为const指针的''syntax sugar''(请注意,

不是指向consts的指针)。 />
通过引用传递参数是一种更安全,更方便的方法来传递

指向值的指针。

被调用者可以修改值但不能指针,即不能将

''指针''切换到别的东西。


当你使用''new'时'数据在堆上分配,并且在更改后,在函数返回后,
函数将保持更改状态。但这也是

引用的情况......'啤酒b3''在堆上分配''b3''?



b3在堆栈中,但相关的一点是标记为/ *

的部分简单* /它是不正确的。

你通过''价值''啤酒,只有在checkBeer

电话才有意义。


再见Carlo


9月5日上午9:54,桌面< f ... @ sss.comwrote:


我做了这个例子:


#include< iostream>


class Beer {

public :

啤酒(){

std :: cout<< 制作啤酒\ n;

num = 1;

}


Beer(int n):num (n){}


int getBeer(){

返回数字;

}


void remove(){

num--;

}


private:

int num;


};


class Brew {

public:

/ *简单* /

无效饮品啤酒(啤酒b){

std :: cout<< 喝酒 << b.getBeer()<< "啤酒的实例!\ n" ;;

b.remove();

}


void checkBeer(啤酒b) {

std :: cout<< b.getBeer()<< "剩下的啤酒实例!\ n" ;;

}


/ *有参考资料。请注意,'&''放在类似

*指针运算符之类的类型之后。 * /

void drinkBeerRef(Beer& b){

std :: cout<< 喝酒 << b.getBeer()<< "参考啤酒!\ n" ;;

b.remove();

}


void checkBeerRef(Beer& b) {

std :: cout<< b.getBeer()<< "参考啤酒剩下!\ n" ;;

}


/ *带指针* /

无效饮料啤酒(啤酒* b ){

std :: cout<< 喝酒 << b-> getBeer()<< "指针啤酒!\ n" ;;

b->删除();

}


void checkBeer(啤酒* b){

std :: cout<< b-> getBeer()<< "指针啤酒离开!\ n" ;;

}


};


int main(){


/ *(0)调用默认构造函数。 * /

啤酒b1;


/ *(1)制作啤酒厂,供您品尝! checkBeer不会打印

*正确的值。 * /

啤酒b2(10); //制作10瓶啤酒。

Brew brew;

brew.drinkBeer(b2);

brew.checkBeer(b2);



** NO NO NO **

后两行是语法错误。你必须使用地址 -

运算符(&)提取对象的地址并将其传递给

指针:


brew.drinkBeer(& b2 );

brew.checkBeer(& b2);


>

std :: cout< ;< std :: endl;


/ *(2)使用参考* /

啤酒b3(10); //制作10瓶啤酒。

brew.drinkBeerRef(b3);

brew.checkBeerRef(b3);


std: :cout<< std :: endl;


/ *(3)使用指针* /


/ *单个对象包含10个啤酒。 * /

啤酒* b4 =新啤酒(10);


/ *或:

*

*啤酒* b4 =新啤酒[10];

*

*使用默认构造函数。 10个包含1个啤酒的物品。

*

* * /


brew.drinkBeer(b4);

brew.checkBeer(b4);



您可以通过derefrence运算符取消引用指针(一元*)。尝试

这个也是:

brew.drinkBeerRef(* b4);

brew.checkBeerRef(* b4);


要注意:你需要摆脱动态在结束程序之前对象(通过new / new []

运算符创建):


删除b4;

/ *或:

*

*删除[] b4;啤酒* b4 =新啤酒[10];

*

*如果啤酒* b4 =新啤酒[10];

*

* /

返回0;


>

}


如何将参数作为引用传递与传递不同它是一个

指针(除了也给出正确的结果)?


当你使用''new''时,数据在堆上分配当更改为

a函数将在函数返回后保持更改。但这是

也是引用的情况......'啤酒b3''在堆上分配''b3''?



否,''b3''在堆栈上分配并在退出封闭代码之前解除分配(首先破坏

然后解除分配)块(主要

函数在这种情况下)。


提示:

动态对象(用new创建)放在堆积和生存

除非你杀了它们(用删除销毁)。

无 - 代码块中声明的静态对象在
$ b $时被销毁b封闭块出口。

实例数据成员被所有者对象销毁。

静态对象在程序结束时被销毁。

指针可以是整数类型的加/减(++ / - )。这是使用内部数组时有用的



int a [10 ];

int * ptr = a; // ptr =& a [0];

cout<< * ++ ptr; // ptr =& a [1]; cout<< * ptr;


问候,

FM。


" Carlo Capelli" < ca *********** @ rdbos.itwrote in message

news:Zb ***************** @ tornado .fastwebnet.it ...

[...]


您可以将引用视为const的''syntax sugar''指针



确实。


I have made this example:

#include<iostream>

class Beer {
public:
Beer() {
std::cout << "made a beer\n";
num = 1;
}

Beer(int n) : num(n) {}

int getBeer(){
return num;
}

void remove(){
num--;
}

private:
int num;
};

class Brew {
public:
/* Simple*/
void drinkBeer(Beer b) {
std::cout << "Drank " << b.getBeer() << " instances of beers!\n";
b.remove();
}

void checkBeer(Beer b) {
std::cout << b.getBeer() << " instances of beers left!\n";
}


/* With references. Notice that the ''&'' is placed AFTER the type like the
* pointer operator. */
void drinkBeerRef(Beer& b) {
std::cout << "Drank " << b.getBeer() << " reference beers!\n";
b.remove();
}

void checkBeerRef(Beer& b) {
std::cout << b.getBeer() << " reference beers left!\n";
}

/* With pointers */
void drinkBeer(Beer* b) {
std::cout << "Drank " << b->getBeer() << " pointer beers!\n";
b->remove();
}

void checkBeer(Beer* b) {
std::cout << b->getBeer() << " pointer beers left!\n";
}
};
int main(){

/* (0) Calling default constructor. */
Beer b1;

/* (1) Make a brewery where you can drink! checkBeer will not print
* the right value. */
Beer b2(10); // make 10 beers.
Brew brew;
brew.drinkBeer(b2);
brew.checkBeer(b2);

std::cout << std::endl;
/* (2) Using references */
Beer b3(10); // make 10 beers.
brew.drinkBeerRef(b3);
brew.checkBeerRef(b3);

std::cout << std::endl;

/* (3) Using pointers */

/* Single object containing 10 beers. */
Beer* b4 = new Beer(10);

/* or:
*
* Beer* b4 = new Beer[10];
*
* using default constructor. 10 objects containing 1 beer.
*
* */

brew.drinkBeer(b4);
brew.checkBeer(b4);

return 0;
}
How does passing an argument as a reference differ from passing it as a
pointer (besides from also giving the right result)?

When you use ''new'' the data is allocated on the heap and when changed in
a function will remain changed after the functions returns. But this is
also the case with references...does ''Beer b3'' allocate ''b3'' on the heap?

解决方案


"desktop" <ff*@sss.comha scritto nel messaggio
news:fb**********@news.net.uni-c.dk...

>I have made this example:

....

How does passing an argument as a reference differ from passing it as a
pointer (besides from also giving the right result)?

You can think to references as ''syntax sugar'' for const pointers (beware,
not pointer to consts).
Passing an argument by reference is a safer and more convenient way to pass
the pointer to the value.
The callee can modify the value but not the pointer, i.e. can''t switch the
''pointer'' to something else.

When you use ''new'' the data is allocated on the heap and when changed in a
function will remain changed after the functions returns. But this is also
the case with references...does ''Beer b3'' allocate ''b3'' on the heap?

b3 is on the stack, but the pertinent point is that the section marked /*
Simple*/ it''s incorrect.
You pass ''by value'' the beer, and that make sense only in the checkBeer
call.

Bye Carlo


On Sep 5, 9:54 am, desktop <f...@sss.comwrote:

I have made this example:

#include<iostream>

class Beer {
public:
Beer() {
std::cout << "made a beer\n";
num = 1;
}

Beer(int n) : num(n) {}

int getBeer(){
return num;
}

void remove(){
num--;
}

private:
int num;

};

class Brew {
public:
/* Simple*/
void drinkBeer(Beer b) {
std::cout << "Drank " << b.getBeer() << " instances of beers!\n";
b.remove();
}

void checkBeer(Beer b) {
std::cout << b.getBeer() << " instances of beers left!\n";
}

/* With references. Notice that the ''&'' is placed AFTER the type like the
* pointer operator. */
void drinkBeerRef(Beer& b) {
std::cout << "Drank " << b.getBeer() << " reference beers!\n";
b.remove();
}

void checkBeerRef(Beer& b) {
std::cout << b.getBeer() << " reference beers left!\n";
}

/* With pointers */
void drinkBeer(Beer* b) {
std::cout << "Drank " << b->getBeer() << " pointer beers!\n";
b->remove();
}

void checkBeer(Beer* b) {
std::cout << b->getBeer() << " pointer beers left!\n";
}

};

int main(){

/* (0) Calling default constructor. */
Beer b1;

/* (1) Make a brewery where you can drink! checkBeer will not print
* the right value. */
Beer b2(10); // make 10 beers.
Brew brew;
brew.drinkBeer(b2);
brew.checkBeer(b2);

**NO NO NO**
the 2 later lines are syntax errors.You must use the address-of
operator (&) to extract the address of an object and pass it to a
pointer:

brew.drinkBeer(&b2);
brew.checkBeer(&b2);

>
std::cout << std::endl;

/* (2) Using references */
Beer b3(10); // make 10 beers.
brew.drinkBeerRef(b3);
brew.checkBeerRef(b3);

std::cout << std::endl;

/* (3) Using pointers */

/* Single object containing 10 beers. */
Beer* b4 = new Beer(10);

/* or:
*
* Beer* b4 = new Beer[10];
*
* using default constructor. 10 objects containing 1 beer.
*
* */

brew.drinkBeer(b4);
brew.checkBeer(b4);

you can dereference a pointer via the derefrence operator(unary *).try
this one too:

brew.drinkBeerRef( * b4 );
brew.checkBeerRef( * b4 );

beware: you need to get rid of dynamic objects(created via new/new[]
operators) before ending the program :

delete b4;
/* or:
*
* delete[] b4;Beer* b4 = new Beer[10];
*
* if Beer* b4 = new Beer[10];
*
*/
return 0;

>
}

How does passing an argument as a reference differ from passing it as a
pointer (besides from also giving the right result)?

When you use ''new'' the data is allocated on the heap and when changed in
a function will remain changed after the functions returns. But this is
also the case with references...does ''Beer b3'' allocate ''b3'' on the heap?

No,''b3'' is allocated on the stack and deallocated(first destructed
then deallocated) prior to exiting the enclosing code block(main
function in this context).

Tip:
Dynamic objects (created with new ) are placed on the heap and live
there unless you do kill them (destroy with delete).
None - static objects declared in code blocks are destroyed when the
enclosing block exits.
Instance data members are destroyed by the owner object.
static objects are destroyed at the end of the program.
pointers can be inc/decremented(++/--) like integral types.this is
usefull when working with intrinsic arrays:

int a[10];
int * ptr =a;//ptr = & a[0];
cout <<*++ptr;//ptr= & a[1];cout<<*ptr;

regards,
FM.


"Carlo Capelli" <ca***********@rdbos.itwrote in message
news:Zb*****************@tornado.fastwebnet.it...
[...]

You can think to references as ''syntax sugar'' for const pointers

Indeed.


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

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