调用成员函数而不创建类的对象或指针? [英] invoking member functions without creating an object or pointer of the class?
问题描述
是否有可能在不创建该类的对象(甚至是指针)的情况下调用类
的成员函数。
例如。
#include< iostream.h>
班级考试
{
public:
void fun()
{
cout<<" Inside test :: fun\\\
";
}
};
我想调用类测试的fun()而不创建
test t甚至test * ptr ;?
我在网上搜索了令人信服的答案,但没有得到任何答案。
( http://www.devx.com/tips/Tip/15846 )
或者这是一个完全疯狂的问题? (我不介意,如果有人同意
那个:) ..
谢谢和问候,
Yogesh Joshi
[见 http://www.gotw .ca / resources / clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
eg.
#include <iostream.h>
class test
{
public:
void fun()
{
cout<<"Inside test::fun\n";
}
};
I want to call fun() of class test without creating
test t or even test * ptr;?
I searched on the net for the convincing answer but didn''t get any.
(http://www.devx.com/tips/Tip/15846 )
Or is this a totally insane question? (I won''t mind if anyone agrees
with that :)..)
Thanks and Regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
yp *********@indiatimes.com 写道:
是否有可能在不创建对象的情况下调用类的成员函数(甚至指向该类的指针。
不,除非它是静态成员。
例如。
#include< iostream.h>
没有这样的标准标题。
班级测试
{
公开:
虚空乐趣()
{
cout<<" Inside test :: fun\\\
";
}
};
我想调用fun()类测试没有创建测试t甚至测试* ptr;?
因为你的有趣功能不需要一个物体,所以要静止。
我在网上搜索了令人信服的信息回答但没有得到任何答案。
( http:// www.devx.com/tips/Tip/15846 )
或者这是一个完全疯狂的问题? (我不介意,如果有人同意
:)..)
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
No, unless it''s a static member.
eg.
#include <iostream.h>
No such standard header.
class test
{
public:
void fun()
{
cout<<"Inside test::fun\n";
}
};
I want to call fun() of class test without creating
test t or even test * ptr;?
Since your ''fun'' function does not need an object, make it ''static''.
I searched on the net for the convincing answer but didn''t get any.
(http://www.devx.com/tips/Tip/15846 )
Or is this a totally insane question? (I won''t mind if anyone agrees
with that :)..)
它不是。但它似乎源于糟糕的设计(或误解
的非静态成员功能)。
V
It''s not. But it seems to stem from a poor design (or misunderstanding
of the purpose of non-static member functions).
V
yp*********@indiatimes.com 写道:
是否有可能调用类的成员函数
而无需创建该类的对象(甚至是指针)。
[]
或者这是一个完全疯狂的问题? (我不介意,如果有人同意
:)..)
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
[]
Or is this a totally insane question? (I won''t mind if anyone agrees
with that :)..)
好像是一个。
正确设计的类的成员函数访问对象的数据
成员。如果没有,那么该函数不应该是成员。那么,
如果你没有函数操作的数据,
函数应该在哪里查找数据?
>
请记住,一个成员函数是一个带有隐藏的
参数的函数,该函数在函数内部名称为this。
给出声明:
struct A {void foo(); };
A :: foo()基本上是
void foo(A * const this);
>
并且您可以破解该语言并在没有有效对象的情况下调用它,例如
这个:
((A *)0) - > ; foo();
但是一旦函数访问''this'指针所指向的数据,你就会遇到麻烦。
[见 http://www.gotw.ca/resources/clcm .htm 有关的信息]
[comp.lang.c ++。版主。第一次海报:做到这一点! ]
It seems like one.
A member function of a properly designed class accesses object''s data
members. If it does not, then the function should not be a member. So,
if you don''t have the data the function operates upon, where is the
function is supposed to find the data?
Bear in mind, that a member function is a function with a hidded
argument that has name ''this'' inside the function.
Given the declaration:
struct A { void foo(); };
A::foo() is essentially
void foo(A* const this);
And you can hack the language and call it without a valid object like
this:
((A*)0)->foo();
But once the function accesses data pointed to by ''this'' pointer you
are in troubles.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
2006年1月16日09:14:36 -0500, yp ********* @ indiatimes.com 写道:
On 16 Jan 2006 09:14:36 -0500, yp*********@indiatimes.com wrote:
是否有可能调用类的成员函数<没有创建该类的对象(甚至是指针)。
例如。
#include< iostream.h>
课堂测试
{
public:
void fun()
{
cout<<" Inside test :: fun \ n";
}
} ;
我想在没有创建测试的情况下调用类测试的fun(),甚至测试* ptr;?
我在网上搜索了令人信服的答案,但没有'得到任何。
( http://www.devx。 com / tips / Tip / 15846 )
或者这是一个完全疯狂的问题? (我不介意,如果有人同意那个:) ..
感谢和问候,
Yogesh Joshi
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
eg.
#include <iostream.h>
class test
{
public:
void fun()
{
cout<<"Inside test::fun\n";
}
};
I want to call fun() of class test without creating
test t or even test * ptr;?
I searched on the net for the convincing answer but didn''t get any.
(http://www.devx.com/tips/Tip/15846 )
Or is this a totally insane question? (I won''t mind if anyone agrees
with that :)..)
Thanks and Regards,
Yogesh Joshi
>
fun必须是静态成员函数。
static void fun()
{
std :: cout<< inside test :: fun\\\
;
}
由于该方法根本不使用this指针,它可能应该
无论如何都是静态的。
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
fun must be a static member function.
static void fun()
{
std::cout << "inside test::fun\n";
}
As the method doesn''t use the this pointer at all, it probably should
be static anyway.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
这篇关于调用成员函数而不创建类的对象或指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!