public fields or properties?

Alright so I'm trying to creating something that is sort of like properties in C# for my classes in C++.

for example. in C# i would do this:
int MaxHP { get; set; }

or, with backing field:
1
2
3
4
5
int MaxHP 
{ 
    get { return maxHP; }
    set { maxHP = value; }
}


However so far with C++, I've only been able to replicate this with:
1
2
3
4
5
6
7
8
9
10
private:	
    int maxHP;	
    int maxEP;
public:
    int GetMaxHP() { return maxHP; }
    void UpSetMaxHP(int value){ maxHP += value; }
    void DownSetMaxHP(int value){ maxHP -= value; }
    int GetMaxEP(){ return maxEP; }
    void UpSetMaxEP(int value){ maxEP += value; }
    void DownSetMaxEP(int value){ maxEP -= value; }


I must be missing something in the way things should be designed. In C# the property would be access like a field. however in C++ I have to do functions which work differently when accessed from other objects.

I guess i could do:
1
2
public:
    int MaxHP;

But that feels like I am sort of defeating the purpose.
Maybe wrap it in a struct?
1
2
3
4
5
6
7
8
9
10
11
private:
    int maxHP

...

public:
    struct MaxHP
    {
        get(){return maxHP;}
        set(int val){maxHP = val;}
    }
closed account (zb0S216C)
@naraku9333: That won't work because "???::MaxHP" will not have access to "???'s" members unless it had a "???" object to reference. However, you could change it a little:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Something
  {
    private:
      struct MaxHP
        {
          friend class Something;

          private:
            int _M_Value_;

          public:
            int Query_Value( ) const /* noexcept */ { return( _M_Value_ ); }
        } _M_MaxHP_;

    public:
      void Print( ) const /* noexcept */
        {
          std::cout << _M_MaxHP_._M_Value_;
        }
  };

We're using "friend" here to prevent setters and to tighten encapsulation between "Something" and "Something::MaxHP".

On a side note, I don't think this design is the way to go.

Wazzak
Last edited on
@naraku9333:
The way i currently have it would be more effective then this. Simply because I need to be able to modify the value of the "character" object without overwritting it.

In C# I would have my other object access that data using the properties like so:
1
2
hero.MaxHP += 5; // when adding a magical item.
hero.MaxHP -= 5; // when removing same item. 

However, so far, to do the same thing in C++, I've been stuck with the examples above in my original post.

@Framework:
I like your example as it help me learn something new, however, for what I wish to do, I would sooner just use public fields. Simply because I have a lot of these fields which have to be accessed and modified by other objects and I don't want to complicate my code too much. However every book I've read and studied, always discourage using public fields like so. Which causes my dilemma.
Last edited on
So I got an answer from someone on Stack Overflow which actually works great for this. So figured I'd share it.

1
2
public:
    int& MaxHP { return maxHP; }

returns a reference which can easily be manipulated from the other object. In my current project it works just like C# properties would.
Last edited on
If your going to do that you might as well make the variable public.
> I have a lot of these fields which have to be accessed and modified by other objects
┬┐what about letting the one that has the information, to handle the data?
1
2
3
4
5
hero.MaxHP += 5; // when adding a magical item.
hero.MaxHP -= 5; // when removing same item. 

//or simply
hero.add_item( ring );



> We're using "friend" here to prevent setters and to tighten encapsulation
The inner class is private...
closed account (zb0S216C)
Yes, I know it's private; I don't know how the OP's class is meant to function, so I assumed private. Regardless of level of access, it's still tightens encapsulation.

Wazzak
If your going to do that you might as well make the variable public.

Not at all. This cant be access directly, you have to create a temp int& in the other object to manipulable it. Also That's just my original dilemma, properties in C# make things as if they were public, but they aren't, they still have a private backing field. However your right and I agree that you might as well use a public field, but every book I've studied, say not to do so. However, properties, or that function I just showed, both gives the ability to add more code that can alter things. While the public field is just a field.

I was not planning on doing this originally, but I've considered what I could do with the above exemple;
1
2
3
4
5
public:
    int& MaxHP 
    {
        if (maxHP < 990) return maxHP 
    }


@ne555:
I guess that would be another great way of doing it. However with my code already made a certain way, I don't know if i want to change it now. Though I might, as I could probably have more functionality.


so I assumed private.

Good assumption. :)
Last edited on
Topic archived. No new replies allowed.