Thread: Protecting object members

  1. #1
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87

    Protecting object members

    Plenty of questions lately!

    Ok, if I have a member of a class that has private scope and is a pointer (for the sake of this question it can be of any type); I have another object that is requesting information from the first object. The only way I can think of to protect the integrity of the private member is to copy to a temporary variable of the same data type and return a copy of it. Is this the most efficient way of doing this?
    Code:
    class foo
    {
    private:
       Someobject* so;
    public:
        ...
       Someobject getData(void);  // Return a copy of the so data member
    };
    
    Someobject foo::getData(void)
    {
      Someobject temp;
    
       // Assume that Someobject has an overloaded operator= defined
       temp = so;
    
      // Return a copy of the member object
      return temp; 
    }
    Visual C++ .net
    Windows XP profesional

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think you should be able to use const in some way to protect it, so that you can only read it, not modify it. I'm not sure though.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Your right, but that would only work in the instance that you do not want to change its value through the use of an appropriate member function. In many cases you may wish to adjust the value or the contents of a private member through a member function, but still be able to return the information contained within (and not have the data inadvertantly changed by manipulating a returned pointer to the data).
    Visual C++ .net
    Windows XP profesional

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Well, if you dont want the data to be modified, I guess you shouldn't return the address of the data. The obvious solution is, as you suggested, to return a copy of the data. But maybe you dont want that?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    It depends on what kind of object you are returning. If someObject is a bitmap, a complete BSP structure or another kind of huge, maybe several megabytes big object, I'd just return a pointer to the object. After all, it's the objects responsibility to keeps it's integrity, not that of it's host.

    However, if you want to allow limited access to the object, you could create a struct in the public scope with pointers to selected methods of the private object.
    Last edited by darksaidin; 08-27-2003 at 06:45 AM.
    [code]

    your code here....

    [/code]

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    PHP Code:
    class C
    {
    private:
        
    TYPE m_Variable;
    public:
        const 
    TYPEgetTYPE() const;  // Read Access
        
    TYPEGetTYPE(); // Read and write access
    };

    const 
    TYPEC::getData() const
    {
      return 
    m_Variable
    }

    TYPEC::getData()
    {
      return 
    m_Variable

    This way you have two accessor functions: a read only function that returns a constant reference, something you cannot use to change your original variable or another function you can use to modify your internal state. If you don't need the second variant, leave it out. Your compiler will pick what function to call dependent on your use.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Excellent - yeah, I reckon that is what I want.

    thanks
    Visual C++ .net
    Windows XP profesional

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    nvoight, in your code, would I be right in saying that the function definitions SHOULD match their declaration? I assume it's a typo.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM
  4. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2002, 12:40 AM