
[英]Why does #define _LARGEFILE_SOURCE break these function pointers?
[英]Why does referencing break when done through pointers
我有一个类测试引用的默认构造函数。
class test {
public:
test(int &input1) : int_test(input1) {};
~test() {};
int & int_test;
};
然后再有2个与test交互的类,如下所示:
class notebook
{
public:
notebook() {};
~notebook() {};
int int_notebook;
};
class factory
{
public:
factory() {};
~factory(){};
notebook *p_notebook;
};
如果我使用整数初始化测试(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
一旦我通过第三类工厂使用指向类笔记本成员的指针初始化了测试类:
factory f1;
notebook s1;
notebook s2;
s1.int_notebook=10;
s2.int_notebook=2;
int notebook::*p_notebook= ¬ebook::int_notebook;
f1.p_notebook=&s1;
test t1(((f1.p_notebook->*p_notebook)));
cout<<t1.int_test; // This gives 10
但是,如果我将f1.p_notebook的指针更改为笔记本s2的另一个对象;
f1.p_notebook=&s2;
cout<<t1.int_test; // This gives 10
t1的a的引用成员(t1.int_test)不反映指针的变化。 有人可以向我解释为什么吗? 或者我在这里做错了。
class CTester
{
public:
CTester(int &input1) : int_test(input1)
{
std::cout << int_test << std::endl;
}
int &int_test;
};
class notebook
{
public:
int int_notebook;
};
class factory
{
public:
notebook *p_notebook;
};
int main()
{
factory f1;
notebook s1;
notebook s2;
s1.int_notebook=10;
s2.int_notebook=2;
int notebook::*p_notebook = ¬ebook::int_notebook;
f1.p_notebook=&s1;
CTester t1(f1.p_notebook->*p_notebook);
f1.p_notebook=&s2;
CTester t2(f1.p_notebook->*p_notebook);
return 0;
}
此打印
10
2
您的类test
引用了一个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
成员传递给test的构造函数。 因此,现在对象t1
引用了s1的int_notebook
成员。 它并不关心您用来获取该成员的间接的晦涩难懂的级别。 It [t1]对f1或f1.p_notebook一无所知。 因此,当您执行此操作时:
f1.p_notebook=&s2;
这对s1.int_notebook绝对没有影响,因此对t1的引用成员没有影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.