小编sre*_*ree的帖子

static const double不能有类内初始值设定项.为什么会这样?

具有以下代码的问题是"const double"类型的静态成员,不能具有类内初始化程序.为什么仅适用于以下代码中的'const double'?请帮我.

class sample{
   static const char mc = '?';
   static const double md = 2.2;
   static const bool mb = true;
};
const char sample::mc;
const double sample::md;
const bool sample::mb;
int main(){
}
Run Code Online (Sandbox Code Playgroud)

c++

31
推荐指数
3
解决办法
3万
查看次数

为什么要打印反向字符串?

我对u6.c的预期输出是ABC,但在这里我得到了CBA为什么会这样呢?你能详细解释一下吗?

union mediatech
{ 
 int i; 
 char c[5]; 
};

int main(){
 mediatech u1 = {2};               // 1
 mediatech u2 = {'a'};             // 2
 mediatech u3 = {2.0};             // 3
 mediatech u6 = {'ABC'};           // 6

 cout<<"\nu6.i = "<<u6.i<<" u6.c="<<u6.c;  // o/p: u6.i=4276803  u6.c=CBA
}
Run Code Online (Sandbox Code Playgroud)

c++ unions

6
推荐指数
2
解决办法
238
查看次数

使用可选参数覆盖虚函数

为什么这种印刷23作为输出; 我的期望是33.有人可以对此有所了解.

struct A {
    virtual void f() {cout << "1";}
};

/* Private inheritance */
struct B : private A {
    void f(int x = 0) {cout << "2";}
};

struct C : B {
    void f(){cout << "3";}
};

int main() {
    C obj;
    B &ref = obj;
    ref.f();
    obj.f();
}
Run Code Online (Sandbox Code Playgroud)

c++ virtual inheritance optional-arguments

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

如何grep以.c或.cpp结尾的行?

我有一个文件如下,我想grep有线.c.cpp扩展名.我尝试过使用cat file|grep ".c"grep但是我将所有类型的扩展作为输出.请详细说明一下.提前致谢.

file contents are below:
/dir/a/b/cds/main.c
/dir/a/f/cmdss/file.cpp
/dir/a/b/cds/main.h
/dir/a/f/cmdss/file.hpp
/dir/a/b/cdys/main_abc.c
/dir/a/f/cmfs/file_123.cpp
Run Code Online (Sandbox Code Playgroud)

linux shell

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

虚函数,函数重载,继承

为什么不打印"双倍"?寻找详细的解释.谢谢你的时间!

#include<iostream.h>
using namespace std;

class B{
 public:
 virtual  int ft(int i) { cout <<"int"; return 0;}
};
class D: public B {
 public:
  double ft(double i){cout << "doub"; return 0.0;}
  int ft(int i) { cout <<"intdoub"; return 0;}
};

int main(){
 B *pB = new D;
 pB->ft(2.3);
}
Run Code Online (Sandbox Code Playgroud)

o/p是'intdoub'

c++ virtual

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

推入向量后,内存已损坏

为什么在推入向量后内存被破坏了.在下面的程序中,我有一个带字符串var的结构(它不是指针).我每次创建一个本地struct对象并分配一个字符串值并推送到向量.在推送到向量后,我正在对本地struct对象进行更改.但是这种变化正在向量结构对象的字符串数据中反映出来.

    #include <iostream>
    #include <vector>
    #include <string>
    #include <memory.h>

    using namespace std;

    void PushVector(string);

    struct thread_info
    {
            int  id;
            string threadname;
            bool bval;
    };

    std::vector<thread_info> myvector;


    int main ()
    {
            PushVector("Thread1"); // valid data into vector
            PushVector("Thread2");

            struct thread_info print;

            while(!myvector.empty())
            {
                    for(unsigned int index = 0; index < myvector.size(); ++index )
                    {
                            print = myvector.at(index);
                            cout<<"id : "<<print.id<<"\nthread name : "<<print.threadname<<"\nbool value : "<<print.bval<<endl;
                    }
                    myvector.clear();
            }
            return 0;
    }

    void PushVector(const string str)
    {

            std::cout << "Push the …
Run Code Online (Sandbox Code Playgroud)

c++ linux stl

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

如何调用纯虚函数?

如何在这里调用纯虚函数"pvf()"?请问有人可以解释一下吗?.................................................. .............

#include<iostream>

using namespace std; 

class a
{
public:
    a()
    {
        this->pvf();
        this->p();
    }

    virtual int pvf() = 0;

    virtual void p()
    {
        cout << "\n p fun";
    }
};

int a::pvf()
{
    cout<<"\n pure";
}

class b : public a
{
    int i,j,k;

    void o()
    {
        cout<<"\n der";
    }
};

int main()
{
    //b b1; 
}
Run Code Online (Sandbox Code Playgroud)

c++ pure-virtual

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

算术表达式从左到右或反向评估?

在这里我把o/p称为'1',但我的期望是'100'.为什么会这样?请说清楚.谢谢!

int main()
    {
            cout << 100 / 10 / 10 << endl;
    }
Run Code Online (Sandbox Code Playgroud)

c++

-8
推荐指数
1
解决办法
1020
查看次数