Inaccessible base class despite friendshipUnexpected inaccessible base (first derived class)Calling the base constructor in C#How to call a parent class function from derived class function?How to access a data class's private member variable from another derived class whose parent class is a friend class of the data class?Why does C++ not allow inherited friendship?Python class inherits objectC++ inheritance - inaccessible base?c++ friend inheritanceDo I really have a car in my garage?c# new in object declaration when inherit and overrideUnexpected inaccessible base (first derived class)

What can I do with a research project that is my university’s intellectual property?

Is this proposal by U.S. presidential candidate Pete Buttigieg to change the composition of the Supreme Court constitutional?

Interaction between Leyline of Anticipation and Teferi, Time Raveler

How does DC work with natural 20?

.NET executes a SQL query and Active Monitor shows multiple rows blocking each other

Helping ease my back pain when I'm studying 13 hours everyday, even weekends

How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?

Can Ogre clerics use Purify Food and Drink on humanoid characters?

How long would it take to cross the Channel in 1890's?

How can I politely work my way around not liking coffee or beer when it comes to professional networking?

Why does the Saturn V have standalone inter-stage rings?

Prime sieve in Python

Understanding the reasoning of the woman who agreed with King Solomon to "cut the baby in half"

Can White Castle?

Drawing people along with x and y axis

How large would a mega structure have to be to host 1 billion people indefinitely?

What happens to Cessna electric flaps that are moving when power is lost?

Trainee keeps missing deadlines for independent learning

LWC - Best practice for routing?

How to model a twisted cylinder like this

Should developer taking test phones home or put in office?

Java TreeMap.floorKey() equivalent for std::map

Do I have to explain the mechanical superiority of the player-character within the fiction of the game?

What did River say when she woke from her proto-comatose state?



Inaccessible base class despite friendship


Unexpected inaccessible base (first derived class)Calling the base constructor in C#How to call a parent class function from derived class function?How to access a data class's private member variable from another derived class whose parent class is a friend class of the data class?Why does C++ not allow inherited friendship?Python class inherits objectC++ inheritance - inaccessible base?c++ friend inheritanceDo I really have a car in my garage?c# new in object declaration when inherit and overrideUnexpected inaccessible base (first derived class)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








6















There are tons of questions regarding this error and all of the answers seem to imply that downcasting is impossible. Only this answer mentions friendship as possible solution, at least as I understand it. However the following code (irrelevant stuff removed for clarity) does not compile:



class C;

class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo(A* a) ;
;

B b;
C c;

void bar()

c.foo(&b); // this produces error: class A is an inaccessible base of B



Why friendship does not work on a reference? After all, "C" is perfectly capable of calling protected methods of "A" through pointer to "B".



The full error is



prog.cc: In function 'void bar()':
prog.cc:20:13: error: 'A' is an inaccessible base of 'B'
20 | c.foo(&b);









share|improve this question









New contributor



Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • I am indeed dead wrong. Shot my mouth off too soon and spent the last few minutes looking for confirmation, something I should have done first. Anyone have a sword I can throw myself on?

    – user4581301
    8 hours ago


















6















There are tons of questions regarding this error and all of the answers seem to imply that downcasting is impossible. Only this answer mentions friendship as possible solution, at least as I understand it. However the following code (irrelevant stuff removed for clarity) does not compile:



class C;

class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo(A* a) ;
;

B b;
C c;

void bar()

c.foo(&b); // this produces error: class A is an inaccessible base of B



Why friendship does not work on a reference? After all, "C" is perfectly capable of calling protected methods of "A" through pointer to "B".



The full error is



prog.cc: In function 'void bar()':
prog.cc:20:13: error: 'A' is an inaccessible base of 'B'
20 | c.foo(&b);









share|improve this question









New contributor



Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • I am indeed dead wrong. Shot my mouth off too soon and spent the last few minutes looking for confirmation, something I should have done first. Anyone have a sword I can throw myself on?

    – user4581301
    8 hours ago














6












6








6








There are tons of questions regarding this error and all of the answers seem to imply that downcasting is impossible. Only this answer mentions friendship as possible solution, at least as I understand it. However the following code (irrelevant stuff removed for clarity) does not compile:



class C;

class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo(A* a) ;
;

B b;
C c;

void bar()

c.foo(&b); // this produces error: class A is an inaccessible base of B



Why friendship does not work on a reference? After all, "C" is perfectly capable of calling protected methods of "A" through pointer to "B".



The full error is



