Quoting Jack Harvard <jack.harv...@gmail.com>:


On 9 Jun 2011, at 00:10, Nilay Vaish wrote:

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.

It's still overriding for the function() defined in B which is overriding the function defined in base class A (whether it's defined as virtual or pure virtual). In C++ it's allowed to override with a private member. That means you can only call it via a pointer or reference to the base. This is occasionally useful (eg if the base is a private one), but it isn't very common in my experience.

(A long, long time ago there were rules in C++ like those in Java to
prevent derived classes reducing the visibility of members. They were
abandoned because they got in the way of reasonable code.)

_______________________________________________
gem5-dev mailing list
gem5-dev@m5sim.org
http://m5sim.org/mailman/listinfo/gem5-dev


Guys, lets try to keep conversations on the list related to gem5 please.

Gabe
_______________________________________________
gem5-dev mailing list
gem5-dev@m5sim.org
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to