假设我有一个Base类和一个这样的派生类:
Base* BasePtr = new Foo();
让我们说一个指向base的指针:
setFoo()
如何使用此Base* Baseptr;
变量访问Foo
?
可以不使用虚拟方法吗?
在基础中使用虚拟方法只是为了能够使用Process[] processes = Process.GetProcessesByName("chrome");
foreach (Process p in processes)
{
IntPtr windowHandle = p.MainWindowHandle;
Rectangle rect = new Rectangle();
GetWindowRect(windowHandle, out rect);
//System.Windows.Forms.MessageBox.Show("Top = " + rect.Top + "left = " + rect.Left + "width = " + rect.Width + "height = " + rect.Height + "X = " + rect.X + "Y = " + rect.Y + "Right = " + rect.Right + "Bottom = " + rect.Bottom);
System.Drawing.Point pt = new System.Drawing.Point();
if (rect.Width > 20 && rect.Height > 20)
{
pt.X = rect.Left + ((rect.Right - rect.Left) / 4); // point in the middle
pt.Y = rect.Top + 30; // and a little bit lower (URL edit field)
int try_again_cnt = 0;
try_again:
pt.Y += 4;
if (try_again_cnt++ > 6)
return;
object varChildID;
IAccessible accObj;
IntPtr success = AccessibleObjectFromPoint(pt, out accObj, out varChildID);
int ChildID = (int)varChildID;
**** GETTING ChildID 0 EVERY TIME DONT KNOW WHY *******
if (ChildID != 0)
{
string s = accObj.get_accValue(varChildID);
System.Windows.MessageBox.Show(s);
}
else
{
goto try_again;
}
}
自己的方法,这似乎是违反直觉的。
我问的原因是我有一个需要指向Base的指针数组,以及一种编辑派生类的方法'自己的属性是必需的。
由于
答案 0 :(得分:-1)
这个怎么样:
Foo * p = new Foo();
p->setFoo(10);
Base * BasePtr = p;
可替换地:
Base * BasePtr = new Foo();
static_cast<Foo *>(BasePtr)->setFoo(10);