Is this a good (correct) way to encapsulate a collection?

class MyContainedClass {
};

class MyClass {
public:
  MyContainedClass * getElement() {
    // ...
    std::list<MyContainedClass>::iterator it = ... // retrieve somehow
    return &(*it);
  }
  // other methods
private:
  std::list<MyContainedClass> m_contained;
};

Although msdn says std::listit should not do moving items when deleting or pasting, is it a good and common way to return a pointer to a list item?

PS: I know that I can use a set of pointers (and should have deleteelements in the destructor), collect common pointers (which I don't like), etc.

+3
source share
9 answers

I don't see the use of encapsulating this, but it can only be me. In any case, returning a link instead of a pointer makes much more sense to me.

+1
source

, " " "MyClass", MyClass .

, MyClass , . , , , " , ", " , ".

+1

...

, , , .

, , . , std:: list // .

, std:: list . , " ", STL.

, .

, == std:: list < > . end(), , , , .

, , :

const ...

, , MyContainedClass, / , .

, , const ( ), , ( setElement?).

  const MyContainedClass * getElement() const {
    // ...
    std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
    return &(*it);
  }

.

?

(.. ), . - :

  const MyContainedClass & getElement() const {
    // ...
    std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
    return *it;
  }

. :-p

?

? , , , , .

const_iterator, , .

  std::list<MyContainedClass>::const_iterator getElement() const {
    // ...
    std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
    return it;
  }

, . , , std:: list, ...

+1
STL: 50 , , .
+1

, MyClass. , , , - , STL. , typedef std::list<MyContainedClass> MyClass .

, MyClass, , , , .

, ... , , , : , , , , MyClass... , , , . , ; , , , , , . .


@cos:

, MyContainedClass . :

, , , . - Document - : NewParagraph(), DeleteParagraph() GetParagraph(), (std::list), std::list " ". Document std:: list , ... , , - , , - .

( ) ( ).

. , , : Paragraph s, Paragraph , . , - :

, , .

. ParagraphSelectionDialog Paragraph , Document. , Document - Document - , ParagraphSelectionDialog! - Paragraph , deallocated Paragraph, , Paragraph! , , Document , , Paragraph.

... . , , , , , . , , . , , , std::list , . ... Paragraph , , , , .

: , , , . - , , Document , ... . , Document .

+1

@cos. , , , ++ - . , , , (), () , , .

, , , , , Paragraph Document , , Document .

, Views .

Toughness

, i, . , , -, , , .

, , .NET Java.

, . . , , , , .

, , , , ?

, / . , , - , /. .

, , , , , , , .

  • , / .

  • , / , , .

  • , .

  • , , , .

+1

, , , , , , . , , .

0

std:: list , ( , ), .

, . , :

  const MyContainedClass * getElement() const {
    // ...
    std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
    return &(*it);
  }

MyContainedClass,

    const MyContainedClass& getElement() const {
    // ...
    std::list<MyContainedClass>::const_iterator it = ... // retrieve somehow
    return *it;
  }

NULL.

0

STL , , , . , , , STL .

, , .. , . , deque map to hash_map ..

, , , - -, . , . , - (, )

It would be simpler if the STl classes intended for the inherited, but for efficiency, it was decided not to do this. Google for “inherit from STL classes” for more thoughts on this.

0
source

Source: https://habr.com/ru/post/1697260/


All Articles