No properties in c++?

This is a discussion on No properties in c++? within the C++ Programming forums, part of the General Programming Boards category; As far as I can see, c++ does not implement properties... At least not the way c# does?! Code: //in ...

  1. #1
    Registered Viking
    Join Date
    Aug 2006
    Location
    Norway
    Posts
    19

    No properties in c++?

    As far as I can see, c++ does not implement properties... At least not the way c# does?!

    Code:
    //in c# you can define properties like this
    int myProp
    {
      get { return xxx; }
      set { xxx = value; }
    }
    Can you do this in a somewhat similar way in C++?
    The above method does not work...

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    There are some people*who don't know what properties are, so perhaps a brief description would be handy ...
    *Like myself

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    In a true OOP approach, the interface to data members is provided by access labels and member functions, instead. Check the tutorials on classes on this website.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    Quote Originally Posted by twomers
    There are some people*who don't know what properties are, so perhaps a brief description would be handy ...
    A somewhat ugly technique, in my opinion, derived from Visual Basic that manages access to data members.

    get() and set() functions provide read and write access respectively to a class data member. They indirectly end up controlling the class interface. They also alter the syntax needed to access the members from what is common in C++. Once defined a data member is read and written like so,

    int a = myClass.myMember // read

    myClass.myMember = 10 // write

    note the absence of the function call operator.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Ew! But couldn't you just make member functions ... same syntax, but just with () after ... ? Doesn't that break encapsulation or something?
    I've come up with a solution for saying and hearing - learn elvish or something

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    I didn't know C# imported that. You would expect it didn't, since the objective of the language was to provide C++ programmers a more comfortable medium to .Net.

    However it can just happen that C# get and set implementations are more powerful than they were in Visual Basic. And the syntax to call them (they are always called indirectly) may have also been altered to include ().
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In C++, if you make the members public then they work like properties. That is generally considered bad design, though for most classes.

    >> But couldn't you just make member functions ... same syntax, but just with () after ... ? Doesn't that break encapsulation or something?

    You could do something like that if you wanted. Depending on how you implemented it, encapsulation might be affected. Generally you want to be wary of providing a set function or any other type of "write" function for your member variables unless your interface specifically would benefit from the ability to set that value.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by MisterT
    As far as I can see, c++ does not implement properties... At least not the way c# does?!

    Code:
    //in c# you can define properties like this
    int myProp
    {
      get { return xxx; }
      set { xxx = value; }
    }
    Can you do this in a somewhat similar way in C++?
    The above method does not work...
    yes
    Code:
    int class myProp
    {
    public:
      get () { return value; }
      set (int xxx) {value = xxx; }
    protected:
       int value;
    }

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >As far as I can see, c++ does not implement properties... At least not the way c# does?!
    Correct. There are many things that C# has which C++ doesn't, and vice versa. You shouldn't have any expectations when learning a new language.

    >Can you do this in a somewhat similar way in C++?
    Use public member functions to gain access to private data members. C# properties are an elegant way of doing this safely, though it's an extraneous feature that only serves to complicate the language IMO.

    >Doesn't that break encapsulation or something?
    Not at all. In fact, it's identical to using member functions like we would in C++, except with a more convenient interface. Just because the most common description of properties looks like this:
    Code:
    get {
      return data_member;
    }
    
    set {
      data_member = value;
    }
    It doesn't mean that such a simpleminded approach is always, or even usually used. For example, both set and get don't have to be implemented, and their access levels can be different. Also, the block can contain as much code as you'd like to maintain control over how the actual data members are used. This is far from breaking encapsulation, which is more or less giving external users full control over the member, as if it were public.
    My best code is written with the delete key.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Using getters and setters in anything but a small quantity is a symptom of a bad design.

    Whatever fucntion is needing those values should probably be a member of the class where the variables live.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Whatever fucntion is needing those values should probably be a member of the class where the variables live.
    This is another symptom of bad design that promotes monolithic classes. Naturally, any good programmer will refrain from abusing features. Judicious use of getters and setters is just fine, ne?
    My best code is written with the delete key.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    Quote Originally Posted by Prelude
    >Can you do this in a somewhat similar way in C++?
    Use public member functions to gain access to private data members. C# properties are an elegant way of doing this safely, though it's an extraneous feature that only serves to complicate the language IMO.
    One can even mimic the lack of a function call operator with the aid of references:

    Code:
    class myClass {
    private:
        int val;
    
    public:
        myClass(): val(0), value(val) {}
        myClass(int x): val(x), value(val) {}
    
        int &value;
    };
    
    int main() {
    
        myClass test;
        test.value = 12;
        std::cout << test.value;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >One can even mimic the lack of a function call operator with the aid of references:
    Though that does break encapsulation.
    My best code is written with the delete key.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    completely
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    You've broken encapsulation for all of us?b@stArD! You gonna fix it??

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Version info tab in file properties?
    By cpjust in forum Windows Programming
    Replies: 2
    Last Post: 06-03-2008, 03:42 PM
  2. Replies: 0
    Last Post: 04-28-2008, 11:53 PM
  3. keeping a console programs properties
    By h_howee in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2006, 05:38 PM
  4. Inputting data from a table of properties
    By cassius in forum C Programming
    Replies: 1
    Last Post: 06-01-2005, 11:22 AM
  5. mp3 file properties in windows xp
    By JamesMI in forum Tech Board
    Replies: 7
    Last Post: 01-07-2004, 08:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21