Click here to Skip to main content
15,884,425 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: array => vector Pin
Stephen Hewitt17-Jun-10 14:18
Stephen Hewitt17-Jun-10 14:18 
GeneralRe: array => vector [modified] Pin
VeganFanatic17-Jun-10 14:27
VeganFanatic17-Jun-10 14:27 
GeneralRe: array => vector Pin
Stephen Hewitt17-Jun-10 14:57
Stephen Hewitt17-Jun-10 14:57 
GeneralRe: array => vector Pin
VeganFanatic17-Jun-10 15:10
VeganFanatic17-Jun-10 15:10 
GeneralRe: array => vector Pin
Stephen Hewitt17-Jun-10 15:45
Stephen Hewitt17-Jun-10 15:45 
GeneralRe: array => vector Pin
VeganFanatic17-Jun-10 15:46
VeganFanatic17-Jun-10 15:46 
GeneralRe: array => vector Pin
VeganFanatic17-Jun-10 15:58
VeganFanatic17-Jun-10 15:58 
AnswerRe: array => vector Pin
Aescleal17-Jun-10 22:18
Aescleal17-Jun-10 22:18 
If you want to do something with an array and not a pointer to a lump of data then declare the function as:

template <std::size_t N> my_class &operator=(T (&t)[N])


where T is the type you're templating on. Usually what you do is have a copy constructor that takes the same parameters and then you implement operator= in terms of it (which can improve exception saftety as well if you do it right):

template <std::size_t N> my_class &operator=( T (&t)[N] )
{
    my_class temp( t );
    std::swap( temp. *this );
    return *this;
}


The important thing is getting the size of the array into the function.

Another way of doing this, which is common in the STL, is to specify a range of interators, in this case you could use a range of T pointers:

my_class &operator=( const T *begin, const T * const end )


I'd strongly recommend you grab a copy of "Exceptional C++" by Herb Sutter - it's got loads of guidance on how to write assignment operators.

Cheers,

Ash
Questioninheriting std::vector Pin
VeganFanatic17-Jun-10 8:26
VeganFanatic17-Jun-10 8:26 
AnswerRe: inheriting std::vector Pin
Aescleal17-Jun-10 9:12
Aescleal17-Jun-10 9:12 
GeneralRe: inheriting std::vector Pin
VeganFanatic17-Jun-10 9:15
VeganFanatic17-Jun-10 9:15 
AnswerRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 9:14
professionalChris Losinger17-Jun-10 9:14 
GeneralRe: inheriting std::vector Pin
VeganFanatic17-Jun-10 9:18
VeganFanatic17-Jun-10 9:18 
GeneralRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 9:22
professionalChris Losinger17-Jun-10 9:22 
GeneralRe: inheriting std::vector Pin
VeganFanatic17-Jun-10 9:25
VeganFanatic17-Jun-10 9:25 
GeneralRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 9:28
professionalChris Losinger17-Jun-10 9:28 
GeneralRe: inheriting std::vector Pin
Stephen Hewitt17-Jun-10 14:05
Stephen Hewitt17-Jun-10 14:05 
GeneralRe: inheriting std::vector Pin
Chris Losinger17-Jun-10 16:34
professionalChris Losinger17-Jun-10 16:34 
GeneralRe: inheriting std::vector Pin
Emilio Garavaglia18-Jun-10 3:12
Emilio Garavaglia18-Jun-10 3:12 
GeneralRe: inheriting std::vector Pin
Chris Losinger18-Jun-10 11:17
professionalChris Losinger18-Jun-10 11:17 
Questionscan array of byte Pin
rockago17-Jun-10 8:25
rockago17-Jun-10 8:25 
AnswerRe: scan array of byte Pin
Niklas L17-Jun-10 8:34
Niklas L17-Jun-10 8:34 
GeneralRe: scan array of byte Pin
rockago17-Jun-10 8:48
rockago17-Jun-10 8:48 
GeneralRe: scan array of byte Pin
Niklas L17-Jun-10 9:11
Niklas L17-Jun-10 9:11 
QuestionRe: scan array of byte Pin
David Crow17-Jun-10 9:11
David Crow17-Jun-10 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.