Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图学习指针,为什么要使用它们而不是仅仅使用&;? 我试图用C++来处理指针,用指针和使用和变量来获取对象的位置有什么意义?_C++_C++11_Pointers - Fatal编程技术网

试图学习指针,为什么要使用它们而不是仅仅使用&;? 我试图用C++来处理指针,用指针和使用和变量来获取对象的位置有什么意义?

试图学习指针,为什么要使用它们而不是仅仅使用&;? 我试图用C++来处理指针,用指针和使用和变量来获取对象的位置有什么意义?,c++,c++11,pointers,C++,C++11,Pointers,例如(我没有实际运行此代码,只是举个例子): int评分{10}; int*分数{nullptr}; 分数_ptr=&score; cout&score是“指向int的指针”类型的PR值。prvalue的意思是“纯粹的一个可以放在赋值语句右侧的值” int*score\u ptr是“指向int的指针”类型的左值。这意味着它可以放在赋值语句的左侧 区别在于,一个是没有标识的值,另一个是具有标识的变量 您不能执行和&score,但可以执行和score\u ptr。您可以分配给分数\u ptr,但不能

例如(我没有实际运行此代码,只是举个例子):

int评分{10};
int*分数{nullptr};
分数_ptr=&score;

cout
&score
是“指向int的指针”类型的PR值。prvalue的意思是“纯粹的一个可以放在赋值语句右侧的值”

int*score\u ptr
是“指向int的指针”类型的左值。这意味着它可以放在赋值语句的左侧

区别在于,一个是没有标识的值,另一个是具有标识的变量

您不能执行
和&score
,但可以执行
和score\u ptr
。您可以分配给
分数\u ptr
,但不能分配给
&分数

指针类似于一张写有街道地址的纸。值是街道地址

指针变量是一个街道地址,其中有一张纸说明另一个街道地址在哪里

假设你每4个月搬家一次;也许你是一所大学的合作学生。给某人你的当前地址是浪费时间,因为几个月后它就会变成垃圾。给他们你父母的地址,并让他们把你的邮件转发给你,这样做更有意义


在这里,您居住在
int
中。如果你告诉某人你父母的地址——一个指向
int*
类型变量的地址——即使你四处走动,他们也可以给你发送信息。

有很多人希望在C语言中使用指针的原因++

我认为最基本的是结合<代码>新< /代码>。例如,这是C++中未定义的行为:

SomeClass* someFunc() {
    SomeClass out;
    return &out;
}
它是未定义的行为,因为
out
someFunc
中只有一个有效的内存位置,因此任何调用该函数的人都将处理一个错误的
SomeClass
实例。正确的处理方法是:

SomeClass* someFunc() {
    SomeClass* out = new SomeClass();
    return out;
}
另一个原因可能与
nullptr
有关。例如,假设你有一群人在一个程序中硬编码,你正在实现一个电话目录。运行该程序的用户输入一个电话号码,如果该电话号码不属于任何人,该程序要么给出指向此人的指针,要么给出
nullptr
。仅使用
&


另一个原因是管理从公共类继承的扩展类。指针也可以帮助您避免切片问题。 例如,如果有许多不同的类派生自同一父类,并且希望将它们放入数据类型父类列表中

#include <vector>
class Parent{
protected:
  //variables common to all the children of the parent class
public:
  //methods common to all the children classes
}
class Child1: public Parent{
private:
  //data unique to this class
public:
  //methods unique to this class
}
class Child2: public Parent{
private:
  //data unique to this class
public:
  //methods unique to this class
}
std::vector<Parent*> listOfInstances;
Child1 instance1;
Child2 instance2;
listOfInstances.push_back(instance1);
listOfInstances.push_back(instance2);
#包括
班级家长{
受保护的:
//父类的所有子类通用的变量
公众:
//所有儿童班通用的方法
}
类别Child1:公共父级{
私人:
//此类的唯一数据
公众:
//此类特有的方法
}
第2类:公共家长{
私人:
//此类的唯一数据
公众:
//此类特有的方法
}
std::向量列表;
Child1实例1;
Child2实例2;
实例列表。推回(实例1);
实例列表。推回(实例2);

如果名为
listOfInstances
的向量不是指针,那么
Child1
Child2
所特有的所有数据都将被切断,并且这两个实例都将成为类
父类的实例,这与任何其他类型的变量都是一样的。例如,为什么要使用
int x=42
,而不是每次只写
42
?在堆中分配内存时需要指针,例如使用
new
运算符。指针可以为NULL/nullptr,这使得示例的所有可能不同的副本都是演示指针是什么的玩具练习,而不是在实际代码中如何使用指针。也许你只需要耐心等待,直到你看到更复杂的例子。
#include <vector>
class Parent{
protected:
  //variables common to all the children of the parent class
public:
  //methods common to all the children classes
}
class Child1: public Parent{
private:
  //data unique to this class
public:
  //methods unique to this class
}
class Child2: public Parent{
private:
  //data unique to this class
public:
  //methods unique to this class
}
std::vector<Parent*> listOfInstances;
Child1 instance1;
Child2 instance2;
listOfInstances.push_back(instance1);
listOfInstances.push_back(instance2);