Two C++ snippets for brainstorming

  • Thread starter Chariton Karamitas
  • Start date
C

Chariton Karamitas

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 ----
 
V

Victor Bazarov

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
 
J

Juha Nieminen

Chariton Karamitas said:
// 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.
 
C

Chariton Karamitas

Victor said:
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
 
C

Chariton Karamitas

Juha said:
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
 
P

Paul N

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.
 
J

John Tsiombikas

// 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]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top