prog.cc: In function 'void bar()':
prog.cc:20:13: error: 'A' is an inaccessible base of 'B'
20 | c.foo(&b);









share|improve this question









New contributor



Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











There are tons of questions regarding this error and all of the answers seem to imply that downcasting is impossible. Only this answer mentions friendship as possible solution, at least as I understand it. However the following code (irrelevant stuff removed for clarity) does not compile:



class C;

class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo(A* a) ;
;

B b;
C c;

void bar()

c.foo(&b); // this produces error: class A is an inaccessible base of B



Why friendship does not work on a reference? After all, "C" is perfectly capable of calling protected methods of "A" through pointer to "B".



The full error is



prog.cc: In function 'void bar()':
prog.cc:20:13: error: 'A' is an inaccessible base of 'B'
20 | c.foo(&b);






c++ inheritance friend






share|improve this question









New contributor



Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 7 hours ago







Maple













New contributor



Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









MapleMaple

1336




1336




New contributor



Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Maple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • I am indeed dead wrong. Shot my mouth off too soon and spent the last few minutes looking for confirmation, something I should have done first. Anyone have a sword I can throw myself on?

    – user4581301
    8 hours ago


















  • I am indeed dead wrong. Shot my mouth off too soon and spent the last few minutes looking for confirmation, something I should have done first. Anyone have a sword I can throw myself on?

    – user4581301
    8 hours ago

















I am indeed dead wrong. Shot my mouth off too soon and spent the last few minutes looking for confirmation, something I should have done first. Anyone have a sword I can throw myself on?

– user4581301
8 hours ago






I am indeed dead wrong. Shot my mouth off too soon and spent the last few minutes looking for confirmation, something I should have done first. Anyone have a sword I can throw myself on?

– user4581301
8 hours ago













3 Answers
3






active

oldest

votes


















8














The problem is that the conversion from B* to A* (the one which requires friendship) does not happen in a member function of C, but in the context of the code containing b and c (presumably main or an unrelated function).



It would work fine if you created a member function in C accepting a B* , and then called foo from within it. That would have the conversion happen within the context of C which has the necessary access rights (thanks to friendship).






share|improve this answer

























  • Oh, interesting! So, what you are saying, it is not enough for calling context to have an access to class C and class C be a friend of A. It is also necessary for a calling context to be a friend of A as well, right?

    – Maple
    8 hours ago











  • @Maple It's the caller of foo who does the conversion from B* to A*, which requires the protected access.

    – Angew
    8 hours ago











  • @Angew made sure the calling context is a friend of "A" and it works! thanks a lot for the explanation!

    – Maple
    7 hours ago


















6














Your code is equivalent to this:



B b;
C c;
A * a = &b; // <- This cast produces the error
c.foo(a);


You cannot cast &b as A* since the base class is protected, regardless of the friendship of C.






share|improve this answer

























  • Upvoted this answer because it's concise and more clear

    – Tony J
    7 hours ago











  • I upvoted this also. I've accepted Angew's answer since it was the first that switched a light bulb in my head.

    – Maple
    7 hours ago


















1














In the global scope B is not visible as A, because of protected inheritance.



Only class B itself, classes inherited from B and class C (because the friendship relation) "know" that B is inheriting A. But the rest of the world (including global scope) doesnt'.



So to achieve what you want, you could call



c.foo(&b)


within C scope, for example using some wrapper function, something like (although bad design decision):



#include <iostream>
#include <cstdlib>

class C;


class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo()
B b;
foo(&b); // this call is OK within C-scope

private:
void foo(A* /*a*/)
std::cout << "C::foo(A* a)n";
;
;


int main()

std::cout << "Hello, Wandbox!" << std::endl;
B b;
C c;
//c.foo(&b); // this produces error: class A is an inaccessible base of B
c.foo(); // this is calling c.foo(A*) internally



or live:






share|improve this answer

























  • @formerlyknownas_463035818 You are correct, the placement of a call turns out to be important and should have been included in the example. Ironically, this became obvious to me only after I got an answer. I updated the code in the question.

    – Maple
    7 hours ago











  • @Maple nothing personal, but i dislike the term "obvious". nothing is really obvious ;) still foo is not accesible but at least your example now has the error you report. If you dont mind I'll add the error message to your quesiton

    – formerlyknownas_463035818
    7 hours ago











  • @formerlyknownas_463035818 I returned that "public" back into code to avoid any confusion and focus on actual problem

    – Maple
    7 hours ago











  • @Maple yep. Note that changeing the code after you got an answer is actually not optimal either because it can break answers. Luckily this answer still applies with minial change (global scope -> bar scope). I know it isnt easy to create a good mvce, but leaving out stuff is confusing rather than simplifying most of the time

    – formerlyknownas_463035818
    7 hours ago












  • Hm – if we just consider accessibility, the answer is fine. Having a separate B instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodified b might be used. Possibly having two public foo overloads, one accepting A*, one accepting B* and the latter one just doing the cast as needed for calling the former might be less intervening...

    – Aconcagua
    4 hours ago













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Maple is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56673706%2finaccessible-base-class-despite-friendship%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














