由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++ 是否也有class method??
相关主题
C++编程问题,关于static member 在class中的使用,谢谢A question about singleton
请教个static_cast vs reinterpret_cast的问题。how to statically initialze a mutex in class?
好久没用C++了,想用静态变量写一个简单双向链表,一直报错JAVA 考试题请教
C++默认的copy constructor的疑惑这个函数有问题吗?
问题: C++ static_cast between int and float递归程序
C++ static member method with default arguments[c++] static function in a class
c++ singleton questions请教一个static 函数的问题
[合集] 为什么很少有人用static来实现signleton?why use static function here?
相关话题的讨论汇总
话题: method话题: class话题: c++话题: hi话题: ni
进入Programming版参与讨论
1 (共1页)
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!!!"<: };
: ...

1 (共1页)
进入Programming版参与讨论
相关主题
why use static function here?问题: C++ static_cast between int and float
Test your C++ knowledge...C++ static member method with default arguments
static function and static variable?c++ singleton questions
有没有static return type和static as function arguement?[合集] 为什么很少有人用static来实现signleton?
C++编程问题,关于static member 在class中的使用,谢谢A question about singleton
请教个static_cast vs reinterpret_cast的问题。how to statically initialze a mutex in class?
好久没用C++了,想用静态变量写一个简单双向链表,一直报错JAVA 考试题请教
C++默认的copy constructor的疑惑这个函数有问题吗?
相关话题的讨论汇总
话题: method话题: class话题: c++话题: hi话题: ni