Class interfaces: basic or complex?

I am writing a container class for fun and education. Previously, when writing classes, I contented containers several key ways: GetValue, SetValue, GetSizeand Resize. I did this to avoid "code spaghetti", so my class will be easier to debug.

However, it occurred to me that class users might want to do more than just a substitution. So I added a few more methods:

void Replace(const std::size_t Start, const std::size_t End, const T Value);
void Replace(const std::size_t Start, const std::size_t End, const MyClass Other);
void Insert(const std::size_t Index, const T Value);
void Insert(const std::size_t Index, const MyClass Other);
void Delete(const std::size_t Index);
void Delete(const std::size_t Start, const std::size_t End);

In general, if classes provide only the most basic interface and allow class users to perform their own functions to do complex things? Or is it necessary that complex material be embedded due to maintainability?

+3
source share
5

/ - (, , !). non-friend non-member. , , - .

: . , "convienence" , , .

, - , , .

, . Scott Meyer " ++" ( 3- ) Sutter Alexandrescu " ++".

+2

, ( , ), , O (N * M), N M - .

, , STL.

, , . . .

, .

+2

, , . . , , , :

 void Replace(const std::size_t Start, const std::size_t End, const T Value);

 template<class ContainerType>
 void ReplaceAllElementsInContainer(ContainerType& Container, const std::size_t Start, const std::size_t End, const T Value);

. , .

- ( ++) ( "" ). , .

+2

. , 2 " " "".

, , " " , , , :


container.hpp

class Container
{
protected:
   int GetValue();
   void SetValue(int newValue);

   size_t GetSize();

   void Resize(size_t);
};

, " ":


mcontainers.hpp

#include "containers.hpp";

class MethodContainer: public Container
{
protected:
  void Replace(const std::size_t Start, const std::size_t End, const T Value);
  void Replace(const std::size_t Start, const std::size_t End, const MyClass Other);
  void Insert(const std::size_t Index, const T Value);
  void Insert(const std::size_t Index, const MyClass Other);
  void Delete(const std::size_t Index);
  void Delete(const std::size_t Start, const std::size_t End);

}

, , :


stacks.hpp

#include "containers.hpp";
#include "mcontainers.hpp";

#define pointer void*

class Stack: public MethodContainer
{
public:
  // these methods use "mcontainer::Insert", "mcontainer::Replace", etc
  void Push(pointer Item);
  void Pop();
  pointer Extract();
}

AS @Chris , , , " ", .

, /. . Langr. ++. Altought, ++, ++, , , , .

" " "" " " . "private", .

. (, ), ​​ .

+1

, , .

However, as soon as someone else uses the container, or if you plan to use it in other areas, I would recommend adding interface methods that work with iterator types, then your container is much more open for use with stdlib containers and algorithms. Use the stdlib container interfaces as an example.

0
source

All Articles