G****A 发帖数: 4160 | 1 即在不不实例化任何对象的情况下,仍然能够用class method 实现一些功能(输出字符
串)?
比如,在Ruby下,可以这样做:
>>class Person
>> def self.info
>> puts "Hi, Ni hao"
>> end
>>end
>>Person.info
输出:
>>Hi, Ni hao
不知道用c++可以不??谢谢回答 | s***e 发帖数: 793 | 2 static method.
class person{
public:
static void info() const{ std::cout<<" hello"<
};
person::info();
【在 G****A 的大作中提到】 : 即在不不实例化任何对象的情况下,仍然能够用class method 实现一些功能(输出字符 : 串)? : 比如,在Ruby下,可以这样做: : >>class Person : >> def self.info : >> puts "Hi, Ni hao" : >> end : >>end : >>Person.info : 输出:
| z***e 发帖数: 5393 | 3 sure.
don't have to be static. Once the method doesn't use "this", even it's null,
you can still use it.
e.g.
class A
{
public:
void output(){cout<<"I did her!!!"<
};
...
A* a=null;
a->output();
..
it should work.
【在 G****A 的大作中提到】 : 即在不不实例化任何对象的情况下,仍然能够用class method 实现一些功能(输出字符 : 串)? : 比如,在Ruby下,可以这样做: : >>class Person : >> def self.info : >> puts "Hi, Ni hao" : >> end : >>end : >>Person.info : 输出:
| X****r 发帖数: 3557 | 4 This is bad programming practice. Don't ever do this.
null,
【在 z***e 的大作中提到】 : sure. : don't have to be static. Once the method doesn't use "this", even it's null, : you can still use it. : e.g. : class A : { : public: : void output(){cout<<"I did her!!!"<: }; : ...
| m******t 发帖数: 2416 | 5
I mean I haven't done C++ for a while, but doesn't this code break any time
when A::output() is changed to either reference an instance member, or
becomes virtual? And what do we gain from doing this, exactly?
This sounds to me very much like pointing a shotgun at your own foot, rig
the trigger with some remote control, and fedex the remote control to
somebody half a globe away, with a note that says "press whenever you like".
【在 z***e 的大作中提到】 : sure. : don't have to be static. Once the method doesn't use "this", even it's null, : you can still use it. : e.g. : class A : { : public: : void output(){cout<<"I did her!!!"<: }; : ...
|
|