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)

Employer wants to use my work email account after I quit

Is "Busen" just the area between the breasts?

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

Is it damaging to turn off a small fridge for two days every week?

Can there be an UN resolution to remove a country from the UNSC?

What is the legal status of travelling with methadone in your carry-on?

What is the origin of Scooby-Doo's name?

Output of "$OSTYPE:6" on old releases of Mac OS X

How to get Current Module and Full Action Name in ajax controller

How many children?

How does DC work with natural 20?

Trainee keeps missing deadlines for independent learning

Can someone suggest a path to study Mordell-Weil theorem for someone studying on their own?

Why is prior to creation called holy?

Why did pressing the joystick button spit out keypresses?

Does this Wild Magic result affect the sorcerer or just other creatures?

How to draw this center trajectory of rolling ball?

Is it illegal to withhold someone's passport and green card in California?

Why do all the teams that I have worked with always finish a sprint without completion of all the stories?

How is hair tissue mineral analysis performed?

How many people are necessary to maintain modern civilisation?

How much will studying magic in an academy cost?

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

Prime sieve in Python



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
    7 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
    7 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
    7 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
    7 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
7 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
7 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
    7 hours ago











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

    – Angew
    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


















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
    7 hours ago











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

    – Angew
    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















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
    7 hours ago











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

    – Angew
    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













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 7 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
    7 hours ago











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

    – Angew
    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

















  • 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
    7 hours ago











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

    – Angew
    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
















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
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
7 hours ago













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

– Angew
7 hours ago





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

– Angew
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





@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 7 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

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367