Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Two C++ snippets for brainstorming (http://www.velocityreviews.com/forums/t806527-two-c-snippets-for-brainstorming.html)

Chariton Karamitas 12-02-2011 03:07 PM

Two C++ snippets for brainstorming
 
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 ----

Victor Bazarov 12-02-2011 04:16 PM

Re: Two C++ snippets for brainstorming
 
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

Juha Nieminen 12-02-2011 04:44 PM

Re: Two C++ snippets for brainstorming
 
Chariton Karamitas <chakaram@auth.gr> 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.

Chariton Karamitas 12-02-2011 04:51 PM

Re: Two C++ snippets for brainstorming
 
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 12-02-2011 05:18 PM

Re: Two C++ snippets for brainstorming
 
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

Paul N 12-02-2011 09:48 PM

Re: Two C++ snippets for brainstorming
 
On Dec 2, 3:07*pm, Chariton Karamitas <chaka...@auth.gr> 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.

John Tsiombikas 12-10-2011 09:57 PM

Re: Two C++ snippets for brainstorming
 
On 2011-12-02, Chariton Karamitas <chakaram@auth.gr> 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/


All times are GMT. The time now is 06:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.