The problem is that the conversion from B* to A* (the one which requires friendship) does not happen in a member function of C, but in the context of the code containing b and c (presumably main or an unrelated function).



It would work fine if you created a member function in C accepting a B* , and then called foo from within it. That would have the conversion happen within the context of C which has the necessary access rights (thanks to friendship).






share|improve this answer

























  • Oh, interesting! So, what you are saying, it is not enough for calling context to have an access to class C and class C be a friend of A. It is also necessary for a calling context to be a friend of A as well, right?

    – Maple
    8 hours ago











  • @Maple It's the caller of foo who does the conversion from B* to A*, which requires the protected access.

    – Angew
    8 hours ago











  • @Angew made sure the calling context is a friend of "A" and it works! thanks a lot for the explanation!

    – Maple
    7 hours ago















8














The problem is that the conversion from B* to A* (the one which requires friendship) does not happen in a member function of C, but in the context of the code containing b and c (presumably main or an unrelated function).



It would work fine if you created a member function in C accepting a B* , and then called foo from within it. That would have the conversion happen within the context of C which has the necessary access rights (thanks to friendship).






share|improve this answer

























  • Oh, interesting! So, what you are saying, it is not enough for calling context to have an access to class C and class C be a friend of A. It is also necessary for a calling context to be a friend of A as well, right?

    – Maple
    8 hours ago











  • @Maple It's the caller of foo who does the conversion from B* to A*, which requires the protected access.

    – Angew
    8 hours ago











  • @Angew made sure the calling context is a friend of "A" and it works! thanks a lot for the explanation!

    – Maple
    7 hours ago













8












8








8







The problem is that the conversion from B* to A* (the one which requires friendship) does not happen in a member function of C, but in the context of the code containing b and c (presumably main or an unrelated function).



It would work fine if you created a member function in C accepting a B* , and then called foo from within it. That would have the conversion happen within the context of C which has the necessary access rights (thanks to friendship).






share|improve this answer















The problem is that the conversion from B* to A* (the one which requires friendship) does not happen in a member function of C, but in the context of the code containing b and c (presumably main or an unrelated function).



It would work fine if you created a member function in C accepting a B* , and then called foo from within it. That would have the conversion happen within the context of C which has the necessary access rights (thanks to friendship).







share|improve this answer














share|improve this answer



share|improve this answer








edited 8 hours ago

























answered 8 hours ago









AngewAngew

138k11272362




138k11272362












  • Oh, interesting! So, what you are saying, it is not enough for calling context to have an access to class C and class C be a friend of A. It is also necessary for a calling context to be a friend of A as well, right?

    – Maple
    8 hours ago











  • @Maple It's the caller of foo who does the conversion from B* to A*, which requires the protected access.

    – Angew
    8 hours ago











  • @Angew made sure the calling context is a friend of "A" and it works! thanks a lot for the explanation!

    – Maple
    7 hours ago

















  • Oh, interesting! So, what you are saying, it is not enough for calling context to have an access to class C and class C be a friend of A. It is also necessary for a calling context to be a friend of A as well, right?

    – Maple
    8 hours ago











  • @Maple It's the caller of foo who does the conversion from B* to A*, which requires the protected access.

    – Angew
    8 hours ago











  • @Angew made sure the calling context is a friend of "A" and it works! thanks a lot for the explanation!

    – Maple
    7 hours ago
















Oh, interesting! So, what you are saying, it is not enough for calling context to have an access to class C and class C be a friend of A. It is also necessary for a calling context to be a friend of A as well, right?

– Maple
8 hours ago





Oh, interesting! So, what you are saying, it is not enough for calling context to have an access to class C and class C be a friend of A. It is also necessary for a calling context to be a friend of A as well, right?

– Maple
8 hours ago













@Maple It's the caller of foo who does the conversion from B* to A*, which requires the protected access.

– Angew
8 hours ago





