459,491 Members | 1,209 Online
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

# defining + and = operator for vector<double>

 P: n/a Hi, I was wondering how to define the + and = operator for a vector of double or float and do I need to define it explicitly? does it not get defined automatically(like copy constructor) if one does not define it explicitly? thanks, --A. Aug 15 '05 #1
4 Replies

 P: n/a Amit wrote: I was wondering how to define the + and = operator for a vector of double or float and do I need to define it explicitly? does it not get defined automatically(like copy constructor) if one does not define it explicitly? The copy assignment operator does get defined automatically. However, you still probably want to define one yourself. Read about "The Rule of Three". The operator+ does not get defined automatically. As to how to define operator+, it's up to you. If you want to be able to add a vector to another vector, then most likely you need vector operator+(vector const& v1, vector const& v2) { if (v1.size() != v2.size()) throw "bad size"; // otherwise do what you need here: create a temporary // vector, add values from v1 and v2 in it and then return it vector temp(v1); // add v2 elements to each of 'temp' return temp; } This should probably be a stand-alone function. V Aug 15 '05 #2

 P: n/a Thanks, Can I define it in my own different namespace though(assuming I am using namepspace std already)? --A. Aug 15 '05 #3

 P: n/a Amit wrote: Thanks, Can I define it in my own different namespace though(assuming I am using namepspace std already)? Most likely not. V Aug 15 '05 #4

 P: n/a Victor Bazarov wrote: Amit wrote: I was wondering how to define the + and = operator for a vector of double or float and do I need to define it explicitly? does it not get defined automatically(like copy constructor) if one does not define it explicitly? The copy assignment operator does get defined automatically. However, you still probably want to define one yourself. Read about "The Rule of Three". The operator+ does not get defined automatically. As to how to define operator+, it's up to you. If you want to be able to add a vector to another vector, then most likely you need vector operator+(vector const& v1, vector const& v2) { if (v1.size() != v2.size()) throw "bad size"; // otherwise do what you need here: create a temporary // vector, add values from v1 and v2 in it and then return it vector temp(v1); // add v2 elements to each of 'temp' return temp; } This should probably be a stand-alone function. V Amit wrote: Thanks, Can I define it in my own different namespace though(assuming I am using namepspace std already)? --A. yes, however, you'd need to use: using namespace your_namespace; or: using your_namespace::operator+; if you wanted to use the operator outside of your namespace. Or, you could type, for example: std::vector result = your_namespace::operator+(v1, v2); however, that kind of defeats the purpose of defining the operator+. Note that you should probably define operator+= too. Aug 15 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.