具有相同函数名的C

具有相同函数名的C

本文介绍了具有相同函数名的C ++继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这个答案已经在这个网站,但我已经看了所有,找不到我的问题的任何解决方案。它属于来自继承类的同名函数。这里是我的代码:

  class A 
{
public:
int foo c){c = c + 1; return c; };
};

class B:public A
{
public:
int foo(int c){c = c-1; return c; };
};

int main()
{
A [2];
array [0] = A item1;
array [1] = B item2;
for(int n = 0; n {
cout< array [n] .foo(10)<< endl
}
return 0;
}



我希望输出:

  11 //来自A类的foo()[10 + 1 = 11] 
9 //来自B类的foo ]

但我得到

  11 
11

发现foo()函数在B类中没有在for循环中被调用。相反,调用A类中的foo()函数,即使是在数组[1]的B对象。



这是因为我已经将数组定义为包含A类的对象吗?如果是这样,是否有一种方法,我可以有来自B类的foo()函数在该for循环中的第二个对象上调用?



对于任何帮助!

解决方案

我会忘记 array [0] = A item1; code>不是有效的C ++,只是假设你分配一个类型 A 的对象到 array [0] 和类型 B 数组[1] 的对象。好的,所以你有两个问题。



第一个被称为对象切片。将 B 类型的对象复制到 A 类型的对象时,只需复制 A 该对象的一部分。所以你在数组[1] 中有一个 B ,它只是一个 A 。如果你想要多态性(你这样做),那么你需要使用提供多态行为的指针或引用。这意味着你的数组 A * array [2]; 和do array [0] =& item1;现在,当你在一个指向 A的指针上调用一个函数的时候,array [1] =& item2;


$ b 指向 B ,它仍然只会调用 A s foo 成员函数。为什么?因为默认情况下,该函数将在对象的静态类型上查找。该静态类型是 A 。如果你想告诉编译器在你的对象的动态类型上查找你的函数 - 对象的 true 类型,即 B - 你需要使成员函数虚拟。因此,在 A 中,执行:

  virtual int foo {c = c + 1; return c; }; 

现在当你的编译器看到你正在调用 foo A * ,它会看到它是虚拟的,说哦,好吧,我应该动态查找这个函数,它会找到 B 执行 foo


Sorry if this answer is already on this site, but I've looked all over and couldn't find any solutions to my issue. It pertains to same-name functions from inherited classes. Here is my code:

class A
{
public:
    int foo(int c) { c = c+1; return c; };
};

class B : public A
{
public:
    int foo(int c) { c = c-1; return c; };
};

int main()
{
    A array[2];
    array[0] = A item1;
    array[1] = B item2;
    for (int n=0;n<2;n++)
    {
        cout << array[n].foo(10) << endl;
    }
    return 0;
}

I would expect an output of:

11    // foo() from A class  [10 + 1 = 11]
9     // foo() from B class  [10 - 1 = 9 ]

But instead I get

11
11

From testing this out, I have found that the foo() function in the B class does not get called within the for-loop. Instead, the foo() function in the A class is called, even on the B object at array[1].

Is this because I have defined the array as containing objects of the A class only? If so, is there a way I can have the foo() function from the B class be called on the second object within that for-loop?

Thank you in advance for any help!

解决方案

I'll forget that array[0] = A item1; isn't valid C++ and just assume that you're assigning an object of type A to array[0] and an object of type B to array[1]. Okay, so you have two problems.

The first is known as object slicing. When you copy an object of type B to an object of type A, you only copy the A part of that object. So what you have in array[1] is not a B at all, it's just an A. If you want polymorphism (which you do), then you need to use either pointers or references which provide polymorphic behaviour. That means make your array an A* array[2]; and do array[0] = &item1; array[1] = &item2;.

Now, when you call a function on a pointer to A that points to a B it will still only call As foo member function. Why? Because by default, the function will be looked up on the static type of the object. That static type is A. If you want to tell the compiler to look up your function on the dynamic type of your object - the true type of your object, which is B - you need to make that member function virtual. So in A, do:

virtual int foo(int c) { c = c+1; return c; };

Now when your compiler see that you're calling foo on an A*, it'll see that it's virtual and say "Oh okay, I should look up this function dynamically" and it'll find B's implementation of foo.

这篇关于具有相同函数名的C ++继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:22