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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:46:53+00:00 2026-05-29T09:46:53+00:00

My goal is to fire a event receiver when a value is changed in

  • 0

My goal is to fire a event receiver when a value is changed in a class. My solution to this is using get and set functions. However, I do not want any performance problems when reading the values. In the class below:

class Vertex {
 public:
  const double& GetX() { return _x; } // should inline automatically
  const double& GetY() { return _y; } // ' '                     ' '
  void SetX(const double& x); // complex stuff in source file
  void SetY(const double& y); // ' '                      ' '
 private:
  double _x, _y;
}

I put the definition of the getters in the header files, because this should allow them to be inlined.

My question is: is there any way to automatically call a function when a value is changed that does not require me to include function definitions in the header file? Or is there perhaps a visibility level that allows anyone to read the data, but only the class itself to modify it? Or maybe something else?

I know an alternative approach would be manually calling an update function, but that looks ugly:

vertex.X = 5;
vertex.Update();

Thank you in advance.

Edit:

So far I’ve heard some great answers. The solution depends on your goal:

  • Easy: getters and setters; not very clean though.
  • Clean: property class (or proxy); implementing operators is not as easy though.

However, what about efficiency?

I just worked out a nice trick for the separate read/write-access permissions I speculated about earlier:

class Vertex {
 public:
  Vertex(double x, double y)
      : _x(x), _y(y), X(_x), Y(_y) { } // defined here for readability
  void SetX(double x) { _x = x; update(); } // defined here for readability
  void SetY(double y) { _y = y; update(); } // update would be the callback
  const double& X, Y;
 private:
  double _x, _y;
}
  • 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-29T09:46:54+00:00Added an answer on May 29, 2026 at 9:46 am

    If all you’re looking for is clean, then probably the cleanest way to do this would be to have a “property” object, like in C#:

    template<typename T>
    class Property {
    public:
        Property(std::function<void(T)> callback) : data(), callback(callback) { }
    
        Property& operator=(const T& newvalue) {
            data = newvalue;
            callback(data);
    
            return *this;
        }
    
        operator T() const {
            return data;
        }
    
    private:
        T data;
        std::function<void(T)> callback;
    };
    
    class Vertex {
    public:
        Vertex() : X(&xcallback), Y(&ycallback) { }
    
        Property<double> X, Y;
    };
    

    (The functions are inline for conciseness on SO, you can move them out if you want.) Then you can do

    Vertex v;
    
    v.X = 4;
    v.Y = 2.3443;
    

    And every time the value is assigned to, the respective callback will be called. And you can use v.X and v.Y in place of wherever you’d use a double as well so they behave like normal doubles (except for the fact that you’d have to implement -=, +=, etc).

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

Sidebar

Related Questions

How can i get/set the goal value? So that i can also use it
From what I understand using .stop() will prevent the callback functions to fire, right?
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
Goal: to programmatically determine the sizes (in bytes) of the fields of a class.
Goal: order records by 'start_date' select first record get the id of the record
Goal: Get values which are unique in $resultSecond So I have two objects. $result
Goal I have a generic class GenericClass<T> and I want to pool instances. I'm
Possible Duplicate: In JavaScript can I make a “click” event fire programmatically for a
This is a bit of subjective question about a specific situation. Main goal for
Goal: To render a google map using geolocation. I am trying to implement the

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.