Please consider the code:
#include <iostream>
using namespace std;
extern "C"
void foo( void );
namespace A
{
template< int No >
class Bar
{
private:
friend void ::foo( void );
static void private_func( int n );
};
template< int No >
void Bar< No >::private_func( int n )
{
cout << "A:Bar< " << No << ">::private_func( " << n << " )" << endl;
}
}
extern "C"
void foo( void )
{
A::Bar< 0 >::private_func( 1 );
}
int main( )
{
cout << " ---- " << endl;
foo( );
}
G++ gives:
> g++ -Wall -o extern_c extern_c.cpp
extern_c.cpp: In function ‘void foo()’:
extern_c.cpp:20:7: error: ‘static void A::Bar<No>::private_func(int) [with int No = 0]’ is private
extern_c.cpp:29:31: error: within this context
If I comment the namspace A
, it will compile and run correctly.
What am I missing?
I looked related topics, but could not find any that fits in my problem.
Thanks people.
EDIT:
I am now convinced that extern "C"
has nothing to do with the problem.
Please ignore it.
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this). The second step, use arrow operator -> to access the member function using the pointer to the object.
That makes the difference to whether the access is "by the object". Unless main() is the name of a function of 'Car' I don't understand why you think it's attempt at accessing the width variable qualifies in the statement, "...private members could be accessed by the object of the class to which they belong."
So, is it possible to access private members outside a class without friend? Yes, it is possible using pointers.
C++ namespaces with private members. A cool alternative to the C way of having static global variables. In C++ you can build beautiful namespaces with private members (variables and functions). This is a good choice when you want to hide the inner workings of a set of utility functions from the final user.
Access global variable using 'extern'. By declaring a variable as extern we are able to access the value of global variables in c language. Basically, extern is a keyword in C language that tells to the compiler that definition of a particular variable is exists elsewhere. Here I am declaring x as extern and then the print the value of x.
In C++ you can build beautiful namespaces with private members (variables and functions). This is a good choice when you want to hide the inner workings of a set of utility functions from the final user. You can accomplish that thanks to the concept of anonymous namespaces. An anonymous namespace is a namespace without name, like the following one:
extern linkage for const globals A constglobal variable has internal linkage by default. If you want the variable to have external linkage, apply the externkeyword to the definition, and to all other declarations in other files:
It's a g++ bug. Exists in 4.4, fixed in 4.6.
UPD: It seems that it's triggered by a combination of template
and namespace
. extern "C"
is not relevant, as it may be commented out and the error remains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With