This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
cpp:syntax:class:generic [2021/04/06 17:02] bmonsuez [Pourquoi des classes génériques en C++] |
cpp:syntax:class:generic [2022/11/18 10:47] (current) |
||
---|---|---|---|
Line 71: | Line 71: | ||
public: | public: | ||
- | buffer(const buffer& anotherBuffer) | + | buffer(const buffer<itemT>& anotherBuffer) |
{ | { | ||
std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + 128, m_buffer); | std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + 128, m_buffer); | ||
} | } | ||
int size() const { return 128; } | int size() const { return 128; } | ||
- | buffer& operator = (const buffer& anotherBuffer) | + | buffer& operator = (const buffer<itemT>& anotherBuffer) |
{ | { | ||
std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + 128, m_buffer); | std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + 128, m_buffer); | ||
Line 169: | Line 169: | ||
public: | public: | ||
- | buffer(const buffer& anotherBuffer) | + | buffer(const buffer<itemT, bufferSize>& anotherBuffer) |
{ | { | ||
std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + bufferSize, m_buffer); | std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + bufferSize, m_buffer); | ||
} | } | ||
int size() const { return bufferSize; } | int size() const { return bufferSize; } | ||
- | buffer& operator = (const buffer& anotherBuffer) | + | buffer& operator = (const buffer<itemT, bufferSize>& anotherBuffer) |
{ | { | ||
std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + bufferSize, m_buffer); | std::copy(anotherBuffer.m_buffer, anotherBuffer.m_buffer + bufferSize, m_buffer); | ||
Line 198: | Line 198: | ||
</code> | </code> | ||
+ | |||
+ | Désormais, quand nous créons un buffer, nous pouvons spécifier la taille du buffer. | ||
+ | |||
+ | <code cpp> | ||
+ | buffer<int, 64> bufferOfIntegers; // buffer contenant 64 entiers. | ||
+ | buffer<char, 32> bufferOfChars; // buffer contenant 32 caractères. | ||
+ | buffer<float> bufferOfFloats; // buffer contenant 128 (la valeur par défaut) nombres à virgule flottante. | ||
+ | </code> | ||
+ | |||
+ | ===== Pour aller plus loin ===== | ||
+ | |||
+ | [[https://en.cppreference.com/w/cpp/language/class_template|Template class]] | ||
+ | |||
+ | [[https://en.cppreference.com/w/cpp/language/template_parameters|Template parameters and template arguments]] | ||
+ | |||
+ | [[https://en.cppreference.com/w/cpp/language/template_argument_deduction|Template argument deduction]] | ||
+ | |||
+ | [[https://en.cppreference.com/w/cpp/language/constraints|Contraints & Concepts]] | ||