Copying value from one vector to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayathri81
    New Member
    • Jul 2012
    • 25

    Copying value from one vector to another

    I have declared two vectors:

    std::vector<Cla ss 1> object1;
    std::vector<Cla ss 2> object2;

    object1 has some value which I want to copy in object2? Is it possible to do?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Not unless there is an assignment in class2 that can assign from class1.

    Or there is a conversion operator in class1 that can convert a class1 object into a class2 object.

    Comment

    • gayathri81
      New Member
      • Jul 2012
      • 25

      #3
      Thank you for your reply. I could not exactly understand what you are telling.

      For example:
      Code:
      std::vector<Geofence> geofences;
      std::vector<Shape> shapes;
      There are some geofences saved in "geofences" . Geofence is a class. Shape is another class that has a formula to calculate distance between 2 points. Polygon is derived from Shape and Shape is base class for it.
      Code:
      std::vector<Shape>::iterator shapeIt = shapes.begin();
      
              for (; shapeIt != shapes.end(); shapeIt++) {
      
                  if (shapeIt->isInside(curPosition.position)) {
                     // push_back(adding new element at end of vector)			            
      } 
              }
      Is this possible practically????
      Last edited by Frinavale; Aug 3 '12, 01:33 PM. Reason: Added code tags.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Yes entirely possible but you have to have provided the right functions so for example

        geofences.push_ back(Geofence(* shapeit));

        Is only going to work if Geofence has a constructor that takes a Shape reference as a parameter, with a prototype something like this

        Code:
        Geofence::Geofence(const Shape& shape)
        {
          // Code to initialise a geofence from a shape
        }
        This is the point weaknessforcats is trying to make, C++ does not provide any conversion implicitly for you since it is 2 different classes you will have to provide a method yourself from copying and/or assigning one to another.

        Comment

        • gayathri81
          New Member
          • Jul 2012
          • 25

          #5
          Thanks for your reply. I am trying it out. Do you mean something like this???
          Code:
          std::vector<Shape> shapes;
          
          
          Geofence::Geofence(const Shape& shapes) 
          { 
            std::vector<Shape>::iterator shapeIt = shapes.begin();
          
            for (; shapeIt != shapes.end(); shapeIt++) {
              if (shapeIt->isInside(curPosition.position)) {
                geofences.push_back(Geofence(*shapeIt));
              } 
            }
          }
          I am completely new to this concept. Please tell me if it is correct.
          Last edited by Frinavale; Aug 3 '12, 01:34 PM. Reason: Added code tags.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            In C++ you write member functions to provide your class behavior.

            Suppose you have two classes A and B and you wish to write this code:

            Code:
            A obja;
            B objb;
            obja = objb;  //assign one object to another
            This code calls the = operator to assign values from a class A object to a class B object. Unless the compiler knows how to do this, your code won't compile. Fortunately, you can write a function that tells the compiler how to make the assignment.

            The function must be called operator= and it must have two arguments. The first argument is the class on the left of the = and the second argument is the class on the right. If this function exists, the compiler will call it and your code will now compile.

            This code might be something like:

            Code:
            A operator=(A left, B right)
            {
                //here you assign members of the right-hand object to members of the left-hand object.
            
                 return left;
            }
            You can overload most operators but you may need to do a little reading to become familiar with the rule of operator overloading.

            Comment

            Working...