如何访问私有变量?
这个问题听起来并不像现在这样公然侮辱。
这是一项家庭作业,而且规格表很少,至少可以说设计很差。我们有一个函数:
double refuel( int liter, GasStation *gs )
{
// TODO: Access private variable MaxFuel of gs and decrement.
}
听起来足够简单吗?应该是这样,但是 GasStation 类没有提供访问私有变量 MaxFuel 的函数。那么如何使用 refuel 功能来访问它呢?
我不考虑创建一个函数 setFuel( int litre )
因为如果我改变他的规范,老师总是会非常激烈地抱怨。所以...我想我必须对其进行某种破解,但我不确定如何在不明确更改 GasStation 中的唯一函数并给它一个参数以便我可以在此处调用它的情况下进行此操作。
也许有什么提示吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编译并运行测试程序。在调试器中运行它并检查 GasStation 的内存布局。计算以字节为单位的确切距离 从 GasStation 的起点到您需要设置的 int 的距离是多少(请注意,如果这是第一件事并且没有虚函数,那么该距离保证为 0,因此您可以省略前几个步骤)。
使用此值将指针增加到对象内需要设置数据的位置,如 Kirill 显示的那样。然而,只是为了成为一个屁股——你的老师应得的——不要在这里使用任何符号语言...直接使用距离值,如下所示:
或者任何 42 应该是到达正确的地方。
如果你真的想成为一个混蛋,请在你的函数中添加一个假变量。编译并运行程序,然后在堆栈上找到该假变量与 GasStation 指针数据位置之间的距离。然后执行以下操作:
Compile and run a test program. Run it in a debugger and examine the memory layout of GasStation. calculate the exact distance in bytes what the distance from the start of a GasStation to the int you need to set is (note if it's the first thing and there's no virtual functions then that distance is guaranteed to be 0 so you can omit the first few steps).
Use this value to increment the pointer to the position within the object where you need to set the data as Kirill shows. However, just to be an ass--your teacher deserves it--do not use any symbolic language here...use the value of the distance directly like so:
or whatever 42 should be to get to the right place.
If you really want to be an ass, put a fake variable in your function. Compile and run the program and then find the distance on the stack between this fake variable and the location of the GasStation pointer data. Then do this:
对于你老师糟糕的作业,我有一个相当糟糕的解决方案。
I have a suitably terrible solution for your teacher's terrible assignment.
显然,
GasStation
的重点是提供GasStation::pump(Car&)
。这将为您减少private: unsigned int MaxFuel
。Obviously the point of a
GasStation
is to offerGasStation::pump(Car&)
. This will decrementprivate: unsigned int MaxFuel
for you.这很脏,但您可以这样做:
现在编译器认为该标头中的所有内容都是公共的。喜悦!
This is dirty, but you can do:
Now the compiler thinks everything in that header is public. Joy!
最好的方法是向
GasStation
添加公共成员函数。不太安全的做法是让
refuel
成为友元函数。如果您知道该类中成员 MaxFuel 的偏移量,则绝对不安全的选项可用。然后您可以按如下方式更改它(切勿在生产代码中执行此操作):
Best way is to add public member function to
GasStation
.Something less safe is to make
refuel
a friend function.Absolutely not safe option is available if you know offset of member
MaxFuel
in that class. Then you could change it as follows (NEVER DO THIS IN PRODUCTION CODE):现在这听起来像是一个真正糟糕的家庭作业。
无论如何,我可以想到三种访问私有数据的方法:
friend
的作弊:
前两个是合法的,但要求你更改类。最后一个是“非侵入式”(无论如何,对于“非侵入式”的某些定义),但绝对是非法的(尽管我还没有看到编译器无法工作)。
Now that sounds like a real lousy homework assignment.
Anyway, I can think of three ways to access private data:
friend
of the classcheating:
The first two are legal, but require you to change the class. The last is "non-intrusive" (for some definition of "non-intrusive", anyway), but is definitely illegal (although I have yet to see a compiler were it wouldn't work).
将
refuel(..)
声明为友元函数。否则,你就不走运了。Declare
refuel(..)
as a friend function. Otherwise, you are out of luck.