在 C++ 中是否可以在静态成员方法内部调用非静态成员方法?
我怀疑这是可能的,但值得一问:我想从静态成员函数内部调用非静态成员函数。我想为班级的每个当前实例执行此操作。是否可以?
当我在测试类中尝试此操作时,出现以下错误:
“无法在没有对象的情况下调用成员函数 'void TestClass::NonStaticMethod()'”
I doubt this is possible, but it's worth asking: I'd like to call a non-static member function from inside of a static one. I'd like to do it for every current instance of the class. Is it possible?
When I try this in a test class I get the following error:
"cannot call member function 'void TestClass::NonStaticMethod()' without an object"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以用一些技巧来做到这一点。如果您想跟踪所有实例,则必须在类构造时注册实例。当然,这很难做到正确,但这是一个粗略的方法:
您必须确保所有构造函数都执行注册。
You could do it with some trickery. If you want to keep track of all instances, you have to register the instances at class construction time. This is tricky to get right, of course, but here's a rough approach:
You'll have to make sure that all constructors perform the registration.
是的,您可以从静态成员内部调用非静态成员,错误消息甚至会建议如何调用。
您需要提供将应用非静态成员函数的实例:
或者
访问非静态成员变量也是如此:
至于在类的每个实例上执行此操作,您需要保留一个列表不知何故。
Yes, you can call a non-static member from inside a static member, and the error message even suggests how.
You need to provide the instance to which the non-static member function will be applied:
or
Same goes for access to non-static member variables:
As far as doing it on every instance of the class, it's up to you to keep a list somehow.
您不能直接调用非静态方法,但如果您想对每个当前实例执行某些操作,可以通过维护实例的静态列表并迭代此列表来实现。类的构造函数可以将自身添加到此静态列表中,析构函数可以删除自身。
You can't directly call a non-static method, but if you want to do something on every current instance, you can do so by maintaining a static list of instances and iterating over this list. The constructor of your class can add itself to this static list and the destructor can remove itself.
嗯,你可以稍微调整一下。基本上将类的每个实例存储在构造函数中。现在我不太记得C++了,所以下面的代码可能不起作用。我只是想让你知道什么是有效的
Well, you can tweak it a little. Basically Store every instance of the class in the constructor. Now I dont remember C++ much, so the following code might not work,. I'm just trying to give you an idea of what would work
唯一可以合法调用非静态函数的时间是当您有一个对象可以调用该函数时。由于静态函数中没有
this
,因此您不能说instanceMethod();
甚至MyClass::instanceMethod();;不知怎的,你必须说
obj.instanceMethod();
或objPtr->instanceMethod();
。但请注意,这意味着如果您从静态函数中创建类的对象(或者可以检索一个对象),则您几乎可以调用您喜欢的任何内容。不过,默认情况下,C++ 并不知道您创建了多少个特定类型的对象或它们在哪里,因此您必须自己跟踪这一点。一种方法可能是拥有一个静态列表/数组/映射/集合/在构造函数中添加到析构函数中并从析构函数中删除的任何对象。
The only time you can legitimately call a non-static function is when you have an object to call the function on. Since you don't have a
this
in a static function, you can't sayinstanceMethod();
or evenMyClass::instanceMethod();
; somehow you have to sayobj.instanceMethod();
orobjPtr->instanceMethod();
.Note, though, this means if you create an object of your class from within your static function (or can retrieve one), you can call pretty much whatever you like on it. C++ doesn't by default know how many objects you've created of a particular type or where they are, though, so you have to keep track of this yourself. One way might be to have a static list/array/map/set/whatever of objects that you add to in the constructor and remove from in the destructor.