How can I reach Protected Variables?

This is a discussion on How can I reach Protected Variables? within the C++ Programming forums, part of the General Programming Boards category; Hy, I have a protected variable in a classe, in a file 'B' under a folder F. I want to ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    How can I reach Protected Variables?

    Hy,
    I have a protected variable in a classe, in a file 'B' under a folder F.
    I want to reach this variable in a classe in a file 'A' under the same folder F.
    Folder F ------> File 1 : Classe with a protected variable X (.h an .cc file)
    ------> File 2 : Classe where i want to use X (.h and .cc file )

    How can I do it?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    What is the relation between the classes?

    Are they 'friends' or the one inherits from the other?

    If they are not 'friends' and class A is not inherited from class B, you can not access a private or protected member.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    there is no relation, but i want to change their relation to resolve my problem.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    If there is no relation between the two classes, you must declare the variables as public and include the B.h file in A.cpp (to include the header file of the class which variables you want in the implementation file of the class which uses them).

    I still don't understand how do you want two reach these variables. Give me example where you need this variable(s).

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    I must change a big programm.

    There is a variable ACK in class A, in file A.h and incremented in A.cc.
    But i want to use tis counter in B.cc ( and B.h eventually). in file B.

    I can just modify the relation between this two classes, or add something but not modify the hole programm ( too bgi, and too much relations between classes).

    I hope that i'am clear.
    Last edited by zayzay; 07-14-2005 at 04:58 AM.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    If I have understood clear you just have to make class B friend to A:

    Code:
    class A{
    ........
    friend class B;
    };
    I think this is the easiest way to get access to ACK.

    The other way is to write a method of class A, which just returns the value of ACK:
    Code:
    class A{
    .......
    int getACK() {return ACK;}

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Thank you for your help.

    If you have time, this is my problem :

    in file mac-802_11.h ---> class MAC_MIB with counter u_int32_t ACKFailureCount (public).
    ------------------------------------> class Mac802_11 with variable : MAC_MIB macmib_ (protected)

    in file mac-802_11.cc ---> incrementation of counter like that : macmib_ACKFailureCount++.

    Now I want to reach the value of this counter in file cmu-trace.cc --> class CMUTrace

    cmu-trace.cc and .h are in the folder trace
    mac-802_11.cc and .h are in the folder mac

    Thak you so much
    Last edited by zayzay; 07-14-2005 at 06:09 AM.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    I see two ways of doing this:
    1)
    declare CMUTrace as friend of Mac802_1 like this:

    Code:
    class Mac802_1{
    .........
    friend class CMUTrace;
    };
    then in any member function of CMUTrace, which have a pointer or an object of type Mac802_1 you can do that:

    Code:
    CMUTrace::anyFunction(Mac802_1 obj){
    cout<<obj.macmib_.ACKFailureCount;//or whatever you want to do with the counter
    }
    2)
    write a method which returns the value of the counter:
    Code:
    class Mac802_11{
    ...........
    int getACK(){return macmib_.ACKFailureCount;}
    };
    I think this is what you want to do.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Exactly, i'm trying it now
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 05:42 PM
  2. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 05:39 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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