What is the difference between abstract class and

2019-02-21 15:22发布

Example:

Iterators are pure abstractions: Anything that behaves like an iterator is an iterator.

What does it mean?

7条回答
趁早两清
2楼-- · 2019-02-21 15:27

In your example, you're talking about Iterators. In C++, and more specifically in the standard library, the term Iterators doesn't refer to a pure abstract class but to what are called concepts. Concepts are used with templates rather than with virtual/inheritance-based polymorphism. Currently (C++11), concepts are only defined in the library documentation, i.e. they do not (yet) exist as part of the C++ language itself. The standard library documents concepts, for example the "Iterator" concept, as a set of requirements for any type/object to be accepted as a type parameter of a template that wants to work with an "Iterator". A set of requirements is defined in terms of which expressions are valid on an object, regardless of its type. It's a form of duck-typing. For example, see : http://en.cppreference.com/w/cpp/concept/Iterator

查看更多
对你真心纯属浪费
3楼-- · 2019-02-21 15:30

Nothing. The C++ Standard states only that a), a class is abstract if it has at least one pure virtual function, direct or inherited, and b), you can't instantiate an abstract class directly. There's no such thing as a pure abstract class.

查看更多
beautiful°
4楼-- · 2019-02-21 15:32

Abstract Class *will atleast have one pure virtual function and can have data members.

Pure Abstract Class is just like an interface. Only pure virtual functions can be defined here. No data members or method definition can be done here.

For more information visit: (https://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes/Pure_Abstract_Classes)

查看更多
叛逆
5楼-- · 2019-02-21 15:38

I would think a pure abstract class is the C++ equivalent of an interface.

See here:

A pure Abstract class has only abstract member functions and no data or concrete member functions. In general, a pure abstract class is used to define an interface and is intended to be inherited by concrete classes. It's a way of forcing a contract between the class designer and the users of that class. The users of this class must declare a matching member function for the class to compile.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-02-21 15:38

An abstract class is a class with some functionality but some that needs to be implemented, whereas a pure abstract class has none of its functionality implemented.

This is a bit like an interface in other languages such as C# and Java.

A pure abstract class would serve the purpose of specifying a 'contract' that concretions of the pure abstract class must adhere to.

查看更多
Rolldiameter
7楼-- · 2019-02-21 15:45

An abstract class has at least one pure virtual function. This is standard C++ terminology.

Some people use the term pure abstract class to describe a class that has nothing but pure virtual functions (in other words, no data members and no concrete functions). This is equivalent to Java interfaces.

Now to your actual question:

Iterators are pure abstractions: Anything that behaves like an iterator is an iterator.

This has nothing to do with abstract classes (pure or otherwise). All it's saying is that anything that fulfils the iterator contract is an iterator. It doesn't even have to be a class (think pointers).

查看更多
登录 后发表回答