@Maple It's the caller of foo who does the conversion from B* to A*, which requires the protected access.

– Angew
8 hours ago













@Angew made sure the calling context is a friend of "A" and it works! thanks a lot for the explanation!

– Maple
7 hours ago





@Angew made sure the calling context is a friend of "A" and it works! thanks a lot for the explanation!

– Maple
7 hours ago













6














Your code is equivalent to this:



B b;
C c;
A * a = &b; // <- This cast produces the error
c.foo(a);


You cannot cast &b as A* since the base class is protected, regardless of the friendship of C.






share|improve this answer

























  • Upvoted this answer because it's concise and more clear

    – Tony J
    7 hours ago











  • I upvoted this also. I've accepted Angew's answer since it was the first that switched a light bulb in my head.

    – Maple
    7 hours ago















6














Your code is equivalent to this:



B b;
C c;
A * a = &b; // <- This cast produces the error
c.foo(a);


You cannot cast &b as A* since the base class is protected, regardless of the friendship of C.






share|improve this answer

























  • Upvoted this answer because it's concise and more clear

    – Tony J
    7 hours ago











  • I upvoted this also. I've accepted Angew's answer since it was the first that switched a light bulb in my head.

    – Maple
    7 hours ago













6












6








6







Your code is equivalent to this:



B b;
C c;
A * a = &b; // <- This cast produces the error
c.foo(a);


You cannot cast &b as A* since the base class is protected, regardless of the friendship of C.






share|improve this answer















Your code is equivalent to this:



B b;
C c;
A * a = &b; // <- This cast produces the error
c.foo(a);


You cannot cast &b as A* since the base class is protected, regardless of the friendship of C.







share|improve this answer














share|improve this answer



share|improve this answer








edited 8 hours ago

























answered 8 hours ago









Gilles-Philippe PailléGilles-Philippe Paillé

1,5171212




1,5171212












  • Upvoted this answer because it's concise and more clear

    – Tony J
    7 hours ago











  • I upvoted this also. I've accepted Angew's answer since it was the first that switched a light bulb in my head.

    – Maple
    7 hours ago

















  • Upvoted this answer because it's concise and more clear

    – Tony J
    7 hours ago











  • I upvoted this also. I've accepted Angew's answer since it was the first that switched a light bulb in my head.

    – Maple
    7 hours ago
















Upvoted this answer because it's concise and more clear

– Tony J
7 hours ago





Upvoted this answer because it's concise and more clear

– Tony J
7 hours ago













I upvoted this also. I've accepted Angew's answer since it was the first that switched a light bulb in my head.

– Maple
7 hours ago





I upvoted this also. I've accepted Angew's answer since it was the first that switched a light bulb in my head.

– Maple
7 hours ago











1














In the global scope B is not visible as A, because of protected inheritance.



Only class B itself, classes inherited from B and class C (because the friendship relation) "know" that B is inheriting A. But the rest of the world (including global scope) doesnt'.



So to achieve what you want, you could call



c.foo(&b)


within C scope, for example using some wrapper function, something like (although bad design decision):



#include <iostream>
#include <cstdlib>

class C;


class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo()
B b;
foo(&b); // this call is OK within C-scope

private:
void foo(A* /*a*/)
std::cout << "C::foo(A* a)n";
;
;


int main()

std::cout << "Hello, Wandbox!" << std::endl;
B b;
C c;
//c.foo(&b); // this produces error: class A is an inaccessible base of B
c.foo(); // this is calling c.foo(A*) internally



or live:






share|improve this answer

























  • @formerlyknownas_463035818 You are correct, the placement of a call turns out to be important and should have been included in the example. Ironically, this became obvious to me only after I got an answer. I updated the code in the question.

    – Maple
    7 hours ago











  • @Maple nothing personal, but i dislike the term "obvious". nothing is really obvious ;) still foo is not accesible but at least your example now has the error you report. If you dont mind I'll add the error message to your quesiton

    – formerlyknownas_463035818
    7 hours ago











  • @formerlyknownas_463035818 I returned that "public" back into code to avoid any confusion and focus on actual problem

    – Maple
    7 hours ago











  • @Maple yep. Note that changeing the code after you got an answer is actually not optimal either because it can break answers. Luckily this answer still applies with minial change (global scope -> bar scope). I know it isnt easy to create a good mvce, but leaving out stuff is confusing rather than simplifying most of the time

    – formerlyknownas_463035818
    7 hours ago












  • Hm – if we just consider accessibility, the answer is fine. Having a separate B instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodified b might be used. Possibly having two public foo overloads, one accepting A*, one accepting B* and the latter one just doing the cast as needed for calling the former might be less intervening...

    – Aconcagua
    4 hours ago















