我随机地玩弄指针和参考文献。
class Product {
int price,qty;
public:
void setData(int price, int qty) {
this->price = price;
(*this).qty = qty;
}
void billing() {
cout << price * qty;
}
};
int main() {
Product *PenObj;
(*PenObj).setData(50,25);
(*PenObj).billing();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这不打印账单。但是当我使用引用对象时,它会打印出账单。
小智 5
您只声明了一个指向 的指针PenObj,但没有创建 的对象PenObj。它应该是Product *PenObj = new PenObj
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |