defining + and = operator for vector<double>

Discussion in 'C++' started by Amit, Aug 15, 2005.

  1. Amit

    Amit Guest

    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.
    Amit, Aug 15, 2005
    #1
    1. Advertising

  2. 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<double> operator+(vector<double> const& v1,
    vector<double> 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<double> temp(v1);
    // add v2 elements to each of 'temp'
    return temp;
    }

    This should probably be a stand-alone function.

    V
    Victor Bazarov, Aug 15, 2005
    #2
    1. Advertising

  3. Amit

    Amit Guest

    Thanks,
    Can I define it in my own different namespace though(assuming I am
    using namepspace std already)?

    --A.
    Amit, Aug 15, 2005
    #3
  4. Amit wrote:
    > Thanks,
    > Can I define it in my own different namespace though(assuming I am
    > using namepspace std already)?


    Most likely not.

    V
    Victor Bazarov, Aug 15, 2005
    #4
  5. Amit

    Alipha Guest

    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<double> operator+(vector<double> const& v1,
    > vector<double> 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<double> 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::eek:perator+;

    if you wanted to use the operator outside of your namespace. Or, you
    could type, for example:

    std::vector<double> result = your_namespace::eek:perator+(v1, v2);

    however, that kind of defeats the purpose of defining the operator+.
    Note that you should probably define operator+= too.
    Alipha, Aug 15, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    402
    Peter Koch Larsen
    Jul 2, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,453
    Victor Bazarov
    Feb 17, 2005
  3. Ingo Nolden
    Replies:
    15
    Views:
    1,511
    Jerry Coffin
    Apr 30, 2005
  4. Replies:
    8
    Views:
    1,894
    Csaba
    Feb 18, 2006
  5. J.M.
    Replies:
    5
    Views:
    772
Loading...

Share This Page