为什么引用在通过指针完成时中断 [英] Why does referencing break when done through pointers

查看:200
本文介绍了为什么引用在通过指针完成时中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类测试的引用默认构造函数。

  class test {
public:
test(int& input1):int_test(input1){};
〜test(){};

int& int_test;
};

然后另外两个与测试交互的类如下:

  class notebook 
{
public:
notebook(){};
〜notebook(){};

int int_notebook;
};

class factory
{
public:
factory(){};
〜factory(){};

笔记本* p_notebook;
};

如果i intitalise test(t2)用一个整数,这个工作原理如下:

  int _tmain(int argc,_TCHAR * argv []){

int var = 90;
test t2(var);
cout<<< t2.int_test; // this gives 90
var = 30;
cout<<< t2.int_test; // this gives 30

一旦我初始化测试类和指向类Notebook的成员的指针,第三类工厂:

  factory f1; 
notebook s1;
notebook s2;
s1.int_notebook = 10;
s2.int_notebook = 2;

int notebook :: * p_notebook =& notebook :: int_notebook;
f1.p_notebook =& s1;

test t1(((f1.p_notebook-> * p_notebook)));
cout<<< t1.int_test; //这给出10

然而,如果我将f1.p_notebook的指针更改为笔记本s2的另一个对象;

  f1.p_notebook =& s2; 
cout<<< t1.int_test; //这给出了10的

t1的引用成员(t1.int_test)不反映更改的指针。可以有人向我解释为什么? c> test c>

有一个int的引用。它不知道什么对象int实际上属于什么。或者它最初如何访问该int。让我们分解这行代码:

  test t1(((f1.p_notebook-> * p_notebook))) 

首先:

  f1.p_notebook 

在该行中,它是指向s1的指针。现在:

  f1.p_notebook-> * p_notebook 

这是s1的 int_notebook 成员。所以用这个:

  test t1(((f1.p_notebook-> * p_notebook) 

您正在传递s1的 int_notebook 构造函数。所以现在对象 t1 引用了s1的 int_notebook 成员。它不关心你用来获得该成员的间接层次模糊。它[t1]什么也不知道f1,或f1.p_notebook。所以当你这样做:

  f1.p_notebook =& s2; 

这对s1.int_notebook完全没有影响,因此它对t1的引用成员没有影响。


i have a referenced default constructor for a class test.

class test {
public:
    test(int &input1) : int_test(input1) {};
    ~test() {};

    int & int_test; 
}; 

Then 2 more classes which interact with test as follows:

class notebook
{ 
public:
    notebook() {};
    ~notebook() {};

    int int_notebook;
};

class factory
{
public: 
    factory() {};
    ~factory(){};

    notebook *p_notebook;
};

If i intitalise test (t2) with an integer, this works as expected:

int _tmain(int argc, _TCHAR* argv[]){

    int var=90;
    test t2(var);
    cout<<t2.int_test; // this gives 90
    var=30;
    cout<<t2.int_test; // this gives 30

Once i initialised the test class with a pointer to a member of class notebook through a third class factory:

factory f1;
notebook s1;
notebook s2;
s1.int_notebook=10;
s2.int_notebook=2;

int notebook::*p_notebook= &notebook::int_notebook;
f1.p_notebook=&s1;

test t1(((f1.p_notebook->*p_notebook)));
cout<<t1.int_test; // This gives  10

however if i change the pointer of f1.p_notebook to another object of notebook s2;

f1.p_notebook=&s2;
cout<<t1.int_test; // This gives  10

the reference member of a of t1 (t1.int_test) doesnt reflect the change of the pointer. could some one explain to me why ? or what i'm doing wrong here.

解决方案

Your class test has a reference to an int. It doesn't know anything about what object that int actually belongs to. Or how it originally got access to that int. Let's break down this line of code:

test t1(((f1.p_notebook->*p_notebook)));

First this:

f1.p_notebook

As of that line, it is a pointer to s1. Now this:

f1.p_notebook->*p_notebook

That's s1's int_notebook member. So with this:

test t1(((f1.p_notebook->*p_notebook)));

You are passing s1's int_notebook member to the constructor of test. So now the object t1 has a reference to s1's int_notebook member. It does not care about the obscure levels of indirection you used to get that member. It[t1] knows nothing of f1, or f1.p_notebook. So when you do this:

f1.p_notebook=&s2;

That has absolutely no effect on s1.int_notebook, and therefore it has no effect on t1's reference member.

这篇关于为什么引用在通过指针完成时中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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