小编gx_*_*gx_的帖子

C++从具有相同虚函数名的多个基类继承

我试过这段代码:

class A
{
    virtual void foo() = 0;
};

class B
{
    virtual void foo() = 0;
};

class C : public A, public B
{
    //virtual void A::foo(){}
    //virtual void B::foo(){}

    virtual void A::foo();
    virtual void B::foo();
};

void C::A::foo(){}
void C::B::foo(){}

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

使用注释部分时可以,但是当我尝试在类声明之外编写定义时,编译器会报告错误.我正在使用MSVC11编译器,有谁知道怎么写这个?我需要将代码移动到cpp文件中.

谢谢~~

c++ virtual class function multiple-inheritance

13
推荐指数
2
解决办法
2万
查看次数

c ++隐式复制和移动构造函数背后的基本原理?

我对c ++隐式复制构造函数的理解类似于

T(T const& x) : 
    base1(x), base2(x) ... , 
    var1(x.var1), var2(x.var2)...
{}
Run Code Online (Sandbox Code Playgroud)

移动构造函数,复制和移动赋值也遵循类似的模式.

为什么没有定义类似于以下内容?

T(T const& x) : 
    base1(static_cast<base1 const&>(x)),
    base2(static_cast<base2 const&>(x)) ... , 
    var1(x.var1), var2(x.var2)...
{}
Run Code Online (Sandbox Code Playgroud)

我有一个类具有隐式复制/移动构造函数/赋值运算符,以及一些转换构造函数.我把工作委托给了一些实现类.

class common_work //common implementation of many work like classes
{
   common_work(common_work const&) = default;
   common_work(common_work&&) = default;// ... implicit constructors work for me.
   //a forwarding constructor which can take many work like objects
   template<class T, enable_if<work_like<T> > >
   common_work(T&& x) { ... }
};
class work1 //one of the implementation …
Run Code Online (Sandbox Code Playgroud)

c++ constructor assignment-operator perfect-forwarding c++11

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

glibc`div()`代码中的错误?

如" 在C++中使用负数进行整数除法 "中所述,在C99之前的C中(即在C89中)和在C++之前的C++中(即在C++ 98和C++ 03中),用于整数除法计算,其中两个操作数都是负数,余数的符号(或等效地,商的舍入方向)是实现定义的.

然后是标准函数std::div,它被指定为将商截断为零(即,余数与被除数(分子)具有相同的符号)(例如,参见"div()库函数的目的是什么?"的答案.

这是glibc的div()(源代码)代码(也引用" Is div function useful(stdlib.h)? "):

(注:div_t定义为:

typedef struct
  {
    int quot;
    int rem;
  } div_t;
Run Code Online (Sandbox Code Playgroud)

- 结束说明.)

/* Return the `div_t' representation of NUMER over DENOM.  */
div_t
div (numer, denom)
     int numer, denom;
{
  div_t result;

  result.quot = numer / denom;
  result.rem = numer % denom;

  /* The ANSI standard says that |QUOT| <= |NUMER / DENOM|, where …
Run Code Online (Sandbox Code Playgroud)

c c++ implementation glibc integer-division

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

为什么auto_ptr专门用于void?

我决定从原始指针转向智能指针,所以我可以从阅读http://en.cppreference.com/w/cpp/memory/auto_ptr开始.

在那里,我看到他们是专门的void.

为什么是这样?它在任何情况下都有用吗?

c++ smart-pointers void template-specialization c++11

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

如何从复制赋值运算符调用复制构造函数?

我正在实施一个链表.我写了一个拷贝构造函数:

        // --- copy constructor ---
    IntList(const IntList& last_list) {
        first = new IntNode(last_list.getFirst()->getData());
        cout << "copy first " << first->getData() << endl;

        IntNode* last_node = first;
        IntNode* node = last_list.getFirst()->getNext();
        while(node!=NULL) {
            IntNode* new_node = new IntNode(node->getData());
            last_node->setNext(new_node);
            cout << "copy " << new_node->getData()<< endl;
            last_node = new_node;
            node = node->getNext();
        }
    }
Run Code Online (Sandbox Code Playgroud)

据我了解,我的副本赋值运算符(operator=)应该有2个目标:

  • 删除当前列表.
  • 复制新列表.

我可以通过调用我已编写的析构函数来实现这两个目标,然后调用复制构造函数.我该怎么做?

c++ destructor copy-constructor assignment-operator

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

按位AND的好奇行为

我正在编码,以下代码没有提供所需的输出.当pos被除以2时,pos&1应该返回余数.当我用pos%2替换pos&1时,一切正常.可能是什么问题呢?

#include <iostream>
using namespace std;
int main(){
    int y;
    unsigned long long int pos;
    cin>>y;
    cin>>pos;
    int f=0;
    while(y>0){
        y--;
        if(pos&1==0){
            f=1-f;
        }
        pos=pos/2;
    }
    if(f==1){
        cout<<"blue\n";
    }
    else
    cout<<"red\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ bits bit-manipulation

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