1














In the global scope B is not visible as A, because of protected inheritance.



Only class B itself, classes inherited from B and class C (because the friendship relation) "know" that B is inheriting A. But the rest of the world (including global scope) doesnt'.



So to achieve what you want, you could call



c.foo(&b)


within C scope, for example using some wrapper function, something like (although bad design decision):



#include <iostream>
#include <cstdlib>

class C;


class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo()
B b;
foo(&b); // this call is OK within C-scope

private:
void foo(A* /*a*/)
std::cout << "C::foo(A* a)n";
;
;


int main()

std::cout << "Hello, Wandbox!" << std::endl;
B b;
C c;
//c.foo(&b); // this produces error: class A is an inaccessible base of B
c.foo(); // this is calling c.foo(A*) internally



or live:






share|improve this answer

























  • @formerlyknownas_463035818 You are correct, the placement of a call turns out to be important and should have been included in the example. Ironically, this became obvious to me only after I got an answer. I updated the code in the question.

    – Maple
    7 hours ago











  • @Maple nothing personal, but i dislike the term "obvious". nothing is really obvious ;) still foo is not accesible but at least your example now has the error you report. If you dont mind I'll add the error message to your quesiton

    – formerlyknownas_463035818
    7 hours ago











  • @formerlyknownas_463035818 I returned that "public" back into code to avoid any confusion and focus on actual problem

    – Maple
    7 hours ago











  • @Maple yep. Note that changeing the code after you got an answer is actually not optimal either because it can break answers. Luckily this answer still applies with minial change (global scope -> bar scope). I know it isnt easy to create a good mvce, but leaving out stuff is confusing rather than simplifying most of the time

    – formerlyknownas_463035818
    7 hours ago












  • Hm – if we just consider accessibility, the answer is fine. Having a separate B instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodified b might be used. Possibly having two public foo overloads, one accepting A*, one accepting B* and the latter one just doing the cast as needed for calling the former might be less intervening...

    – Aconcagua
    4 hours ago













1












1








1







In the global scope B is not visible as A, because of protected inheritance.



Only class B itself, classes inherited from B and class C (because the friendship relation) "know" that B is inheriting A. But the rest of the world (including global scope) doesnt'.



So to achieve what you want, you could call



c.foo(&b)


within C scope, for example using some wrapper function, something like (although bad design decision):



#include <iostream>
#include <cstdlib>

class C;


class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo()
B b;
foo(&b); // this call is OK within C-scope

private:
void foo(A* /*a*/)
std::cout << "C::foo(A* a)n";
;
;


int main()

std::cout << "Hello, Wandbox!" << std::endl;
B b;
C c;
//c.foo(&b); // this produces error: class A is an inaccessible base of B
c.foo(); // this is calling c.foo(A*) internally



or live:






share|improve this answer















In the global scope B is not visible as A, because of protected inheritance.



Only class B itself, classes inherited from B and class C (because the friendship relation) "know" that B is inheriting A. But the rest of the world (including global scope) doesnt'.



So to achieve what you want, you could call



c.foo(&b)


within C scope, for example using some wrapper function, something like (although bad design decision):



#include <iostream>
#include <cstdlib>

class C;


class A
friend class C; // this does not help
;

class B : protected A
friend class C; // this does not help either
;

class C
public:
void foo()
B b;
foo(&b); // this call is OK within C-scope

private:
void foo(A* /*a*/)
std::cout << "C::foo(A* a)n";
;
;


int main()

std::cout << "Hello, Wandbox!" << std::endl;
B b;
C c;
//c.foo(&b); // this produces error: class A is an inaccessible base of B
c.foo(); // this is calling c.foo(A*) internally



or live:







share|improve this answer














share|improve this answer



share|improve this answer








edited 7 hours ago

























answered 8 hours ago









StPiereStPiere

1,233816




1,233816












  • @formerlyknownas_463035818 You are correct, the placement of a call turns out to be important and should have been included in the example. Ironically, this became obvious to me only after I got an answer. I updated the code in the question.

    – Maple
    7 hours ago











  • @Maple nothing personal, but i dislike the term "obvious". nothing is really obvious ;) still foo is not accesible but at least your example now has the error you report. If you dont mind I'll add the error message to your quesiton

    – formerlyknownas_463035818
    7 hours ago











  • @formerlyknownas_463035818 I returned that "public" back into code to avoid any confusion and focus on actual problem

    – Maple
    7 hours ago











  • @Maple yep. Note that changeing the code after you got an answer is actually not optimal either because it can break answers. Luckily this answer still applies with minial change (global scope -> bar scope). I know it isnt easy to create a good mvce, but leaving out stuff is confusing rather than simplifying most of the time

    – formerlyknownas_463035818
    7 hours ago












  • Hm – if we just consider accessibility, the answer is fine. Having a separate B instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodified b might be used. Possibly having two public foo overloads, one accepting A*, one accepting B* and the latter one just doing the cast as needed for calling the former might be less intervening...

    – Aconcagua
    4 hours ago

















  • @formerlyknownas_463035818 You are correct, the placement of a call turns out to be important and should have been included in the example. Ironically, this became obvious to me only after I got an answer. I updated the code in the question.

    – Maple
    7 hours ago











  • @Maple nothing personal, but i dislike the term "obvious". nothing is really obvious ;) still foo is not accesible but at least your example now has the error you report. If you dont mind I'll add the error message to your quesiton

    – formerlyknownas_463035818
    7 hours ago











  • @formerlyknownas_463035818 I returned that "public" back into code to avoid any confusion and focus on actual problem

    – Maple
    7 hours ago











  • @Maple yep. Note that changeing the code after you got an answer is actually not optimal either because it can break answers. Luckily this answer still applies with minial change (global scope -> bar scope). I know it isnt easy to create a good mvce, but leaving out stuff is confusing rather than simplifying most of the time

    – formerlyknownas_463035818
    7 hours ago












  • Hm – if we just consider accessibility, the answer is fine. Having a separate B instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodified b might be used. Possibly having two public foo overloads, one accepting A*, one accepting B* and the latter one just doing the cast as needed for calling the former might be less intervening...

    – Aconcagua
    4 hours ago
















@formerlyknownas_463035818 You are correct, the placement of a call turns out to be important and should have been included in the example. Ironically, this became obvious to me only after I got an answer. I updated the code in the question.

– Maple
7 hours ago





@formerlyknownas_463035818 You are correct, the placement of a call turns out to be important and should have been included in the example. Ironically, this became obvious to me only after I got an answer. I updated the code in the question.

– Maple
7 hours ago













@Maple nothing personal, but i dislike the term "obvious". nothing is really obvious ;) still foo is not accesible but at least your example now has the error you report. If you dont mind I'll add the error message to your quesiton

– formerlyknownas_463035818
7 hours ago





@Maple nothing personal, but i dislike the term "obvious". nothing is really obvious ;) still foo is not accesible but at least your example now has the error you report. If you dont mind I'll add the error message to your quesiton

– formerlyknownas_463035818
7 hours ago













@formerlyknownas_463035818 I returned that "public" back into code to avoid any confusion and focus on actual problem

– Maple
7 hours ago





@formerlyknownas_463035818 I returned that "public" back into code to avoid any confusion and focus on actual problem

– Maple
7 hours ago













@Maple yep. Note that changeing the code after you got an answer is actually not optimal either because it can break answers. Luckily this answer still applies with minial change (global scope -> bar scope). I know it isnt easy to create a good mvce, but leaving out stuff is confusing rather than simplifying most of the time

– formerlyknownas_463035818
7 hours ago






@Maple yep. Note that changeing the code after you got an answer is actually not optimal either because it can break answers. Luckily this answer still applies with minial change (global scope -> bar scope). I know it isnt easy to create a good mvce, but leaving out stuff is confusing rather than simplifying most of the time

– formerlyknownas_463035818
7 hours ago














Hm – if we just consider accessibility, the answer is fine. Having a separate B instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodified b might be used. Possibly having two public foo overloads, one accepting A*, one accepting B* and the latter one just doing the cast as needed for calling the former might be less intervening...

– Aconcagua
4 hours ago





Hm – if we just consider accessibility, the answer is fine. Having a separate B instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodified b might be used. Possibly having two public foo overloads, one accepting A*, one accepting B* and the latter one just doing the cast as needed for calling the former might be less intervening...

– Aconcagua
4 hours ago










Maple is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Maple is a new contributor. Be nice, and check out our Code of Conduct.












Maple is a new contributor. Be nice, and check out our Code of Conduct.











Maple is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56673706%2finaccessible-base-class-despite-friendship%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу