4

Is there a way to bind member function to something like member variable?

Let's say i have simple vector struct:

struct Vec3 {
    int x, y, z;

    Vec2 xy() const { return Vec2(x, y); }
    Vec2 xz() const { return Vec2(x, z); }
    Vec2 yz() const { return Vec2(y, z); }
}

Now i can use it like:

Vec3 t = { 5, 3, 2 };
Vec2 s = t.xy() + t.yz();

But is there a way i could use it like:

Vec3 t = { 5, 3, 2 };
Vec2 s = t.xy; // this here ? execute function without '()'.
3
  • 1
    No, even using a function object you need to use the call operator, which is denoted by parentheses. C# has something like this where using a variable will invoke the getter, C++ AFAIK doesn't.
    – user3920237
    Dec 6, 2014 at 12:53
  • Yea, i tried with #defines but ... that is just really not helpful... Too bad, it would be usefull for some data structures etc. :)
    – RippeR
    Dec 6, 2014 at 12:55
  • 1
    @user3920237 Actually, you are wrong. It can be done by using implicit type conversion as both answers show. C++'s metaprogramming abilities are astounding at times... Aug 3, 2016 at 16:57

2 Answers 2

5

While C++ does not offer properties by default, you can implement them pretty easily by yourself. Here is a simplistic approach:

#include <functional>

template<typename T>
struct property
{
public:
  typedef std::function<T()> getter;
  typedef std::function<void(T)> setter;

public:
  property(getter get, setter set)
  : get_(get)
  , set_(set)
  { }

  operator T() const { return get_(); }
  property& operator=(T x) { set_(x); return *this; }

private:
  getter get_;
  setter set_;
};

We can now rewrite your Vec3 class using these 'properties':

class Vec3
{
public:
  Vec3(int vx, int vy, int vz)
  : x(std::bind(&Vec3::get_x, this), std::bind(&Vec3::set_x, this, std::placeholders::_1))
  , y(std::bind(&Vec3::get_y, this), std::bind(&Vec3::set_y, this, std::placeholders::_1))
  , z(std::bind(&Vec3::get_z, this), std::bind(&Vec3::set_z, this, std::placeholders::_1))
  , xy(std::bind(&Vec3::get_xy, this), std::bind(&Vec3::set_xy, this, std::placeholders::_1))
  , xz(std::bind(&Vec3::get_xz, this), std::bind(&Vec3::set_xz, this, std::placeholders::_1))
  , yz(std::bind(&Vec3::get_yz, this), std::bind(&Vec3::set_yz, this, std::placeholders::_1))
  , x_(vx)
  , y_(vy)
  , z_(vz)
  { }

  property<int> x;
  property<int> y;
  property<int> z;

  property<Vec2> xy;
  property<Vec2> xz;
  property<Vec2> yz;

protected:
  int get_x() { return x_; }
  void set_x(int x) { x_ = x; }

  int get_y() { return y_; }
  void set_y(int y) { y_ = y; }

  int get_z() { return z_; }
  void set_z(int z) { z_ = z; }

  Vec2 get_xy() { return { x_, y_ }; }
  void set_xy(Vec2 xy) { x_ = xy.x; y_ = xy.y; }

  Vec2 get_xz() { return { x_, z_ }; }
  void set_xz(Vec2 xz) { x_ = xz.x; z_ = xz.y; }

  Vec2 get_yz() { return { y_, z_ }; }
  void set_yz(Vec2 yz) { y_ = yz.x; z_ = yz.y; }

private:
  int x_, y_, z_;
};

Which can be used like this:

std::ostream& operator<<(std::ostream& out, const Vec2& v2)
{
  out << '[' << v2.x << ", " << v2.y << ']';
  return out;
}

std::ostream& operator<<(std::ostream& out, const Vec3& v3)
{
  out << '[' << v3.x << ", " << v3.y << ", " << v3.z << ']';
  return out;
}

int main(int argc, char** argv)
{
  Vec3 v3 { 2, 0, 1 };
  std::cout << v3 << std::endl;
  v3.y = 3;
  std::cout << v3.xy << std::endl;
  std::cout << v3.xz << std::endl;
  std::cout << v3.yz << std::endl;

  return 0;
}

As you can see, what you are asking is possible, it just requires a lot of code.

See the live example on ideone

3
  • Well, i must say i'm very impressed byt that. Also thankful for such complex demo. Using casting operator to achieve getter-like functionality - briliant!
    – RippeR
    Dec 7, 2014 at 21:14
  • Btw. if you'd overload operator T&() instead of operator T() then you wouldn't have to implement setters, but copy constructor/assignment operator would be nice ;).
    – RippeR
    Dec 7, 2014 at 21:32
  • This is hideously inefficient and also wrong. You effectively store a pointer to yourself, so the default copy/move operations do the wrong thing - the "properties" of a copy are linked to the original, not the copy.
    – T.C.
    Aug 3, 2016 at 16:49
2

You can do it by adding helper structs with user-defined convertion functions to Vec2, each containing a reference to the parent (Vec3) in order to have access to its members.

A working example (Visual Studio 2015 update 3):

#include <iostream>

struct Vec2 {
    int x, y;
};

struct Vec3 {
    int x, y, z;
    struct XY {
        Vec3& outer;
        XY(Vec3& _outer) : outer {_outer} {};
        operator Vec2() { return Vec2 {outer.x, outer.y}; };
    } xy;
    struct XZ {
        Vec3& outer;
        XZ(Vec3& _outer) : outer {_outer} {};
        operator Vec2() { return Vec2 {outer.x, outer.z}; };
    } xz;
    struct YZ {
        Vec3& outer;
        YZ(Vec3& _outer) : outer {_outer} {};
        operator Vec2() { return Vec2 {outer.y, outer.z}; };
    } yz;
    Vec3(int _x, int _y, int _z) :
        xy {*this}, xz {*this}, yz {*this},
        x {_x}, y {_y}, z {_z} {};
};

int main() {
    Vec3 t {5,3,2};
    Vec2 xy = t.xy; // look, mom, no parentheses!
    Vec2 xz = t.xz; 
    Vec2 yz = t.yz;
    std::cout << xy.x << ", " << xy.y << std::endl;
    std::cout << xz.x << ", " << xz.y << std::endl;
    std::cout << yz.x << ", " << yz.y << std::endl;
}

Output:

5, 3
5, 2
3, 2
1
  • I think it should be mentioned that, while this can be done, it shouldn't be done (except for extremely rare, special circumstances). Programmers have been trained not to expect any fancy processing happening when no parentheses are used, so this trick is in violation of the principle of least astonishment (en.wikipedia.org/wiki/Principle_of_least_astonishment). Aug 3, 2016 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.