Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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++_Class_Reference_This - Fatal编程技术网

C++ C++;通过引用传递和此对象

C++ C++;通过引用传递和此对象,c++,class,reference,this,C++,Class,Reference,This,为什么答案是“OK” class-CTest{ 公众: int isitme(CTest&cobj); }; int CTest::isitme(CTest和cobj) { 如果(&cobj==this) { 返回true; } 其他的 { 返回false; } } int main() { CTest a; CTest*b=&a; 如果(b->isitme(a)) { cout因为成员函数隐式地接收一个指向其所调用对象的指针作为参数。此指针在函数体中作为此指针可用 所以当你这样做的时候: b-

为什么答案是“OK”

class-CTest{
公众:
int isitme(CTest&cobj);
};
int CTest::isitme(CTest和cobj)
{
如果(&cobj==this)
{
返回true;
}
其他的
{
返回false;
}
}
int main()
{
CTest a;
CTest*b=&a;
如果(b->isitme(a))
{

cout因为成员函数隐式地接收一个指向其所调用对象的指针作为参数。此指针在函数体中作为
指针可用

所以当你这样做的时候:

b->isitme(a)
成员函数
isitme()
隐式接收指针
b
作为参数,该指针将被视为函数内的
指针

由于
b
指向
a
将指向
a
(毕竟,您正在通过指针
b
调用对象
a
上的成员函数
isitme()

由于
a
是作为显式参数传递的,因此
a
也是引用
cobj
绑定的对象。因此,获取
cobj
的地址将为您提供
a
的地址。这反过来意味着表达式:

//  Address of "a": "cobj" is bound to argument "a" in the function call
//  vvvvv
    &cobj == this
//           ^^^^ 
//           Address of "a": the function is called through a pointer to "a"

计算结果为
true

,因为成员函数隐式地接收指向其所调用对象的指针作为参数。此指针在函数体中作为
指针可用

int CTest::isitme(CTest &cobj) 
{ 
    // cobj is a reference, but still an object
    CTest * pCobj = &cobj; //pCobj is now the pointer to the actual memory
    //so it's the same as this pointer. Because you did before CTest *b = &a;        
}
所以当你这样做的时候:

b->isitme(a)
成员函数
isitme()
隐式接收指针
b
作为参数,该指针将被视为函数内的
指针

由于
b
指向
a
将指向
a
(毕竟,您正在通过指针
b
调用对象
a
上的成员函数
isitme()

由于
a
是作为显式参数传递的,因此
a
也是引用
cobj
绑定的对象。因此,获取
cobj
的地址将为您提供
a
的地址。这反过来意味着表达式:

//  Address of "a": "cobj" is bound to argument "a" in the function call
//  vvvvv
    &cobj == this
//           ^^^^ 
//           Address of "a": the function is called through a pointer to "a"

计算结果为
true

b指向a,则在中使用a

int CTest::isitme(CTest &cobj) 
{ 
    // cobj is a reference, but still an object
    CTest * pCobj = &cobj; //pCobj is now the pointer to the actual memory
    //so it's the same as this pointer. Because you did before CTest *b = &a;        
}
b->isitme(a)

isitme()只是将作为参数传递的对象与
this
进行检查。在本例中,它是这样的,所以OK

b指向a,然后在中使用a

b->isitme(a)

isitme()只是将作为参数传递的对象与
this
进行检查。在这种情况下,是这样的,所以OK

简单的回答是
b
是指向
a
的指针——它们都指向同一个实际对象,因此当您比较这两个“对象”的地址时,它们的比较结果是相等的

还有一些事情确实需要指出,虽然更多的是评论而不是回答,但它不适合评论,所以

int CTest::isitme(CTest &cobj)
{
    if(&cobj == this)
    {
        return true;
    }
    else
    {
        return false;
    }
}
老实说,这是一个相当糟糕的代码。因为它确实返回了一个
bool
,所以您应该声明它返回bool。直接返回比较结果也更好。您还应该阅读一些关于“const correction”的内容.考虑到这些因素,您的函数最终看起来更像:

bool CTest::isitme(CTest const &cobj) const {
    return this == &cobj;
}

简而言之,
b
是指向
a
的指针——它们都指向同一个实际对象,因此当您比较这两个对象的地址时,它们的比较结果是相等的

还有一些事情确实需要指出,虽然更多的是评论而不是回答,但它不适合评论,所以

int CTest::isitme(CTest &cobj)
{
    if(&cobj == this)
    {
        return true;
    }
    else
    {
        return false;
    }
}
老实说,这是一个相当糟糕的代码。因为它确实返回了一个
bool
,所以您应该声明它返回bool。直接返回比较结果也更好。您还应该阅读一些关于“const correction”的内容.考虑到这些因素,您的函数最终看起来更像:

bool CTest::isitme(CTest const &cobj) const {
    return this == &cobj;
}

这里发生了几件事

首先,只分配了一份
CTest
。要检查这一点,可以在构造函数中打印:

class CTest {
public:
  CTest() {
    cout << "Constructor called";
  }    
...
};
然后,对象将按值传递。因此,将生成一个
cobj
的副本。实际上,调用另一个构造函数(编译器提供的默认副本构造函数)来生成一个新对象,该对象是给定对象的副本

要证明这一点,请重写默认的复制构造函数以显示消息

class CTest {
public:
  // Default constructor
  CTest() {
    cout << "Constructor called" << endl;
  }
  // Copy constructor
  CTest(CTest& source) {
    cout << "Copy constructor called" << endl;
  }
  // Comparison
  bool isitme(CTest obj) {
    return (this == &obj);
  }
};

// Main program
int main (int argc, char* argv[]) {
  ...
  CTest a;
  CTest* b = &a;
  cout << (b->isitme(a)) << endl;
  ...
}
class-CTest{
公众:
//默认构造函数
CTest(){

这里发生了几件事

首先,只分配了一份
CTest
。要检查这一点,可以在构造函数中打印:

class CTest {
public:
  CTest() {
    cout << "Constructor called";
  }    
...
};
然后,对象将按值传递。因此,将生成一个
cobj
的副本。实际上,调用另一个构造函数(编译器提供的默认副本构造函数)来生成一个新对象,该对象是给定对象的副本

要证明这一点,请重写默认的复制构造函数以显示消息

class CTest {
public:
  // Default constructor
  CTest() {
    cout << "Constructor called" << endl;
  }
  // Copy constructor
  CTest(CTest& source) {
    cout << "Copy constructor called" << endl;
  }
  // Comparison
  bool isitme(CTest obj) {
    return (this == &obj);
  }
};

// Main program
int main (int argc, char* argv[]) {
  ...
  CTest a;
  CTest* b = &a;
  cout << (b->isitme(a)) << endl;
  ...
}
class-CTest{
公众:
//默认构造函数
CTest(){

这难道不是针对初始化为原始对象的指针进行测试时所期望的吗?这是针对初始化为原始对象的指针进行测试时所期望的吗?