简体   繁体   English

指向C ++中对象的指针

[英]Pointer to object in C++

I saw the below code in Stackoverflow.com . 我在Stackoverflow.com看到了以下代码。 I was looking for the same code, bacuse I have a doubt. 我一直在寻找相同的代码,因为我对此表示怀疑。 If we will see the below code 如果我们看到以下代码
CRectangle r1, *r2; r2= new CRectangle;
1. Why are we doing new CRectangle ? 1.我们为什么要做new CRectangle What actually we are trying to do? 实际上我们正在尝试做什么?

  1. One of colleague told me, when we write the code we make, 一位同事告诉我,当我们编写自己编写的代码时,
    CRectangle *r2 = 0; ,

Then we initialize with some other value or address. 然后,我们使用其他值或地址进行initialize I really got confused. 我真的很困惑。 Please help me. 请帮我。

using namespace std; 
class CRectangle
{
    int width, height;  
public:    
    void set_values (int, int);  
    int area (void) {return (width * height);
    } 
}; 
void CRectangle::set_values (int a, int b) 
{    
    width = a;   
    height = b;
} 
int main () 
{   
    CRectangle r1, *r2;    
    r2= new CRectangle;    
    r1.set_values (1,2);  
    r2->set_values (3,4);   
    cout << "r1.area(): " << r1.area() << endl;    
    cout << "r2->area(): " << r2->area() << endl;  
    cout << "(*r2).area(): " << (*r2).area() << endl;  
    delete r2;    
    return 0; 
} 

CRectangle r1, *r2; C矩形r1,* r2; r2= new CRectangle; r2 =新的CRectangle;

r1 is the object. r1是对象。 r2 is the pointer to the object. r2是指向对象的指针。

CRectangle *r2 = 0;

Pointer contains the address of the object. 指针包含对象的地址。 If the pointer has not initialized, it can have any value (that we don't know). 如果指针尚未初始化,则它可以具有任何值(我们不知道)。 So, we initialize 0 so that we know our pointer has value 0 . 因此,我们初始化0以便我们知道指针的值为0 We can use that value to avoid double initializing or some memory error. 我们可以使用该值来避免两次初始化或某些内存错误。

When we are about to assign the object to the pointer, what we usually do is_ 当我们将对象分配给指针时,通常要做的是

if (r2 == 0)
    r2 = new CRectangle;
else
    std::cout << "Pointer is not empty or invalid" << std::endl;

In your example r1 has type of CRectangle class, but r2 has type of pointer to CRectangle class. 在您的示例中, r1具有CRectangle类的类型,但是r2具有指向CRectangle类的指针的类型。 The pointer is just address in global memory ( heap ). 指针只是全局内存( )中的地址。 In order to allocate and mark memory as CRectangle object we need use new . 为了将内存分配和标记为CRectangle对象,我们需要使用new

Please, read the details in books 请阅读书中的详细信息

CRectangle r1, *r2;    
r2= new CRectangle;

This code declares a CRectangle object ( r1 ), and a pointer to a CRectangle object, which doesn't yet point to anything ( r2 ). 此代码声明一个CRectangle对象( r1 ),以及一个指向CRectangle对象的指针,该指针尚未指向任何内容( r2 )。 Both variables are allocated on the stack. 这两个变量都分配在堆栈上。

The second line allocates memory for a new CRectangle object (from the heap), calls any constructor for that class, and then returns the address and assigns it to r2 . 第二行(从堆中)为新的CRectangle对象分配内存,调用该类的任何构造函数,然后返回地址并将其分配给r2 So r2 then points to a CRectangle object. 因此, r2然后指向一个CRectangle对象。

Setting r2 to 0 or NULL is simply a standard way to indicate that the pointer doesn't actually point to anything. r2设置为0或NULL只是指示指针实际上没有指向任何内容的标准方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM