0

I want to define within a class a variable that can be read by any other function but that can be modified only by member function. For example, C# has the Property for this purpose. Traditionally, we've defined a function that returns a private member variable. But i think that does not look sophisticated.

Is there any other way to do this in C++?

netcoder
  • 66,435
  • 19
  • 125
  • 142
  • All the C# property is is a pretty wrapping on the same thing your are talking about with C++. Just make a private (or protected) member variable, provide a public getting and a private/protected setter, and you effectively have exactly what c# is doing. Sophisticated or not. – David Hope Feb 12 '13 at 15:47
  • @DavidHope or no setter at all. – Arne Mertz Feb 12 '13 at 15:50
  • This is *not* a duplicate of the question stated above, since that is about inheritance and this one about C#-style readonly access. – Arne Mertz Feb 14 '13 at 07:55

5 Answers5

1

No. Getter functions (and if necessary Setters as well) are the C++ way to access data members. In general, you make any data member a private variable to ensure encapsulation. You use public data members only if what you are designing is not a real class but a mere "bunch of data" without (much) behavior on its own, in which case it is common to define it as a struct.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
0

Your qualms about returning access to a private member are unwarranted.

class Thing
{
public:
    const std::string& GetString() const { return m_String; } // users can access m_String by calling GetString()

    void SomeRandomFunc() { m_String = "Hello"; } // member functions can write to m_String

private:
    std::string m_String;
};
David
  • 27,652
  • 18
  • 89
  • 138
0

If you write in Visual Studio than there is Property in C++ like C#. But compiler-specific of course. If you ask me, I prefer "get" function

0

there is no such thing as property in C++, even in C# this is a pair of set/get functions. Just some sugar.

But you could do something that will look pretty much the same as property.

Just take a look at this class prototype

    template<class T, class Owner>
class Property {
    friend Owner;
public:
    Property(const T &_v) : m_v(_v) {
    }

    operator T() {
        return m_v;
    }

private:
    T   m_v;

    Property<T,Owner>& operator = (const Property<T,Owner> &v) {
        m_v = v.m_v;
        return *this;
    }

    Property<T,Owner>& operator = (const T& v) {
        m_v = v;
        return *this;
    }
};

And declare in your class public member like

class Test {
public:
    Test() : Data(0) {}

    Property<int, Test> Data;

    void SetData(int data) {
        Data = data;


    }
    };
Ation
  • 731
  • 7
  • 16
  • `operator T()` is non-const, so you cannot read the underlying `int` of `Test::Data` of a const `Test` object. `operator T()` is returning m_v by value, this might cause a performance degradation. Also the template Property does not obey the rule of five. – Werner Henze Feb 12 '13 at 16:07
  • you could add operator T() const. Returning object by value also could be managed. Please don't treat this as a production code, this is just example of an idea. – Ation Feb 12 '13 at 16:28
0
class A
{
private:
    string m_string;

public:
    const string& GetString() const
    {
        return m_string;
    }

    __declspec(property(get=GetString)) string String;
};

A a;
cout << a.String << endl;

Still not as good as in C#, though.

And of course there's C++/CLI properties (for managed classes), which are closer to C# properties:

ref class A
{
private:
    String^ m_theString;

public:
    property String^ TheString
    {
        String^ get()
        {
            return m_theString;
        }
    }
};
user1610015
  • 6,561
  • 2
  • 15
  • 18