c++ - Private inheritance: name lookup error -


i have following code example doesn't compile:

#include <stdio.h>  namespace {     class base1     { // line 6     };      class base2: private base1     {     };      class derived: private base2     {     public:         // following function wants print pointer, nothing else!         void print(base1* pointer) {printf("%p\n", pointer);}     }; } 

the error gcc prints is:

test.cpp:6: error: `class my::base1' inaccessible

test.cpp:17: error: within context

now, can guess problem is: when looking @ declaration of print, compiler sees base1 , thinks: base1 base-class subobject of derived* this, don't have access it! while intend base1 should type name.

how can see in c++ standard correct behavior, , not bug in compiler (i sure it's not bug; compilers checked behaved so)?

how should fix error? following fixes work, 1 should choose?

void print(class base1* pointer) {}

void print(::my:: base1* pointer) {}

class base1; void print(base1* pointer) {}


edit:

int main() {     my::base1 object1;     my::derived object3;     object3.print(&object1); } 

the section you're looking 11.1. suggests using ::my::base1* work around this:

[ note: in derived class, lookup of base class name find injected-class-name instead of name of base class in scope in declared. injected-class-name might less accessible name of base class in scope in declared. — end note ]

[ example: class { }; class b : private { }; class c : public b { *p; // error: injected-class-name inaccessible :: * q ; // ok }; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -