小编Ron*_*n_s的帖子

我们可以有一个虚拟静态方法吗?(C++)

可能重复:
C++静态虚拟成员?

我们可以使用虚拟静态方法(在C++中)吗?我试过编译以下代码:

#include <iostream>
using namespace std;

class A
{
public:
    virtual static void f() {cout << "A's static method" << endl;}
};

class B :public A
{
public:
    static void f() {cout << "B's static method" << endl;}
};

int main()
{
    /* some code */
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是编译器说:

member 'f' cannot be declared both virtual and static
Run Code Online (Sandbox Code Playgroud)

所以我猜答案是否定的,但为什么呢?

谢谢,罗恩

c++ virtual static

41
推荐指数
1
解决办法
5万
查看次数

私有继承VS组成:何时使用哪个?

私有继承VS组成.

我在使用每个时都有点困惑.由于私有继承在某种程度上密封了继承链,给出:

class A
{
private:
    int z;
protected:
    int y;
public:
    int x;
};

class B : private A
{
    /* B's data members and methods */
    /* B has access only to A's public and protected */
};

class C : public B
{
    /* can access no fields of B */
};
Run Code Online (Sandbox Code Playgroud)

C将无法使用任何B字段.我何时会使用私有继承,何时使用合成?

谢谢!

c++ inheritance

17
推荐指数
1
解决办法
6131
查看次数

多个(菱形)继承编译时没有"虚拟",但没有

给定以下代码(没有虚拟继承):

class A
{
public:
    virtual void f() = 0;
};

class B : public A
{
 public:
    virtual void f() {}
};

class C : public A
{
 public:
    virtual void f() {}
};

class D : public B, public C
{

/* some code */
};


int main()
{
    D d;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码编译.

另一方面,这里:

class A
{
public:
    virtual void f() = 0;
};

class B : virtual public A
{
    virtual void f() {}
}; …
Run Code Online (Sandbox Code Playgroud)

c++ virtual overriding multiple-inheritance virtual-inheritance

12
推荐指数
2
解决办法
1960
查看次数

Singleton - 受保护与私有构造函数

在设计单例时,为什么要构造构造protected而不是private?这基于我在网上看到的内容.

我们想控制那个类的实例数,公平,但为什么protected呢?不private会这样做吗?

c++ design-patterns

12
推荐指数
2
解决办法
4889
查看次数

虚拟表的顺序是否重要?

我是新人,所以对我很轻松:)从我的讲师前段时间讲过,虚拟表的顺序非常重要.但我不明白这个的原因!!?

鉴于下一个代码:

class A
{
public:
    A() {cout <<"1" << endl;};
    A (const A& s) {cout << "2" << endl;}
    ~A () {cout << "3" << endl;}
    void f1() {cout << "4" << endl; f2();}
    virtual void f2() = 0;
    virtual void f3() {cout << "5" << endl;}

};


class B : public A
{
public:
    B() {cout << "6" << endl;}
    B(const B& b) : A(b) {cout << "7" << endl;}
    ~B() {cout << "8" << endl;}

    virtual void …
Run Code Online (Sandbox Code Playgroud)

c++

10
推荐指数
3
解决办法
1703
查看次数

为什么这两个构造函数一起不产生歧义错误?

考虑以下:

class A
{
private:
    A() {}
public:
    A(int x = 0) {}
};


int main()
{
    A a(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我有两个构造函数,一个是默认值,另一个是使用默认参数转换构造函数.当我尝试编译代码时,我预计会出现歧义错误,但编译器不会产生错误.

即使我没有创建实例A,它也不会产生歧义错误.

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是为什么?

c++ constructor ambiguity

7
推荐指数
2
解决办法
1036
查看次数

为什么返回字符串&从const方法不能编译?

给出以下代码:

#include <iostream>
#include <string>
using namespace std;

class A
{
private:
    string m_name;
    string m_first;
public:
    A(): m_first("string") {}
    virtual void print() const {}
    string& getName() const {return m_first;}  // won't compile 
    const string& getLastName() const {return m_name;}  // compile
};


int main()
{
    A a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器提示:"invalid initialization of reference of type 'std::string&' from expression of type 'const std::string'" 为什么我不能从getName()返回"m_first"?我认为函数尾部的const表示该函数不会改变'this'...但我不是要改变它,只是返回一个数据成员.

c++ string const reference

5
推荐指数
2
解决办法
2462
查看次数

使用纯虚方法的抽象类 - 为什么可以做"Abstract*abs3;"?

考虑以下 :

class Abstract
{
public:
    virtual void func() = 0;
};

int main() {

    Abstract abs1; // doesn't compile
    Abstract * abs2 = new Abstract(); // doesn't compile
    Abstract * abs3;  // compiles

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

请注意我没有实现func(),那么为什么Abstract * abs3; 我们有一个纯虚方法和一个抽象类呢?我知道如果我尝试做abs3-> func(),我会遇到运行时错误.但是,我还不清楚为什么C++允许代码编译......?

谢谢,罗恩

c++ abstract-class pure-virtual

4
推荐指数
1
解决办法
359
查看次数

删除这个?它有什么作用?

鉴于以下内容:

#include <iostream>
using namespace std;

class A
{
public:
    void func() {delete this;}
    A() : x(5) {cout << "ctor A" << endl;}
    ~A() {cout << "dtor A" << endl;}
    int x;
};

int main() {
    A a;
    cout << "The X value: " << a.x << endl;
    a.func();  // calling 'delete this' ->going to the dtor #1
    cout << "The X value: " << a.x << endl;
    return 0;  
}
Run Code Online (Sandbox Code Playgroud)

输出是:

ctor A
The X value: 5
dtor A …
Run Code Online (Sandbox Code Playgroud)

c++ destructor self-destruction delete-operator

2
推荐指数
1
解决办法
785
查看次数

我什么时候需要实现operator []?

给出以下模板函数:

template <class T>
void DoSomething(T &obj1, T &obj2)
{
      if(obj1 > obj2)
        cout<<"obj1 bigger: "<<obj1;
      else if(obj1 == obj2)
        cout<<"equal";
      else cout<<"obj2 bigger: "<<obj2;

      T tmp(3);
      T array[2];
      array[0]=obj1;
      array[1]=obj2;
}
Run Code Online (Sandbox Code Playgroud)

我需要定义一个名为MyClass的类(仅限声明,即只是.h文件),它可以使用该模板函数.我定义了下一个声明:

class MyClass
{
public:
    MyClass();    // default ctor
    MyClass(int x);  // for ctor with one argument
    bool operator ==(const MyClass& myclass) const;
    bool operator >(const MyClass& myclass) const;
    friend ostream& operator<<(ostream &out,const MyClass& myclass);  // output operator

};
Run Code Online (Sandbox Code Playgroud)

我不明白的是为什么不需要为行定义operator []:

array[0]=obj1; array[1]=obj2; 
Run Code Online (Sandbox Code Playgroud)

?我什么时候需要定义operator []?谢谢,罗恩

c++ templates class

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

复制构造函数从const转换为非const?

考虑以下 :

class A
{
public:
    int xx;
    A(const A& other)
    {
        cout << "A cctor" << endl;
        /*  do some stuff */
    }

    A(int x) : xx(x) {}  /* conversion constructor */

    };


int main()
{    
    A a = 1;
    A other = a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下(以及一般情况下)说CCtor从const转换为非const是正确的吗?

谢谢,罗恩

c++ constructor copy-constructor

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