c ++:成员函数和非成员函数之间的差异 [英] c++: Difference between member and non member functions

查看:505
本文介绍了c ++:成员函数和非成员函数之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成员函数和非成员函数在c ++中有什么区别。

解决方案

我现在将调用方法)和一个自由函数(我现在将调用函数)。



让我们只是说他们不是因此不同。对象代码通常可以编译为C(或汇编),它们是没有方法概念的过程语言。



现在,这是不对的,让我们来看看差异。


$ b

语法是任何语言的显而易见的部分,所以它是最简单的方法。



第一个注意:在C ++中有两种不同的方法多种其他语言), static 方法和常规方法。



两种方法都有完全访问权限类的内部( protected private 节)以及(当然)访问类 public 接口。



static > c>

在常规方法中,一个特殊的关键字( this 在C ++中)允许访问已经调用该方法的当前对象(通过 - > / code>, - > * b

一个常规的方法可能是 const 和/或 volatile 分别) const 和/或 volatile 限定对象。例如,不能在 const 对象上调用非< - code> const 方法。这可以被看作是方法中的合格 this ,即 void Foo :: bar()const 有一个



标记为 virtual 。 Virtuality通过启用覆盖来启用运行时多态性。我不会在这里扩展这个机制,让我们注意,函数不能是虚拟的。



一个经常被忽略的点是,方法( static 和regular)在类中作用域。这对于名称查找(其他方法或属性/变量)非常重要,因为它意味着在从类外声明的元素上的方法查找期间,类的元素具有优先级。



由于在属性或方法之前使用 this-> 的限定不是强制性的,在常规方法中很方便,虽然它可能引入微妙的错误。在静态方法中,它避免了通过类名限定要访问的静态属性和方法。



现在主要的语法差异已经被断言,让我们检查概念



OOP通常是将状态和行为联系在一起的状态)。这是通过创建分组属性(状态)和行为(方法)和(理论上),说明只有方法可以作用于状态。因此,在OOP中,方法负责实现类的行为。



这些方法参与状态的封装(从实现细节中释放客户端)保存类不变量(关于类状态的语句,从出生到死亡都是真实的,无论你用它做什么)。



C ++



在C ++中,如前所述,这是通过使用不同的访问级别( public protected private )并允许访问非 - public 级别到代码的受限部分。一般来说,属性将是私有的,因此只能被类方法访问(也可能有一些朋友,对于语法错误)。



注意:我敦促你不要使用 protected 属性,很难跟踪它们的修改,并且由于派生类的集合是无界的...它们的实现不能轻易地改变。



但是,请注意C ++不鼓励使用大量方法使界面膨胀。



麻烦的是,保持不变量越多,响应性越好,使得越难以跟踪错误并确保正确性。另外,由于方法依赖于类的内部,它使变化更加昂贵。



而在C ++中,通常建议写一个最小的方法集,其余的行为到非朋友函数(只要不会增加成本太多)。





这个答案变得相当冗长,但我怀疑我忽视了其他人会发现的差异批评...哦。好的。


What is the difference between member and nonnember functions in c++.

解决方案

There are several differences between a member function (which I will now call method) and a free function (which I will now call function).

First, let's just state that they are not so different. Object code can generally be compiled down to C (or assembly) which are procedural languages with no notion of methods. Both methods and functions are then called like subroutines.

Now that this is out of the way, let's look at the differences. They can be classified in two categories: conceptual and syntactic.

Syntactically

The syntax is the obvious part of any language, so it's the easiest to get out of the way.

First note: there are two different kinds of methods in C++ (and a number of other languages), the static methods and regular methods.

Both kinds of methods have full access to the class internals (protected and private sections) as well (of course) as access to the class public interface.

static methods are equivalent to friend functions (apart from some scoping differences).

Within a regular method, a special keyword (this in C++) allows access to the current object on which the method has been invoked (via the ., ->, .* or ->* operators).

A regular method may be const and/or volatile qualified, enabling it for (respectively) const and/or volatile qualified objects. For example, a non-const method cannot be called on a const object. This can be seen as qualifying this within the method, ie void Foo::bar() const has a this of type Foo const*.

A regular method may be marked as virtual. Virtuality enables runtime polymorphism by enabling overriding. I won't extend on this mechanism here, let's just note that functions cannot be virtual.

One often ignored point, is that methods (both static and regular) are scoped within the class. This is important for name lookup (of other methods or attributes/variables) as it means that the elements of the class have priority during lookup from a method on the elements declared outside of the class.

Since the qualification using this-> before attribute or methods is not mandatory, this comes handy in regular methods, though it may introduce subtle bugs. In static methods, it avoids qualifying by the class name the static attributes and methods one whishes to access.

Now that the main syntactic differences have been asserted, let's check the conceptual differences.

Conceptually

OOP is generally about tying together state and behavior (of this state). This is done by creating classes which group attributes (state) and behavior (methods) and (in theory) stating that only the methods can act on the state. Therefore, in OOP, the methods are responsible for implementing the behavior of the class.

The methods participate to the encapsulation of state (freeing clients from the implementation detail) and to the preservation of the class invariants (statements about the class state that hold true from its birth to its death, whatever you do with it).

C++

In C++, as we have seen previously, this is done by using different access levels (public, protected and private) and granting access to the non-public levels to a restricted portion of the code. Generally attributes will be private and thus only accessible to the class methods (and maybe some friends, for syntax quirks).

Note: I urge you not to use protected attributes, it's hard to track down their modifications and since the set of derived classes is unbounded... their implementation cannot be changed easily afterward.

However, beware that C++ discourages from bloating the interface with lots of methods.

The trouble is that because methods are responsible of maintaining invariants, the more there are and the more the responsability is spread, making it more difficult to track down bugs and ensure correctness. Also, since methods depends on the class internals, it makes change more costly.

Instead, in C++, it is generally advised to write a minimal set of methods and delegating the rest of the behavior to non-friend functions (as long as it doesn't increase the cost too much).

  • See Sutter's take on std::string in Monolith Unstrung.
  • The delegation to non-friend methods was emphasized by Sutter in its Interface Principle in which he states that functions that are delivered with the class (in the same file/same namespace) and use the class, are logically part of the class interface. He restates in Exceptional C++.

This answer is becoming rather long-winded, yet I suspect that I have overlooked differences that other would find critical... oh well.

这篇关于c ++:成员函数和非成员函数之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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