我想通过引用发送一个对象。该对象代表一个类
[test.php的]
$car= new Car();
$car->l=1000;
$car2 = new Car2();
$car2->method($car);
[php_cod.cc]
PHP_METHOD(Car2, method)
{
Car2 *car;
Car obj11;
zend_class_entry ce2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj11) == FAILURE) {
RETURN_NULL();
}
car2_object *obj = (car2_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
car2 = obj->car2;
if (car2 != NULL) {
//cout<<"in car 2 ref"<<endl;
//car2->reference(s);
(car2->method(obj11));
}
}
[test.cc]
void Car2::method(Car &carr)
{
cout<<"IN THE REFERENCE CLASS"<<carr.l<<endl;
}
我哪里错了? 谢谢!欣赏
当我运行php test.php时,值vor carr.l等于 155160836.为什么?我哪里错了
答案 0 :(得分:0)
在代码的php部分,obj11
是一个未初始化的指针,它被传递给Car2 :: method((car2->method(*obj11));
)。所以 -
cout<<"IN THE REFERENCE CLASS"<<carr.l<<endl;
// carr is a garbage reference and is causing the segmentation fault