CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    26

    Using a member accessor in an assignment..

    I'm trying to use member access functions to assign values to them. It's not working with pointers. For example:

    Code:
    class Foo
    {
    
    public:
    Foo();
    void testFunc();
    float* myPointer() { return _myPointer; }
    
    private:
    
    float* _myPointer;
    
    }
    
    void Foo:testFunc()
    {
       
    float arr[] = { 1, 2 , 3, 4 };
    
    myPointer() = &arr[2]; <========= Fails here!!  Why??
    
    }
    Last edited by Shaderhacker; July 25th, 2007 at 06:10 PM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Using a member accessor in an assignment..

    Please use code tags.

    The line:
    Code:
    myPointer() = &arr[2];
    Makes no sence. It doesn't modify anything. It looks like you want a "setter" function in your class:
    Code:
    class Foo
    {
       public
          ...
          void SetPointer(float *pFloat) { _myPointer = pFloat; }
          ...
    };
    Viggy

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Using a member accessor in an assignment..

    Let me expand on that... The function 'myPointer()' doesn't return a reference to anything, it returns the actual value of the variable. So, your statement would would evaluate to:
    Code:
    0x12345678 = 0x98765432
    Viggy

  4. #4
    Join Date
    Jul 2007
    Posts
    26

    Re: Using a member accessor in an assignment..

    Quote Originally Posted by MrViggy
    Let me expand on that... The function 'myPointer()' doesn't return a reference to anything, it returns the actual value of the variable. So, your statement would would evaluate to:
    Code:
    0x12345678 = 0x98765432
    Viggy
    Excellent explanation!

    OK. I guess I *DO* want a reference returned to my pointer. I like using name() as a variable instead of the actual name. So, could I do this:

    Code:
    float& B() { return &b; }
    
    ..
    
    private:
    float* b;
    Then do:
    Code:
    B() = &of_something[3];
    -M
    Last edited by Shaderhacker; July 25th, 2007 at 06:10 PM.

  5. #5
    Join Date
    Jan 2007
    Posts
    94

    Re: Using a member accessor in an assignment..

    One of the core principles behind Object-oriented design is Encapsulation. part of this is not leaking out references to private data members. (implementation details)

    If you're going to do this, you may as well declare 'b' as a public data member... or better still, take a good look at the design of your program, and see what needs to be improved, so that you've got no need to directly play about with class data members. (MrViggy's original suggestion of using a 'set' member function would be the obvious solution, without knowing more details of the problem)

  6. #6
    Join Date
    Jul 2007
    Posts
    26

    Re: Using a member accessor in an assignment..

    Quote Originally Posted by Bench_
    One of the core principles behind Object-oriented design is Encapsulation. part of this is not leaking out references to private data members. (implementation details)

    If you're going to do this, you may as well declare 'b' as a public data member... or better still, take a good look at the design of your program, and see what needs to be improved, so that you've got no need to directly play about with class data members. (MrViggy's original suggestion of using a 'set' member function would be the obvious solution, without knowing more details of the problem)
    OK. I've looked over the code and design more carefully, it seems like the only functions that will access the private class members is the member functions within the class. Having said that, I've removed the accessor.

    -M

  7. #7
    Join Date
    Dec 2002
    Location
    Kiev, Ukraine
    Posts
    61

    Re: Using a member accessor in an assignment..

    for educational purposes:

    Code:
    float *&myPointer() { return _myPointer; }

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

Featured