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;
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
New contributor
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
c++ inheritance friend
New contributor
New contributor
edited 7 hours ago
Maple
New contributor
asked 8 hours ago
MapleMaple
1336
1336
New contributor
New contributor
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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).
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 offoo
who does the conversion fromB*
toA*
, 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
add a comment |
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
.
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
add a comment |
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:
@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 ;) stillfoo
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 separateB
instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodifiedb
might be used. Possibly having two public foo overloads, one acceptingA*
, one acceptingB*
and the latter one just doing the cast as needed for calling the former might be less intervening...
– Aconcagua
4 hours ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
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 offoo
who does the conversion fromB*
toA*
, 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
add a comment |
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).
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 offoo
who does the conversion fromB*
toA*
, 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
add a comment |
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).
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).
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 offoo
who does the conversion fromB*
toA*
, 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
add a comment |
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 offoo
who does the conversion fromB*
toA*
, 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
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
.
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
add a comment |
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
add a comment |
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:
@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 ;) stillfoo
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 separateB
instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodifiedb
might be used. Possibly having two public foo overloads, one acceptingA*
, one acceptingB*
and the latter one just doing the cast as needed for calling the former might be less intervening...
– Aconcagua
4 hours ago
add a comment |
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:
@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 ;) stillfoo
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 separateB
instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodifiedb
might be used. Possibly having two public foo overloads, one acceptingA*
, one acceptingB*
and the latter one just doing the cast as needed for calling the former might be less intervening...
– Aconcagua
4 hours ago
add a comment |
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:
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:
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 ;) stillfoo
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 separateB
instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodifiedb
might be used. Possibly having two public foo overloads, one acceptingA*
, one acceptingB*
and the latter one just doing the cast as needed for calling the former might be less intervening...
– Aconcagua
4 hours ago
add a comment |
@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 ;) stillfoo
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 separateB
instance is, globally seen, critical, though, as foo might modify it – but later in main, the unmodifiedb
might be used. Possibly having two public foo overloads, one acceptingA*
, one acceptingB*
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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