Re: [gem5-dev] Query on inheritance and virtual functions

2011-06-08 Thread Nilay Vaish

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?

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


Re: [gem5-dev] Query on inheritance and virtual functions

2011-06-08 Thread Jack Harvard


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.





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


Re: [gem5-dev] Query on inheritance and virtual functions

2011-06-08 Thread Nilay Vaish

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.


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


Re: [gem5-dev] Query on inheritance and virtual functions

2011-06-08 Thread Jack Harvard

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.   

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


Re: [gem5-dev] Query on inheritance and virtual functions

2011-06-08 Thread Nilay Vaish

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


Re: [gem5-dev] Query on inheritance and virtual functions

2011-06-08 Thread Jack Harvard

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


[gem5-dev] Query on inheritance and virtual functions

2011-06-07 Thread Nilay Vaish

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
___
gem5-dev mailing list
gem5-dev@m5sim.org
http://m5sim.org/mailman/listinfo/gem5-dev


Re: [gem5-dev] Query on inheritance and virtual functions

2011-06-07 Thread Jack Harvard
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
 ___
 gem5-dev mailing list
 gem5-dev@m5sim.org
 http://m5sim.org/mailman/listinfo/gem5-dev

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