重写父类函数时的特殊状况

1.静态父类方法android

       在C++中没法重写静态方法。其实子类和父类存在同名的静态方法,它们是两个独立的方法。静态方法是属于类的,调用是经过类名而不是对象,虽然也能够经过对象调用静态方法,也就至关与class::function,可是C++在调用静态方法时,不关心对象实际是什么,只是关心编译时的类型。以下的结果ios

       

/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#ifndef SUPER_HPP
#define SUPER_HPP

#include <iostream>
/**
 * @brief The Super class 超类  父类
 */
class Super
{
public:
    Super() {}
    virtual ~Super() {}
    virtual void fun(const std::string &arg = "Super") const {
        std::cout << arg << '\n';
    }

    static void staticFun() {
        std::cout << "Super Static Function!\n";
    }
};

#endif // SUPER_HPP
/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#ifndef SUB_HPP
#define SUB_HPP
#include "super.hpp"

/**
 * @brief The Sub class 子类
 */
class Sub : public Super
{
public:
    Sub():Super() {}
    virtual void fun(const std::string &arg = "Sub") const{
        std::cout << arg << '\n';
    }

    static void staticFun() {
        std::cout << "Sub Static Function!\n";
    }
};

#endif // SUB_HPP
#include "sub.hpp"


int main()
{
    Super::staticFun();
    Sub::staticFun();
    Sub su;
    Super &s = su;
    s.staticFun();
    return 0;
}

因此子类不要重写静态方法,那是新的方法,还有就是静态方法调用经过类名设计模式

2.父类方法被重载函数

    当指定名称以及一组参数重写某个方法时,编译器会隐式的隐藏父类中的同名方法的全部其余实例。它的意思是,你居然重写了其中的一个,那么重载的你也应该所有重写,由于是相同的方法名,功能其实也应该是相同的,只是参数不同样而已,你居然改变一个方法,那说明功能所有要改变。spa

   

/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#ifndef SUPER_HPP
#define SUPER_HPP

#include <iostream>
/**
 * @brief The Super class 超类  父类
 */
class Super
{
public:
    Super() {}
    virtual ~Super() {}
    virtual void fun(const std::string &arg = "Super") const {
        std::cout << arg << '\n';
    }

    static void staticFun() {
        std::cout << "Super Static Function!\n";
    }
    //下面为两个重载的函数
    virtual void overload() const {
        std::cout << "Super overload()\n";
    }

    virtual void overload(int t) const {
        std::cout << "Super overload(int) and arg is " << t << '\n';
    }
};

#endif // SUPER_HPP

/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#ifndef SUB_HPP
#define SUB_HPP
#include "super.hpp"

/**
 * @brief The Sub class 子类
 */
class Sub : public Super
{
public:
    Sub():Super() {}
    virtual void fun(const std::string &arg = "Sub") const{
        std::cout << arg << '\n';
    }

    static void staticFun() {
        std::cout << "Sub Static Function!\n";
    }
    //Sub重写了其中的一个
    virtual void overload() const {
        std::cout << "Sub overload()\n";
    }
};

#endif // SUB_HPP

#include "sub.hpp"


int main()
{
    Sub su;
    su.overload(8);
    return 0;
}


一个Sub的对象调用了形参为int的函数,编译时报no matching method of 'overload(int)'的错误,说明,在子类中的父类重载方法已经被隐式的隐藏了。
.net

可是你实在须要这些问题,而你又不须要重写,可使用using语句,添加一句using Super::overload,标明我接受父类全部的重载函数设计

3.重写private和protected父类方法code

   访问限定符只是限定访问和重写没有关系。其实这种重写private和protected方法是一种设计模式,被称为模板。在作android开发中,Activity类有protected void onCreate方法须要重写,就是在一个类继承体系中,这几个操做是固定有的,在没有子类实现的行为是不同的。再举个例子,在Qt中一个QWidget的子类能够须要处理一些事件,可是每一个子类对响应事件多是不一样的。对象

4.父类方法具备默认参数值blog

   在C++中解析默认参数值是根据描述对象的表达式类型绑定默认参数,而不是根据实际的对象类型绑定参数。这点和上述提到的第一点很像。

  

#include "sub.hpp"


int main()
{
    Sub su;
    Super &s = su;
    s.fun();
    return 0;
}

5.子类函数具备不一样的访问级别

访问基本的改变无非两种,要不增强,要不放松。能够有以下形式,下降访问级别可让子类为访问级别高的提供一个public修饰的包裹函数,还有就是直接在子类中重写方法和修改访问限定符。提升访问级别只能在子类中修改访问限定符,但是有时也不能作到彻底的提升

/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#ifndef SUPER_HPP
#define SUPER_HPP

#include <iostream>
/**
 * @brief The Super class 超类  父类
 */
class Super
{
public:
    Super() {}
    virtual ~Super() {}
    //public 访问级别的函数
    virtual void fun() const {
        std::cout << "public" << '\n';
    }

    static void staticFun() {
        std::cout << "Super Static Function!\n";
    }
    //下面为两个重载的函数
    virtual void overload() const {
        std::cout << "Super overload()\n";
    }

    virtual void overload(int t) const {
        std::cout << "Super overload(int) and arg is " << t << '\n';
    }

    virtual void overload(double t) const {
        std::cout << "Super overload(double) and arg is " << t << '\n';
    }

};

#endif // SUPER_HPP


/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#ifndef SUB_HPP
#define SUB_HPP
#include "super.hpp"

/**
 * @brief The Sub class 子类
 */
class Sub : public Super
{
public:
    Sub():Super() {}


    static void staticFun() {
        std::cout << "Sub Static Function!\n";
    }
    //Sub重写了其中的一个
    virtual void overload() const {
        std::cout << "Sub overload()\n";
    }
    using Super::overload;
protected:
private:
    //private
    virtual void fun() const{
        std::cout << "private" << '\n';
    }
};

#endif // SUB_HPP

#include "sub.hpp"


int main()
{
    Sub su;
 //   su.fun();
    Super &s = su;
    s.fun();
    return 0;
}

相关文章
相关标签/搜索