小编b4k*_*ncs的帖子

C++继承的方法调用基类的方法而不是重载方法

鉴于代码:

class A{
public:
    void callFirst()
    {
        callSecond();
    }
    void callSecond()
    {
        cout << "This an object of class A." << endl;
    }
};

class B : public A{
public:
    void callSecond()
    {
        cout << "This is an object of class B." << endl;
    }
};

int main()
{
    B b;
    b.callFirst();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到输出:

This an object of class A.
Run Code Online (Sandbox Code Playgroud)

我可以这样做,当我调用派生类的继承方法时,它不会反过来调用基类的方法而不是重载方法,除了重载第一个方法?

c++ inheritance overloading

0
推荐指数
1
解决办法
71
查看次数

在C++中删除动态char**

披露:我正试图用严格的时间和内存限制来解决挑战.我通常会使用向量和字符串,但在这里我需要最快和最小的解决方案(实际上在时间限制之上运行的向量),所以我转向char*的动态数组.我的代码的相关部分:

char** substrings(string s, int* n){
    *n = 0;
    ...
    ////////////////////////////////
    char** strings = new char*[*n];
    ////////////////////////////////
    for (int i = 0; i < s.length(); i++){
        for (int j = 1; j < s.length() - i + 1; j++){
            ...
            strings[si] = tmp;
            ...
        }
    }
    return strings;
}

int main(){
    ...
    for (int ti = 0; ti < t; ti++){
        cin >> s;
        char** substr = substrings(s, &n);
        ...

        for (int i = 0; i < n; i++){
            delete …
Run Code Online (Sandbox Code Playgroud)

c++ arrays dynamic char

0
推荐指数
1
解决办法
273
查看次数

标签 统计

c++ ×2

arrays ×1

char ×1

dynamic ×1

inheritance ×1

overloading ×1