Skip to content

Fantastic class design in Hust School of Cyber Science and Engineer which expands your imagination.

Notifications You must be signed in to change notification settings

xieyuschen/CarManager

Repository files navigation

whether an extension method or not in Cpp

  A wonderdul page to read in stackoverflow ! Here are some excellent paragraphs:
  Different languages approach development in different ways. In particular C# and Java have a strong point of view with respect to OO that leads to everything is an object mindset (C# is a little more lax here). In that approach, extension methods provide a simple way of extending an existing object or interface to add new features.
  There are no extension methods in C++, nor are they needed. When developing C++, forget the everything is an object paradigm --which, by the way, is false even in Java/C# . A different mindset is taken in C++, there are objects, and the objects have operations that are inherently part of the object, but there are also other operations that form part of the interface and need not be part of the class. A must read by Herb Sutter is What's In a Class?, where the author defends (and I agree) that you can easily extend any given class with simple free functions.

  As a particular simple example, the standard templated class basic_ostream has a few member methods to dump the contents of some primitive types, and then it is enhanced with (also templated) free functions that extend that functionality to other types by using the existing public interface. For example,std::cout << 1; is implemented as a member function, while std::cout << "Hi"; is a free function implemented in terms of other more basic members.

  Extensibility in C++ is achieved by means of free functions, not by ways of adding new methods to existing objects

So by the way,what's the free function? :)

  The term free function in C++ simply refers to non-member functions. Every function that is not a member function is a free function.

A Interface Principle

A worth reading passage about Interface Principle
Add some abstracts here about passage's points.

1. What's a class?

In my mind: a class is a collection to describe some real world thing, it owns some datas and methods.

So let's look class definition carefully:
A class describes a set of data along with the functions that operate on that data.
Pay attention to the describes of functions,a function belongs to class as long as function operate on the data.

Here is a demo,Does the f belongs to X?

--besides access right, f is not significant different from f as a member function in X.

class X { /*...*/ };

    /*...*/
 void f( const X& );

2. Interfacec Principle:

For a class X, all functions, including free functions, that both
  (a) "mention" X, and
  (b) are "supplied with" X
are logically part of X, because they form part of the interface of X.

In short,**if f is supplied in X or mentions

About

Fantastic class design in Hust School of Cyber Science and Engineer which expands your imagination.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published