Thread: Accessing public methods of class members

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Question Accessing public methods of class members

    I am teaching myself C++ as part of a hobby of mine that I am working on. I am also trying to pick up good programming practice as I go along.

    The structure of the problem is as follows. I am constructing an object, call it A, which has a private member object B, which in turn has a private member object C. Each object represent a statistical process which returns a random value, and I am generating derived processes from the base process, which is C.

    Each object has a value once initialised, and the member function to return this value is "CurrentValue()". So I can return the value of A, B or C by A.CurrentValue(), B.CurrentValue() or C.CurrentValue(). The last two can only be done within object A and B respectively.

    The member functions are public (obviously) and the actual values of the process are private. To get the next value in the process, I call a public member of A "GenerateNext()" which calls "GenerateNext()" in B and in turn calls "GenerateNext()" in C. So, at any time, all 3 processes are consistent.

    My actual program only really cares about the value of A, but I want to be able to return the value of B and C easily for debugging and so on. I essentially want to be able to do A.B.CurrentValue(), and A.B.C.CurrentValue(). The only way I can figure out to do this would be to make B and C public rather than private members of the relevant parent class. I figure there must be a way to achieve this that is non-kludgy.

    Any suggestions or pointers would be appreciated.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    (I'm assuming that all A,B,C are objects of different classes. Are they ?)
    Since they represent processes (..can you clarify that more ?..there maybe something wrong with the way you're approaching this...);
    You can declare the abject A as a functor which returns a vector of the values and automatically generate the next value.

    Example: (Put something like this in the public portion of the class)
    Code:
    vector<int> operator()()   //If you do not understand this, read up on operator overloading. 
    {
       vector<int> val;
       val.push_back(CurrentValue());
       val.push_back(B.CurrentValue());
       val.push_back(C.CurrentValue());
       GenerateNext();    B.GenerateNext();     C.GenerateNext();
       return val;
    }
    After that you can call the object like a function and get back the three values.
    vector<int> x = A();
    x[0], x[1], and x[2] gives the three individual values.

    And, you can tweak the fnction fuch that it takes an agrument, depending on which it returns only one value or all the three.
    Last edited by manasij7479; 02-26-2012 at 07:57 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A debugger can easily bypass protection such as thing. Hence you should be able to peak at the values in A, which contains some reference to B, and so on.
    Otherwise, you can configure some debug function that returns a reference to B and C, etc. It goes without saying that it should only exist in a debug build.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Thanks for the replies.

    If it's only for debugging, I could #define private as public, and remove that for non-debug builds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing protected class members
    By Korhedron in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2010, 08:12 AM
  2. Accessing members of a class within a template
    By chadwickstein in forum C++ Programming
    Replies: 10
    Last Post: 10-03-2006, 09:47 AM
  3. Accessing members of parent class
    By Snip in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2006, 01:28 PM
  4. Class members accessing problems
    By HumbuckeR in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2006, 05:29 PM
  5. accessing Class members
    By fizisyen in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2004, 06:18 PM