-2

I'm trying to write a Swap function to swap 2 different types of objects of an array in C++. How can I do that? (I prefer using pointer to solve this problem)

I've tried to use template classes to swap 2 objects because the types of object are different from others and when looping through the array, I don't know which objects that one element belongs to.

I have 3 classes (Object_A, Object_B and Object_C) as below:

class Object_A : public base_class 
{

private:
    int workingTime;
    int relaxTime;
    float salary;
public:
    Object_A();
    virtual ~Object_A();
    float Cal_Salary();

    int getWorkingTime() const;

    void setWorkingTime(int workingTime);

    int getRelaxTime() const;

    void setRelaxTime(int relaxTime);

    float getSalary() const;

    void setSalary(float salary);
};

class Object_B : public base_class 
{

private:
    float income;
public:
    Officer();
    virtual ~Officer();

    float getIncome() const;

    void setIncome(float income);
};

class Object_C : public base_class 
{

private:
    std::string role;
    float roleCoef;
    float bonus;

public:
    Manager();
    virtual ~Manager();
    float Cal_Bonus();

    const std::string& getRole() const;

    void setRole(const std::string& role);

    float getRoleCoef() const;

    void setRoleCoef(float roleCoef);

    float getBonus() const;

    void setBonus(float bonus);
};

// I tried to write SwapValues func
template<class T, class U>
void SwapValues(T* a, U* b)
{
    T temp = a;
    a = b;
    b = temp;
}

I have an array with base_class type to store some elements of three objects above (Object_A, Object_B and Object_C). However when I want to swap one element of Object_A to one of Object_C with SwapValues() func, it doesn't work!

Thanks a lot for your help.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Lucas
  • 1
  • 7
  • `**template` ?! – Oblivion Jun 23 '19 at 09:40
  • _"it doesn't work!"_ Isn't a concise problem description. Post a [mcve] including all inputs, outputs and verbatim error messages as required her pleas. – πάντα ῥεῖ Jun 23 '19 at 09:40
  • 1
    @oblivion OP probably wanted to highlight the code. – πάντα ῥεῖ Jun 23 '19 at 09:41
  • you need to provide some of the classes you used as well: https://godbolt.org/z/MJIqwY – Oblivion Jun 23 '19 at 09:43
  • why don't you write your swap function to work with base_class ? – OznOg Jun 23 '19 at 09:47
  • @OznOg yep, I agree with you that I should write my swap function to work with only base_class because all classes are extended base_class. Actually, I wrote it before but it still doesn't work as I expec so I've tried this one :'( – Lucas Jun 23 '19 at 09:54
  • Your array can't possibly contain value types of both `Object_B` and `Object_C`. I assume your array contains `*base_class` elements, *not* `base_class`? – Nikos C. Jun 23 '19 at 10:00
  • @NikosC. you're right – Lucas Jun 23 '19 at 10:27
  • If an array can contain objects of different types, then the elements of the array are presumably pointers in some guise (e.g. pointers to a common base class, smart pointers to a common base class, etc). All you need to do is swap the pointers. Trying to implement a swap of two objects of actual different types is only possible, in general, if you reconstruct the objects, since (for example) they may contain different data and have different sizes. – Peter Jun 23 '19 at 11:16
  • @Peter I see, thank you so much – Lucas Jun 23 '19 at 13:35

3 Answers3

1

EDIT: Re-reading your question, the reason why your pointerswap doesn't work is because you are not passing the pointers as reference. If you insist on calling the function SwapValues, you can implement it like this:

class base_class {
  // ...

  friend void SwapValues(base_class*& a, base_class*& b) {
    ::std::swap(a,b);
  }
};

Observe that SwapValues is still free function, not a member function.


You should always use ::std::swap as it sometimes offers efficient alternatives:

::std::swap(*a,*b);

You can also implement a member function in your class if you want to customise how swap works. For instance, your members can be swapped efficiently, as they are either POD or ::std::string which has a constant-time swap.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • Mind the two-step. – Deduplicator Jun 23 '19 at 09:50
  • @Deduplicator: Come again? – bitmask Jun 23 '19 at 09:51
  • C++20 will introduce [customization point objects (CPO)](https://stackoverflow.com/questions/53495848/what-are-customization-point-objects-and-how-to-use-them), but until then using a qualified name for calling `swap()` suppresses the custom implementations. The two-step `using std::swap; swap(*a, *b);` or the alternative `std::iter_swap(a, b);` are the way to go until then. Admittedly, for pointers it doesn't hurt. – Deduplicator Jun 23 '19 at 09:55
  • @Deduplicator: Ah, interesting. I'm not sure if the edit already addresses this. – bitmask Jun 23 '19 at 10:00
0

I think you should try

void SwapValues(T* a, U* b)
{
    T* temp = a;
    a = b;
    b = temp;
}
Mahi
  • 332
  • 3
  • 11
0

Since your array consists of pointers (base_class* elements), you don't need your own swap function. You can just assign such pointers normally, and thus you can just use std::swap.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96