How do I overload the [] operator in c++? I have a card deck class where I want to access cards by deck[1], instead of deck.getCard(1).
Note: I have a private vector where I am storing the cards.
Operator overloading in c++?
int operator [] (int ndx)
{
// put anything you want here to accesss
// element "ndx". Just return the value
// of that element as the return value of
// this function.
return value_of_ndx_element_of_your_vector;
}
Reply:Lost me with vectors, but I do a web search for c++ tutorials and most allow you to search their sites for specific answers.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment