On Wed, 8 Jun 2011, Jack Harvard wrote:
On 8 Jun 2011, at 23:28, Nilay Vaish wrote:
On Wed, 8 Jun 2011, Jack Harvard wrote:
On 8 Jun 2011, at 19:09, Nilay Vaish wrote:
On Wed, 8 Jun 2011, Jack Harvard wrote:
When you declare your function private, you can't use instance.function() to
access it. Is it generating a compile time error?
On 8 Jun 2011, at 00:31, Nilay Vaish wrote:
Consider the following class declarations --
class A
{
public:
virtual void function() = 0;
};
class B : public A
{
private:
void function();
}
int main()
{
B b;
b.function();
}
Will this code compile correctly?
--
Nilay
I should say that my example program was not what I intended it to be. The main
function should look like --
int main()
{
B* b = new B();
A* a = b;
a->function();
return 0;
}
Now what would happen?
This compiles. However, if you do b->function(), you would get the same error
as your last example, due to the same reason.
It compiles and executes fine. What surprises me is that even though function()
is private for class B, still it gets invoked using the pointer from class A. I
was not aware of this before.
Overriding and access visibility is orthogonal, you use class A pointer to
access its public function.
I won't term this is a overriding, the function that will be called would
be the one defined in class B, as 'function()' is a virtual member of
class A. But then, 'function()' is private to class B, so I would expect
some error to occur. I think the reason is that visibility is tested only
at compile time and never at run time.
--
Nilay
_______________________________________________
gem5-dev mailing list
gem5-dev@m5sim.org
http://m5sim.org/mailman/listinfo/gem5-dev