Results 1 to 7 of 7

Thread: get{} set{} equivalents

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    get{} set{} equivalents

    What are the get{} set{} accessor equivalents in C++?

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    You have to write your own, unless you're writing managed c++.

    Code:
    protected:
         int m;
    public:
         int GetM() const;
         void SetM(int newM);
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I don't understand the syntax of putting const after the function. Could you explain that and give an example of how the user would access and change the variable?

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Consider:
    Code:
    class a
    {
    public:
      int func() const;
      int func2();
    }:
    When the const keyword appears at the end of the function declaration in a class, it means that calling that function will not modify any of the data in that class. Only members specified with the const keyword like this can be called if the object is marked as const:
    Code:
    int x(const a& blah)
    {
      blah.func(); // legal, func() is const
      blah.func2() // illegal, is NOT const
      return 0;
    };
    Z.

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Is there really any point in doing things that way? The function itself controls what it does not any variables sent to it.

    Well anyway, in VS .net, how does the designer know if you make your own control what variables are properties of the class?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The notion of properties does not exist in C++. Classes have member variables and member functions. A variable might be public, then you can simply access it. Or it might be private/protected, then you need access functions. This is similar to properties, but there is no built-in mechanism for properties.

    There is one really good reason for this. Consider this code:

    obj.member.member.member.member.member = 4;

    How many function calls are there? In C++, you know it nearly exactly. Zero calls, except if the last member is of an object type that has overloaded the = operator, but you would probably know that.

    In C#, who can say? How many of those members are just variables and how many are properties which imply a call to the getter?

    Since performance is an important part of C++, it's good to know what happens.


    About the const.
    It does make sense. The const keyword is an important way to avoid bugs that come from accidental modification of data. Using const you can tell the compiler that this variable must not be modified. Using const the compiler can create temporary objects that would be dangerous or useless otherwise. Conside this code that passes a string to a function.
    Code:
    // The functions to be called.
    void func1(string s);
    void func2(string &s);
    void func3(const string &s);
    
    // Variable for passing.
    string st("hello");
    
    // Passing:
    func1(st); // Valid. A copy of st is created and passed by value.
    // Problem: st gets copied. A large object is passed by value.
    func2(st); // Valid. A reference to st is passed.
    // Problem: st can be modified. This might not be what you intend.
    func3(st); // Valid. A constant reference to st is passed.
    // This avoids both problems.
    func1("hello"); // Valid. A temporary object is constructed and passed by value.
    // Problem: passing by value again.
    func2("hello"); // Invalid. You may not pass a reference to a temporary object.
    // Problem: does not compile.
    func3("hello"); // Valid. A temporary object is created and a constant reference is passed.
    // Again avoids both problems.
    As you see, func3 (using const) is the perfect solution. But now we need to rely that the string really isn't modified. How to ensure that? The methods called might modify the internal variables anyway.
    The solution are const methods.
    How do they work?
    Let me take you further back
    The first C++ compilers were actually preprocessors which translated the C++ code to C code. A class would be compiled like this:
    Input
    Code:
    class MyClass
    {
      int myvar;
      int othervar;
    
    public:
      MyClass() : myvar(3), othervar(3) {}
    
      void dostuff(int param) {
        myvar = param;
      }
    };
    
    int main()
    {
      MyClass obj;
      obj.dostuff(29);
    
      return 0;
    }
    Output
    Code:
    struct MyClass
    {
      int myvar;
      int othervar;
    };
    
    _MyClass_constr_v(struct MyClass *this)
    {
      this->myvar = 3;
      this->othervar = 3;
    }
    
    void _MyClass_dostuff_v_i(struct MyClass *this, int param)
    {
      this->myvar = param;
    }
    
    int main()
    {
      struct MyClass obj;
      _MyClass_constr_v(&obj);
      _MyClass_dostuff_v_i(&obj, 29);
    
      return 0;
    }
    Such a compiler would translate a const method like this:
    Code:
    class MyClass { ...
      int getmy() const {
        return myvar;
      }
    };
    Code:
    int _MyClass_getmy_c_i_v(const struct MyClass *this)
    {
      return this->myvar;
    }
    What does that mean? It means that if getmy attempted to change any value it would fail because the this pointer is constant. It means another thing:
    Code:
    void foo(const MyClass &ref)
    {
      ref.dostuff(32);
    }
    Code:
    void foo_v_cr_MyClass(const struct MyClass *ref)
    {
      _MyClass_dostuff_v_i(ref, 32); // compile error, no implicit
      // conversion from "const struct MyClass *" to "struct MyClass *".
    }

    Any questions left?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    No. Thank you CornedBee.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width