c# 泛型类型调用成员函数_C ++中类成员函数的类型

c# 泛型类型调用成员函数

We already know what member functions are, what they do, how to define member function and how to call them using class objects. Now lets learn about some special member functions which can be defined in C++ classes. Following are the different types of Member functions:

我们已经知道什么是成员函数,它们做什么,如何定义成员函数以及如何使用类对象调用它们。 现在让我们了解一些可以在C ++类中定义的特殊成员函数。 以下是成员函数的不同类型:

  1. Simple functions

    简单的功能

  2. Static functions

    静态功能

  3. Const functions

    const函数

  4. Inline functions

    内联函数

  5. Friend functions

    朋友功能

C ++中的简单成员函数 (Simple Member functions in C++)

These are the basic member function, which dont have any special keyword like static etc as prefix. All the general member functions, which are of below given form, are termed as simple and basic member functions.

这些是基本的成员函数,没有任何特殊关键字,例如static等作为前缀。 具有以下给定形式的所有常规成员函数均称为简单成员函数和基本成员函数。

return_type functionName(parameter_list)
{
    function body;
}

C ++中的静态成员函数 (Static Member functions in C++)

Static is something that holds its position. Static is a keyword which can be used with data members as well as the member functions. We will discuss this in details later. As of now we will discuss its usage with member functions only.

静态是保持其地位的东西。 静态是可以与数据成员以及成员函数一起使用的关键字。 稍后我们将详细讨论。 到目前为止,我们将仅与成员函数讨论其用法。

A function is made static by using static keyword with function name. These functions work for the class as whole rather than for a particular object of a class.

通过使用带有功能名称的static关键字将功能static 。 这些函数对整个类有用,而不是对类的特定对象起作用。

It can be called using the object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.

可以使用对象和直接成员访问来调用它. 操作员。 但是,使用类名称和作用域解析::运算符本身调用静态成员函数更为典型。

For example:

例如:

class X
{
    public:
    static void f()
    {
        // statement
    }
};

int main()
{
    X::f();   // calling member function directly with class name
}

These functions cannot access ordinary data members and member functions, but only static data members and static member functions can be called inside them.

这些函数无法访问普通数据成员和成员函数,但是只能在内部调用static数据成员和static成员函数。

It doesn't have any "this" keyword which is the reason it cannot access ordinary members. We will study about "this" keyword later.

它没有任何“ this”关键字,这是它无法访问普通成员的原因。 稍后我们将研究“ this”关键字。

C ++中的const成员函数 (Const Member functions in C++)

We will study Const keyword in detail later(Const Keyword), but as an introduction, Const keyword makes variables constant, that means once defined, there values can't be changed.

稍后我们将详细研究Const关键字( Const Keyword ),但是作为介绍,Const关键字使变量不变,这意味着一旦定义,就无法更改值。

When used with member function, such member functions can never modify the object or its related data members.

与成员函数一起使用时,此类成员函数永远不能修改对象或其相关数据成员。

// basic syntax of const Member Function

void fun() const 
{
    // statement
}

C ++中的内联函数 (Inline functions in C++)

All the member functions defined inside the class definition are by default declared as Inline. We will study Inline Functions in details in the next topic.

默认情况下,在类定义中定义的所有成员函数都声明为Inline。 在下一个主题中,我们将详细研究内联函数。

C ++中的Friend函数 (Friend functions in C++)

Friend functions are actually not class member function. Friend functions are made to give private access to non-class functions. You can declare a global function as friend, or a member function of other class as friend.

朋友函数实际上不是类成员函数。 通过使用Friend函数可以私有访问非类函数。 您可以将全局函数声明为friend,或将其他类的成员函数声明为friend。

For example:

例如:

class WithFriend
{
    int i;
    public:
    friend void fun(); // global function as friend
};

void fun()
{
    WithFriend wf;
    wf.i=10;  // access to private data member
    cout << wf.i;
}

int main()
{
    fun(); //Can be called directly
}

Hence, friend functions can access private data members by creating object of the class. Similarly we can also make function of some other class as friend, or we can also make an entire class as friend class.

因此,朋友功能可以通过创建类的对象来访问私有数据成员。 类似地,我们也可以将其他某个类的功能作为friend类,或者我们也可以将整个类作为friend类

class Other
{
    void fun();
};

class WithFriend
{
    private:
    int i;
    public:
    void getdata();  // Member function of class WithFriend
    
    // making function of class Other as friend here
    friend void Other::fun();   
    
    // making the complete class as friend
    friend class Other;  
};

When we make a class as friend, all its member functions automatically become friend functions.

当我们将一个类作为朋友时,其所有成员函数都会自动成为朋友函数。

Friend Functions is a reason, why C++ is not called as a pure Object Oriented language. Because it violates the concept of Encapsulation.

Friend Functions是为什么不能将C ++称为纯面向对象语言的原因 。 因为它违反了封装的概念。

翻译自: https://www.studytonight.com/cpp/types-of-member-function.php

c# 泛型类型调用成员函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值