Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ global extern "C" friend can't reach private member on namespaced class

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.

  • C++: namespace conflict between extern "C" and class member
  • Why this friend function can't access a private member of the class?

Thanks people.


EDIT:

I am now convinced that extern "C" has nothing to do with the problem. Please ignore it.

like image 703
j4x Avatar asked Jul 25 '11 19:07

j4x


People also ask

How can you access private members outside the class?

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.

How can private members be accessed using pointers in c++?

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.

Can object access private members?

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

Can We access private data members of a class without using a member or a friend function?

So, is it possible to access private members outside a class without friend? Yes, it is possible using pointers.

What is a private member in C++?

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.

How to access global variable using 'extern' in C language?

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.

What are anonymous namespaces in C++?

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:

How do you add extern linkage to a const global variable?

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:


1 Answers

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.

like image 197
n. 1.8e9-where's-my-share m. Avatar answered Sep 28 '22 02:09

n. 1.8e9-where's-my-share m.