静态功能 [英] static functions

查看:87
本文介绍了静态功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的问题听起来很愚蠢,但仍然想尝试一下

我的问题是,我们在类名的帮助下访问静态函数,如果在对象的帮助下访问静态函数,则会出错,这是为什么.

我知道静态块或静态函数在创建对象之前先获取内存,然后为什么不在创建对象后通过对象名称访问静态函数.我读了很多书,但是都没有明确的解释,请帮帮我

谢谢与问候
Radix

I know my question may sound stupid but still thought of giving it a try

My question is that we access a static function with the help of a class name and if we access the static function with the help of an object then we get error, why is this so.

I know a static block or function gets memory before the object gets created then why not access the static function via object name after the object creation. I read many books but none have a clear cut explanation for this please help me out

Thanks and Regards
Radix

推荐答案

有很多文章有明确的解释,您只需要正确阅读即可.

仅通过入口点的地址调用静态函数.相反,实例函数是在某些语言self中具有称为this的附加隐藏参数的函数.从技术上讲,它与其他任何参数(通常是第一个)完全传递.

因此,如果您有以下内容:

There is a lot of articles with clear explanation, you just need to read properly.

Static function is called just by address of the entry point. An instance function, in contrast, is a function with additional hidden parameter called this, in some languages self. Technically, it is passed exactly as any other parameter, usually first one.

So if you have this:

class MyClass {
   public void MyMethod(int parameter) {/*...*/ someField = parameter; }
   private int someField;
}
MyClass myVariable;
myVariable.MyMethod(3);



在幕后,它的工作原理完全像这样(伪代码!不是真实代码!):



Behind the scene it works exactly like this (pseudo-code! not real code!):

MyClass.MyMethod(myVariable, 3);



myVariable作为参数传递时,它提供对实例成员(如someField)的访问,与其他(非静态方法)相同:它们也需要这种方式,从调用实例方法传递.这就是为什么没有某些实例就无法一次从静态方法调用实例方法的原因.您只能从静态方法访问其他静态成员(包括方法).

自然,从实例方法中,您可以访问所有可见成员,包括静态成员和实例成员.

有关参考,请参阅我的其他答案:什么是额外的优势代表 [



As myVariable is passed as a parameter, it provide access to the instance members (like someField), same thing about other (non-static methods): they will need this way as well, passed from a calling instance method. That''s why you cannot call instance method from static once, without some instance. You can only access other static members (including methods) from static methods.

Naturally, from an instance method you can access all visible members, both static and instance ones.

For a reference, see my other Answer: What is the Extra Advantage of Delegate[^] — it''s more on delegates but can be useful.

That should resolve your confusion completely, I hope.

—SA


与语言语义/语法有关.例如,使用C#,它将无法编译:

It''s to do with the language semantics/syntax. Example, with C#, this won''t compile:

class A
{
    public static void Foo()
    {
    }
}

static void Main(string[] args)
{
    A a = new A();
    a.Foo();



但是以下C ++代码将编译并运行:



But the following C++ code will compile, and run:

class A
{
public:
    static void Foo(){}
};

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    A a;
    a.Foo();


类静态函数只不过是一个普通函数.它存在于类名称范围中,但看不到任何类数据.

类成员函数是对类实例的数据具有可见性的函数.

A class static function is nothing more than an ordinary function. It exist in the class name scope, but doesn''t see any class data.

A class member function is a function that have visibility of the data of a class instance.

struct A
{
    int m;
    static bool static_method(int x)
    {
        /* do something not using m */
        // return m==x; //this will be an error.
        return x==0;
    }
    bool method(int x)
    {
        /* do something with m */
        return m==x;
    }
};
void test()
{
    A a1, a2;
    a1.m=1;   a2.m=2;
    verify(a1.method(1)); //succeed
    verify(a2.method(2)); //succeed
    //verify(a1.method(3)); //will fail: 3!=1
    //verify(a2.method(4)); //will fail: 4!=2
    verify(A::static_method(0)); // will call A::static_method ...
    verify(a1.static_method(0)); // ... three time, all without ...
    verify(a2.static_method(0)); // ... access the m member.
    //verify(A::static_method(1)); //will fail: 1!=0
    //verify(a1.static_method(1)); //will fail: 1!=0
}



在C ++中,允许使用a1.static_method,但它与A::static_method完全相同,并且-无论如何,都不会从a1对象获得任何信息.
在C#中,语言的设计者决定不允许这种冗余方式指定相同的调用.



In C++, a1.static_method is allowed, but it is perfectly identical to A::static_method, and -in any case, will get nothing from the a1 object.
In C# the designer of the language decided not to allow that redundant way to specify the same call.


这篇关于静态功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