Connecting Tech Pros Worldwide Forums | Help | Site Map

HOW CAN I invoke method through the middle class in the inherit hiberarchy?

Newbie
 
Join Date: Nov 2009
Posts: 1
#1: 1 Week Ago
class Base1 { public virtual func() ... }
class Base2 : Base1 { public override func() ... }
// i already have two class Base2 inherit Base1;
//and want to inovke Base1::func(),but not Base2::func();
class Base3 : Base2 {
public override func()
{
// i want invoke Base1::func();
// how can i do?
}
}

insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: 1 Week Ago

re: HOW CAN I invoke method through the middle class in the inherit hiberarchy?


This looks like C++, so I'm not entirely sure. In C# it would be something like
Expand|Select|Wrap|Line Numbers
  1. base.base.func();
So maybe in C++ it would be
Expand|Select|Wrap|Line Numbers
  1. base::base::func()
I'm not sure as I've never worked with Visual C++.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#3: 1 Week Ago

re: HOW CAN I invoke method through the middle class in the inherit hiberarchy?


base.base throws compiler errors for me. base seems to only go up ONE level for me and no way to go up further?
As for the original problem, I think you need to use different function names?

You might be able to do something as a type cast though?

Don't use virtual, use new keywords instead, observe:


Expand|Select|Wrap|Line Numbers
  1. public class base1 
  2.     {
  3.         public void func()
  4.         {
  5.             MessageBox.Show("base1"); 
  6.         }
  7.     }
  8.     public class base2 : base1
  9.     {
  10.         public new void func()
  11.         {
  12.             MessageBox.Show("base2");
  13.         }
  14.     }
  15.     public class base3 : base2
  16.     {
  17.         public new void func()
  18.         {
  19.             base.func(); //shows "base2"
  20.             base1 o = (base1)this;
  21.             o.func(); //shows "base1"
  22.         }
  23.     }
  24.  
Then this call will verify it:
Expand|Select|Wrap|Line Numbers
  1. base3 f = new base3();
  2. f.func(); 
  3.  
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: 1 Week Ago

re: HOW CAN I invoke method through the middle class in the inherit hiberarchy?


I admit I was just guessing. I've never had to do that.
Reply

Tags
c# syntax