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:rv'
//
// 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 ----
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:rv'
//
// 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 ----