Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:33:26+00:00 2026-06-07T15:33:26+00:00

Consider a point class, with declarations like: // Point.h class Point { public: Point();

  • 0

Consider a point class, with declarations like:

// Point.h
class Point {
    public:
        Point();                           // Default constructor
        Point(const Point & p);            // Copy constructor
        Point & operator=(Point source);   // Copy assignment operator, needs implemented
        ~Point();                          // Destructor
    // ...
    private:
        double m_x;                        // X coordinate
        double m_y;                        // Y coordinate
};

For the homework, the only thing I have left to implement is the copy assignment operator.
The canonical answer for how to do this is the copy-and-swap idiom.

Using the swap function for copy assignment solves one problem and creates another (how to implement the swap function).

While I don’t feel a need to provide a swap function, I wouldn’t know how best to implement swap anyway. Is it to specialize std::swap? I know about neither namespaces nor template specialization yet.

Formally, my question is two-fold:

  1. How should copy assignment be implemented? If it uses a swap function, how should I implement that?
  2. In the wild, would I simply not write the Big Three members, as the Point class is simply two numbers? Will the compiler write the operations correctly and optimally?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:33:28+00:00Added an answer on June 7, 2026 at 3:33 pm
    1. Copy-and-swap is best practice. If you don’t want to override std::swap because you’re unfamiliar with templates, then you can also write a swap_points function or a Point::swap(Point &other) member function.

      The implementation of that (member) function is very simple: just call std::swap on all of the members.

    2. Yes, implementing the Big Three yourself for this class is really an academic exercise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)
Consider the two classes: public class Point { public int x; public int y;
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Consider the following code: [Serializable] public class Human { public string Name { get;
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
Consider I have the following classes: class User { [Key] public Guid Id {get;
Consider the following code: public class Foo { private static final Object LOCK =
Say I've got a very lightweight object: public class Point { public int x;
Consider the code: class UPDServer { //start listener public void start() { UdpClient listener
Consider the following set of classes/Interfaces: class IFish{ public: virtual void eat() = 0;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.