Skip to content

Instantly share code, notes, and snippets.

@JayBazuzi
Last active August 12, 2017 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayBazuzi/b657f62d7bcb4633871d313bb7d25758 to your computer and use it in GitHub Desktop.
Save JayBazuzi/b657f62d7bcb4633871d313bb7d25758 to your computer and use it in GitHub Desktop.
C++ value type styles
class Size
{
int m_width;
int m_height;
public:
Size(int width, int height)
: m_width(width)
, m_height(height)
{}
int GetWidth() const { return m_width; }
int GetHeight() const { return m_height; }
void SetWidth(int width) { m_width = width; }
void SetHeight(int height) { return m_height = height; }
};
class Size
{
int width;
int height;
public:
Size(int width, int height)
: width(width)
, height(height)
{}
int Width() const { return this->width; }
int Height() const { return this->height; }
void SetWidth(int width) { this->width = width;}
void SetHeight(int height) { this->height = height;}
};
struct Size
{
int width;
int height;
};
@ericgu
Copy link

ericgu commented Aug 12, 2017

The struct.

I am not a fan of empty properties. Anders and I talked about this in the early days of C#, and he was of the same opinion, so I started to suggest that to people that maybe that made more sense than writing properties.

And then I stopped doing it because people kept looking at me as if I had grown an extra arm.

It wasn't helped by all the BCL features that supported properties but not fields.

My only exception is if I am shipping a library and I'm going to need binary compatibility when I version it, but I don't think that applies to to value types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment