-2

New to C++ and I need help understanding what a piece of code does. I was given a header file with a bunch of functions I need to define but I'm a little lost on what one means or how I would even begin to define it.

HERE IT IS:List& operator = (const List& source);

Also if you could show me how I would begin to define it that would really help. Because so far this is how I learned to define functions:

ClassName::FunctionName {
//Code goes here
}

So is this how I would do it?

List::List& {
//Code goes here
}
  • 2
    That's an [operator overloading](http://en.cppreference.com/w/cpp/language/operators). Basically, adding (overloading) the `=` behavior for the class. – wendelbsilva Sep 04 '15 at 20:21
  • 3
    You should spend some time studying C++. Here's [a list of books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Sep 04 '15 at 20:22
  • Operator overloading is a cool thing you will find in a lot of languages. The nice thing is that it add functions to enable conventional notation to something you create (class, struct). For example, if you created a new class and want to sort it, you can overload the `<` operator and use [std::sort](http://en.cppreference.com/w/cpp/algorithm/sort). – wendelbsilva Sep 04 '15 at 20:26

1 Answers1

-1
ClassName::FunctionName {
    //Code goes here
}

is not right. It should be

ReturnType ClassName::FunctionName(Parameters) {
    //Code goes here
}

Now in your example the class name is List, the return type is List&, the function name is operator= and the parameters are const List& source. So

List& List::operator=(const List& source) {
    //Code goes here
}
john
  • 85,011
  • 4
  • 57
  • 81