How to Access a Private Variable?

2020-08-02 19:21发布

This question isn't meant to sound as blatantly insulting as it probably is right now.

This is a homework assignment, and the spec sheet is scarce and poorly designed to say the least. We have a function:

double refuel( int liter, GasStation *gs )
{
    // TODO: Access private variable MaxFuel of gs and decrement.
}

Sound simple enough? It should be, but the class GasStation comes with no function that accesses the private variable MaxFuel. So how can I access it anyway using the function refuel?

I'm not considering creating a function setFuel( int liter ) because the teacher always complains rather energetically if I change his specification. So... I guess I have to do some sort of hack around it, but I'm not sure how to go about this without explicitely changing the only function in GasStation and giving it a parameter so that I can call it here.

Any hints perhaps?

7条回答
时光不老,我们不散
2楼-- · 2020-08-02 19:59

Declare refuel(..) as a friend function. Otherwise, you are out of luck.

查看更多
登录 后发表回答