Two C++ snippets for brainstorming

Discussion in 'C++' started by Chariton Karamitas, Dec 2, 2011.

  1. Hello everyone,

    While practicing on C++, a friend of mine and I came across two
    interesting cases, one regarding static functions and another regarding
    friend functions. Our actual questions are given in the comments of the
    C++ snippets given below. Help is appreciated :)


    --- snip ---
    // C++ test #1
    #include <iostream>

    using namespace std;

    class hello {
    private:
    int prv;
    public:
    hello(void);
    static void static_fun(void);
    ~hello(void);
    };

    hello::hello(void) {
    cout << "Hello, World!" << endl;
    return;
    }

    void hello::static_fun(void) {
    // For static functions there's no 'this'.
    //
    // Test #1:
    // this->prv = 0;
    // error C2355: 'this' : can only be referenced inside non-static
    member functions
    //
    // This is ok, static functions cannot access non-static
    // functions or variables.
    //
    // Test #2:
    // prv = 0;
    // error C2597: illegal reference to non-static member 'hello::prv'
    //
    // Okay, trying to access a non-static member from a static
    // function should cause such an error even if we're trying
    // to access it without a reference to 'this'.
    //

    // Test #3: Compiles successfully!
    hello h;
    h.prv = 0;
    // QUESTION: Accessing a private member of another 'hello' instance!
    // What's going on here? This is valid in both VC and GCC.
    return;
    }

    hello::~hello(void) {
    cout << "Bye Bye, World!" << endl;
    return;
    }

    int main() {
    hello h;
    h.static_fun();
    return 0;
    }
    --- snip ---

    --- snip ---
    // C++ test #2
    #include <iostream>

    using namespace std;

    class B;

    class A {
    protected:
    int A_protected;
    private:
    int A_private;
    public:
    int A_public;

    friend void A_friend(B *);
    };


    class B: public A {
    protected:
    int B_protected;
    private:
    int B_private;
    public:
    int B_public;
    };


    void A_friend(B *b) {
    // QUESTION:
    // If B inherits A as public, the expression [1] compiles
    // and runs correctly.
    //
    // If B inherits A as private, [1] throws a compilation
    // error.
    //
    // Common sense is that private members of class A,
    // should not be accessible from an instance of class B
    // no matter what the inheritance type is!
    //
    // What's going on here? In what way does the inheritance
    // type affect the friend function?

    cout << b->A_private << endl; // [1]
    return;
    }

    int main(int argc, char *argv[]) {
    return 0;
    }
    --- snip ----
     
    Chariton Karamitas, Dec 2, 2011
    #1
    1. Advertising

  2. On 12/2/2011 10:07 AM, Chariton Karamitas wrote:
    > While practicing on C++, a friend of mine and I came across two
    > interesting cases, one regarding static functions and another regarding
    > friend functions. Our actual questions are given in the comments of the
    > C++ snippets given below. Help is appreciated :)
    >


    Homeworks need to be done by the people to whom they are assigned. See
    FAQ 5.2.

    Read about access rights (what is "private", for instance?) and
    friendship (especially pay attention how friendship is inherited).

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 2, 2011
    #2
    1. Advertising

  3. Chariton Karamitas <> wrote:
    > // Test #3: Compiles successfully!
    > hello h;
    > h.prv = 0;
    > // QUESTION: Accessing a private member of another 'hello' instance!
    > // What's going on here? This is valid in both VC and GCC.


    A class has access rights to objects of its own type, obviously. How
    would you implement, for example, a copy constructor or an assignment
    operator otherwise? It would be impossible. It would make no sense.
     
    Juha Nieminen, Dec 2, 2011
    #3
  4. Victor Bazarov wrote:

    > Homeworks need to be done by the people to whom they are assigned. See
    > FAQ 5.2.


    Honestly, I don't know how you reached the conclusion that my questions
    are related to any kind of homework.

    > Read about access rights (what is "private", for instance?) and
    > friendship (especially pay attention how friendship is inherited).


    Your answers indicate that you didn't pay much attention to my
    questions, instead you rushed to the conclusion above. I'm not a C++
    expert, but I think my questions are not related to elementary concepts
    like the meaning of "private" or the fact that friendship is not inherited.

    Thank you anyway
    ../ck
     
    Chariton Karamitas, Dec 2, 2011
    #4
  5. Juha Nieminen wrote:

    > A class has access rights to objects of its own type, obviously. How
    > would you implement, for example, a copy constructor or an assignment
    > operator otherwise? It would be impossible. It would make no sense.


    Your examples were enlightening, thank you. If I compare my example to a
    copy constructor or an assignment operator, it makes more sense.

    ../ck
     
    Chariton Karamitas, Dec 2, 2011
    #5
  6. Chariton Karamitas

    Paul N Guest

    On Dec 2, 3:07 pm, Chariton Karamitas <> wrote:
    > Hello everyone,
    >
    > While practicing on C++, a friend of mine and I came across two
    > interesting cases, one regarding static functions and another regarding
    > friend functions. Our actual questions are given in the comments of the
    > C++ snippets given below. Help is appreciated :)
    >
    > --- snip ---
    > // C++ test #1
    > #include <iostream>
    >
    > using namespace std;
    >
    > class hello {
    >    private:
    >      int prv;
    >    public:
    >      hello(void);
    >      static void static_fun(void);
    >      ~hello(void);
    >
    > };
    >
    > hello::hello(void) {
    >    cout << "Hello, World!" << endl;
    >    return;
    >
    > }
    >
    > void hello::static_fun(void) {
    >    // For static functions there's no 'this'.
    >    //
    >    // Test #1:
    >    //   this->prv = 0;
    >    //   error C2355: 'this' : can only be referenced inside non-static
    > member functions
    >    //
    >    //   This is ok, static functions cannot access non-static
    >    //   functions or variables.
    >    //
    >    // Test #2:
    >    //   prv = 0;
    >    //   error C2597: illegal reference to non-static member 'hello::prv'
    >    //
    >    //   Okay, trying to access a non-static member from a static
    >    //   function should cause such an error even if we're trying
    >    //   to access it without a reference to 'this'.
    >    //


    I think you may be misunderstanding what is going on here. It is not
    the case that a static function does not have access to non-static
    members. Rather, the point about a static function is that it does not
    relate to a particular object when it runs. You could call static_fun
    even if no actual hello objects have been created. The reason you
    can't access the prv member of the object is because there is no "the"
    object, not because you are banned from looking at it.

    >    // Test #3: Compiles successfully!
    >    hello h;
    >    h.prv = 0;
    >    // QUESTION: Accessing a private member of another 'hello' instance!
    >    // What's going on here? This is valid in both VC and GCC.


    Here, h is an object, and it has a prv member, and you can read it,
    because the function is a member function and so can access the
    private members of any object of the same class.

    Hope this helps.
    Paul.
     
    Paul N, Dec 2, 2011
    #6
  7. On 2011-12-02, Chariton Karamitas <> wrote:
    > // C++ test #1


    Since this has been answered by others I'll skip it.

    > // C++ test #2
    > #include <iostream>
    >
    > using namespace std;
    >
    > class B;
    >
    > class A {
    > protected:
    > int A_protected;
    > private:
    > int A_private;
    > public:
    > int A_public;
    >
    > friend void A_friend(B *);
    > };
    >
    >
    > class B: public A {
    > protected:
    > int B_protected;
    > private:
    > int B_private;
    > public:
    > int B_public;
    > };
    >
    >
    > void A_friend(B *b) {
    > // QUESTION:
    > // If B inherits A as public, the expression [1] compiles
    > // and runs correctly.
    > //
    > // If B inherits A as private, [1] throws a compilation
    > // error.
    > //
    > // Common sense is that private members of class A,
    > // should not be accessible from an instance of class B
    > // no matter what the inheritance type is!
    > //
    > // What's going on here? In what way does the inheritance
    > // type affect the friend function?
    >
    > cout << b->A_private << endl; // [1]
    > return;
    > }
    >
    > int main(int argc, char *argv[]) {
    > return 0;
    > }


    This is valid because b can be implicitly converted to a A*.

    Chapter and verse: 11.4: Member access control
    (from the C++ standard draft: 2 dec 1996. I don't have the final version
    of the standard nor the newer C++11 standard but I think this'll do just
    fine).

    A member m is accessible when named in class N if
    - m as a member of N is public, or
    - m as a member of N is private or protected, and the reference occurs
    in a member or friend of class N, or
    - there exists a base class B of N that is accessible at the point of
    reference, and m is accessible when named in class B. [Example:

    class B;
    class A {
    private:
    int i;
    friend void f(B*);
    };
    class B : public A { };
    void f(B* p) {
    p->i = 1; // Okay: B* can be implicitly cast to A*,
    // and f has access to i in A
    -end example]

    --
    John Tsiombikas
    http://nuclear.mutantstargoat.com/
     
    John Tsiombikas, Dec 10, 2011
    #7
    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. - Steve -

    Reusing Code Snippets

    - Steve -, Sep 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    430
    Greg Burns
    Oct 1, 2004
  2. Victor
    Replies:
    1
    Views:
    368
    Markus Wankus
    Jan 13, 2004
  3. David Vaughan
    Replies:
    3
    Views:
    247
    Scott David Daniels
    Aug 22, 2004
  4. Wolfgang Nádasi-Donner
    Replies:
    8
    Views:
    169
    Wolfgang Nádasi-Donner
    Jul 31, 2005
  5. Mr. Bill
    Replies:
    2
    Views:
    96
    Mr. Bill
    Jan 17, 2011
Loading...

Share This Page