1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to change the Private access specifier to Public in Derived Class

Discussion in 'C' started by singhsan02, Sep 22, 2010.

  1. singhsan02

    singhsan02 New Member

    Joined:
    Sep 22, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    Suppose I have a base class with an Integer Variable as defined below.
    Class Base{
    public:
    int a;
    };

    Now I derive privately a derived Class as
    Class Derived : private Base{
    };

    Now int a will become Private in base class.

    My question is how can I again change the access specifier of int a to Public in the derived class?

    Thanks,
    Sanjay
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Code:
    Class Derived : public Base{
    should do it.

    But why do you want to have members as public?
     
  3. singhsan02

    singhsan02 New Member

    Joined:
    Sep 22, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I guess I was not clear what I want to achieve in my question earlier.

    See using Private Inheritance I inherited integer a in to derived class.
    Now the access specifier of int a in derived is Private.I again want to change the access specifier of int a in to say either public or protected in the derived class.
    So the crux is How can I change the access specifier in a class from one type to another?

    ~Sanj
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Now why do you want to be changing the access level of a variable in the middle of your running program?
     
  5. singhsan02

    singhsan02 New Member

    Joined:
    Sep 22, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Right now I don't have a scenario for why I will need it.

    Just wanted to know whether conceptually is it possible or not?

    ~sanj
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    No it is not possible.
     
  7. singhsan02

    singhsan02 New Member

    Joined:
    Sep 22, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Ok..Actually this was asked recently to me in an interview and I believe there is some trick to achieve this behaviour. I searched a lot for this but coudn't get anything.

    Anyways.....
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    No there is no way as far as I know this can be done.
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You should not do it, because if the program design requires that Derived is derived from a private Base then the intent of the design is that Base's members should be private within Derived, so if you need to circumvent that, then your design is broken. If you need Derived::a to be public then (a) derive it as public not private, or (b) add a public accessor function.

    Code:
    class Derived : private Base
    {
    public:
      int *get_a() { return &a; }
    }
    
     
  10. scottu

    scottu New Member

    Joined:
    Dec 14, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    class Derived : private Base {
      public:
        Base::a;
    };
    should do the trick.
     

Share This Page