Thread: No properties in c++?

  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,273
    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
    Ireland
    Posts
    8,446
    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.
    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
    Ireland
    Posts
    8,446
    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.
    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,273
    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
    Ireland
    Posts
    8,446
    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 ().
    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,366
    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,897
    >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,318
    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,897
    >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
    Ireland
    Posts
    8,446
    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;
    }
    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,897
    >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
    Ireland
    Posts
    8,446
    completely
    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,273
    You've broken encapsulation for all of us?b@stArD! You gonna fix it??

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, 06: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, 09:48 AM