Class member variables only readable by member functions?

This is a discussion on Class member variables only readable by member functions? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to declare a variable in a class, so that the variable is read/write to all class members, ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    245

    Class member variables only readable by member functions?

    Is it possible to declare a variable in a class, so that the variable is read/write to all class members, but read-only to functions which are not part of the class?

    Eg.

    Code:
    class myClass
    {
       int i;
    // blah blah
    }
    
    void myClass::myFunc (void)
    {
       i = 2; // ok, it's writable.
    }
    
    void Otherclass::Otherfunc (void)
    {
       myClass Obj;
       
       Obj.i = 2; // error, variable read-only
    }

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Well, this is not possible in standard C++.
    The only alternative is to use an accessor.
    But, if you are using MSVC or BCC, you can have a special feature that is used basically for COM/OLE or VCL support. It's the declarator __property or the __declspec(property) try a search.
    But there is one restriction: you cannot inline the semi-accessor.
    Well, there is another solution, that is, to play with the declarations, I think it should work if you declare in two separate headers, one for your class implementation and the other for other uses, one specifying const and the other not.
    Or you can make use of the const_cast operator.

    Hope it helped a bit.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb You can use a pointer.

    You can pass a pointer to the first object into the function in the 2nd class. I've forgotten the syntax for a pointer to an object, but I have done it.

    When I first tried this, it seemed like "there had to be an easier way"! But, this is the same as if the first object was a structure. You would pass-in a pointer to the structure.

    The variable needs to be public, or you need public assessor functions.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: Class member variables only readable by member functions?

    Originally posted by _Elixia_
    Is it possible to declare a variable in a class, so that the variable is read/write to all class members, but read-only to functions which are not part of the class?

    Eg.

    Code:
    class myClass
    {
       int i;
    // blah blah
    }
    
    void myClass::myFunc (void)
    {
       i = 2; // ok, it's writable.
    }
    
    void Otherclass::Otherfunc (void)
    {
       myClass Obj;
       
       Obj.i = 2; // error, variable read-only
    }
    If you have class member functions, you should not be using public or protected variables anyway, so you should only be using accessors. Use inline accessor functions if you need speed.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    You raise a very valid point. I was using an array of structures to store information in, but looking back at what I have written, I fail to see why I have done this! There's no reason really as it would be just as easy to call a member function with the variables that would be the assignments, and much more structured.

    [scribbles out the class declaration and goes back to the drawing board....]

    lyx: thanks for your information also. I don't think I'll use the "property" as they are compiler dependant, and differing header files I think would be a nightmare to manage overtime (even if the "const" was just a #define somewhere). However, the const_cast seems a valid way to accomplish the task, and would be portable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

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