简体   繁体   English

C ++中的函数签名和继承

[英]Function signature and inheritance in C++

Assume I have two classes A and B and B derives A . 假设我有两个类ABB派生A

Class A: A类:

class A
{
public:
    virtual const unsigned char* getArray()
    {
        return array;
    }

protected:
    unsigned char array[250];
};

Class B: B级:

class B : public A
{
public:
    virtual unsigned char* getArray()
    {
        return array;
    }
};

Can I create a class C that will do this? 我可以创建C类来做到这一点吗?

class C
{
public:
    const unsigned char* getArrayMiddle(A &a)
    {
        return (a.getArray() + 125);
    }

    unsigned char* getArrayMiddle(B &b)
    {
        return (b.getArray() + 125);
    }
};

With this simple example, I am trying to know if I can create two methods in the class C , one of them returning a const pointer if required by the type of the object it receives. 通过这个简单的示例,我试图知道是否可以在类C创建两个方法,如果接收到的对象的类型要求,则其中一个可以返回const指针。

Will the compiler automatically call the right method in C depending on the type of the parameter, even if B derives A ? 即使B派生A ,编译器是否也会根据参数的类型自动在C调用正确的方法?

To clarify, if I have 澄清一下,如果我有

A* obj = new B();
C c;
c.getArrayMiddle(*obj);

which function will be called? 哪个函数会被调用? Could it create unexpected situations? 会造成意想不到的情况吗?

Will the compiler automatically call the right method in C depending on the type of the parameter, even if B derives A ? 即使B派生A ,编译器是否也会根据参数的类型自动在C调用正确的方法?

The compiler will interpret a method call as being to either of the two methods depending on the static (compile-time) type of argument expression. 编译器将根据参数表达式的静态 (编译时)类型将方法调用解释为对这两个方法中的任何一个。 Whether you consider this to be the right method is determined, I suppose, by which one you want to be called. 我想,确定您是否认为这是正确的方法是要调用的方法。

If the static type of the argument is B & then the second variant (returning unsigned char * ) will be called. 如果参数的静态类型为B & ,则将调用第二个变体(返回unsigned char * )。 If it is not a B & but is an A & then the first variant (returning const unsigned char * ) will be called. 如果不是B &而是A &则将调用第一个变体(返回const unsigned char * )。

The precise rules determining which overloaded method is called in which circumstances are quite complex, but in general, a more specific suitable candidate is usually preferred. 确定在哪种情况下调用哪种重载方法的精确规则非常复杂,但是通常,通常首选更具体的合适候选对象。 In this case B & is more specific than A & , so it will be called if indeed the argument is a B & , even though it is in that case trivially convertible to an A & due to the inheritance hierarchy. 在这种情况下, B &A &更具体,因此,如果确实是B & ,则将调用该参数,即使在这种情况下A &由于继承层次结构,该参数可以微转换为A &

However , it is worth noting that (as per comment from cpplearner) your code as posted should not compile. 但是 ,值得注意的是(根据cpplearner的评论)发布的代码不应编译。 B should not be able to override getArray with a version which drops the const qualifier. B不能使用删除const限定符的版本覆盖getArray G++ 5.4 diagnoses this as an error. G ++ 5.4将其诊断为错误。 You could instead not have one method override the other: drop the virtual specifier, or give them different names, or mark the method in A as a const method. 相反,您不能让一个方法替代另一个方法:删除virtual说明符,或给它们指定不同的名称,或将A中的方法标记为const方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM