I have a question about C ++ private inheritance. See the following code example:
#include <iostream>
class Foo {
public:
virtual void doSomething(int value) {
std::cout << value << std::endl;
}
};
void foobar(Foo& foo, int value) {
foo.doSomething(value);
}
class Bar : Foo {
public:
Bar() {
foobar(*this, 42);
}
};
int main() {
Bar b;
foobar(b, 42);
}
At the moment, I cannot understand (or find the correct specification in C ++) why the function foobarcan be called in the constructor Barwith *this , despite private inheritance . If I try to call a function foobarwith Barobject bin a function main, the compiler will throw an error, as expected, due to private inheritance.
What is the difference between foobar(*this, 42)and foobar(b, 42)which I am missing?
source
share