Can a sub-class (composite object) member function access private or protected members of base class

Discussion in 'C++' started by pkpatil@gmail.com, Jun 9, 2006.

  1. Guest

    Hi,

    Can a private composite object in a class access private or protected
    members of base class?
    For e.g.

    class composite {
    void memberFunction();
    };

    class main {
    private:
    int a;
    composite comp;
    protected:
    int b;
    };

    Above, can composite::memberFunction() access main::a and main::b of
    main class "main"? If so, how?
     
    , Jun 9, 2006
    #1
    1. Advertising

  2. wrote:
    > Can a private composite object in a class access private or protected
    > members of base class?
    > For e.g.
    >
    > class composite {
    > void memberFunction();
    > };
    >
    > class main {
    > private:
    > int a;
    > composite comp;
    > protected:
    > int b;
    > };
    >
    > Above, can composite::memberFunction() access main::a and main::b of
    > main class "main"? If so, how?


    No, it cannot. The relationship exists between types and functions, not
    between objects and classes or between objects and objects. In your case,
    the 'composite::memberFunction' has no special access rights to class
    'main'. To give the right to that function you need to declare it 'friend'
    to 'main' class.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 9, 2006
    #2
    1. Advertising

  3. Noah Roberts Guest

    wrote:
    > Hi,
    >
    > Can a private composite object in a class access private or protected
    > members of base class?


    There is no 'base class' in your example. Base classes are the class
    that a derived class derives from. It has to do with inheritance, not
    /composition/ as you show below.

    > For e.g.
    >
    > class composite {
    > void memberFunction();
    > };
    >
    > class main {
    > private:
    > int a;
    > composite comp;
    > protected:
    > int b;
    > };
     
    Noah Roberts, Jun 9, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CoolPint
    Replies:
    8
    Views:
    1,000
    Jeff Schwab
    Dec 14, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    815
    Micah Cowan
    Jan 12, 2005
  3. blangela
    Replies:
    8
    Views:
    674
    Erik Wikström
    Sep 26, 2008
  4. Albright
    Replies:
    3
    Views:
    451
    Alf P. Steinbach
    Feb 17, 2009
  5. K. Frank
    Replies:
    8
    Views:
    659
    K. Frank
    Mar 22, 2012
Loading...

Share This Page