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 6136855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:39:16+00:00 2026-05-23T17:39:16+00:00

This is a general question on programming style. Let’s say I have an object

  • 0

This is a general question on programming style. Let’s say I have an object Line which has some methods and private variables Point point_a_ and Point point_b_. Let’s say that at some point I need to change the position of the two points. What programming style would you prefer between the following cases? They all do the same thing (or should do: I didn’t compile, but seems pretty straightforward).

CASE 1

Class Line {
public:
  Line(Point point_a, Point point_b) : point_a_(point_a), point_b_(point_b) {}

  void UpdatePoints(Point point_a, Point point_b) { 
    point_a_ = point_a; point_b_ = point_b;
  }

  double Distance();

private:
  Point point_a_;
  Point point_b_;
};

int main (int argc, char * const argv[]) {
  Point point_a(0,0,0);
  Point point_b(1,1,1);
  Line line(point_a,point_b);
  std::cout<<line.Distance()<<"\n";

  point_a.x = 1;
  line.UpdatePoints(point_a,point_b);
  std::cout<<line.Distance()<<"\n";
}

CASE 2

Class Line {
public:
  Line(Point point_a, Point point_b) : point_a_(point_a), point_b_(point_b) {}
  Point& point_a() { return point_a_; }
  Point& point_b() { return point_b_; }
  double Distance();

private:
  Point point_a_;
  Point point_b_;
};

int main (int argc, char * const argv[]) {
  Point point_a(0,0,0);
  Point point_b(1,1,1);
  Line line(point_a,point_b);
  std::cout<<line.Distance()<<"\n";

  line.point_a().x = 1;
  std::cout<<line.Distance()<<"\n";
}

CASE 3

Class Line {
public:
  Line(Point* point_a, Point* point_b) : point_a_(point_a), point_b_(point_b) {}

  double Distance();

private:
  Point* point_a_;
  Point* point_b_;
};

int main (int argc, char * const argv[]) {
  Point point_a(0,0,0);
  Point point_b(1,1,1);
  Line line(&point_a,&point_b);
  std::cout<<line.Distance()<<"\n";

  point_a.x = 1;
  std::cout<<line.Distance()<<"\n";
}

Any feedback is greatly appreciated!!

Thanks!

[EDIT] Speed is paramount in my software!

  • 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-05-23T17:39:16+00:00Added an answer on May 23, 2026 at 5:39 pm

    In this simple scenario I might just use public member variables.

    Otherwise I would provide getters that return a const reference and matching setters.

    class Line {
    public:
        Line(const Point& p1, const Point&p2) : m_p1(p1), m_p2(p2) {}
    
        const Point& p1() const 
        { return m_p1; }
    
        const Point& p2() const
        { return m_p2; }
    
        void setP1(const Point& p1)
        { m_p1 = p1; }
    
        void setP2(const Point& p2)
        { m_p2 = p2; }
    
    private:
        Point m_p1;
        Point m_p2;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question on programming style and C# language design in general, I'd
This is a general programming question (it doesn't have a specific purpose/application yet). If
I think this question has not to do with programming in general, but nevertheless
This is a general question. What are some techniques you employ to prevent the
Note: This is a general programming question, not specific to any language, but feel
This is more of a maths/general programming question, but I am programming with PHP
Although I'm coding in Objective C, this is more of a general programming question.
I have a general question about this. When you have a gallery, sometimes people
This is a general programming question. I'm learning about C++ and I've learned that
This is a general programming question. What is the best way to make a

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.