小编JDe*_*ein的帖子

为什么左移与变量产生不同的结果与常数?

在编译下面的代码后,我得到了一个奇怪的结果,a = 1而b = 0.谁能解释一下幕后发生的事情?

#include<iostream>
using namespace std;

int main(){

    int n=32; 
    int a=1<<n;  //turns out a=1
    int b=1<<32; //turns out b=0
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ bit-shift

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

为什么条件移动不能正常工作

在我编译下面的代码后,该函数似乎没有按预期工作.

int cread(int *xp){

     return (xp?*xp:0);

}
Run Code Online (Sandbox Code Playgroud)

我在汇编版本中提取对应项,如下所示.

xp 在登记 %edx

movl $0, %eax
testl  %edx, %edx
cmovne (%edx), %eax
Run Code Online (Sandbox Code Playgroud)

谁能告诉我为什么即使测试失败,指针xp仍然被取消引用cmovne?不是ZF设置为1通过testl指令时%edx0

assembly

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

编译器如何决定调用哪个函数?

假设String类中有两个重载的成员函数(const版本和非const版本):

char & String::operator[](int i)         //Version 1
{
    cout<<"char & String::operator[](int i) get invoked."<<std::endl;
    return str[i];
}


const char & String::operator[](int i) const       //Version 2
{

    cout<<"const char & String::operator[](int i) const get invoked."<<std::endl;
    return str[i];
}
Run Code Online (Sandbox Code Playgroud)

并且有一个测试代码片段

int main(){
    String a;
    cout<<a[0]<<endl;  //Line 1
    a[0]='A';     //Line 2
}
Run Code Online (Sandbox Code Playgroud)

编译器如何决定调用哪个函数?我发现在运行程序时总是调用版本1.谁能告诉我它为什么会这样?版本2如何被调用?

c++ overloading const

4
推荐指数
2
解决办法
323
查看次数

为什么*(pa-1)的输出是`a`的最后一个元素?

我从下面代码的输出得到的是*(pa-1)=5:为什么呢?

#include<iostream>

using namespace std;

int main(){

    int a[5]={1,2,3,4,5};
    int *pa=(int *)(&a+1);

    cout<<"*(pa-1)="<<*(pa-1)<<endl;

}
Run Code Online (Sandbox Code Playgroud)

c++

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

"跟随链接"和"走树"是什么意思?

(我现在是Java的新生.)

以下是Ivor Horton的Beginning Java中的文字

"要确定Path对象是引用文件还是目录,可以使用Files类中的静态isDirectory()和isRegularFile()方法.对于这两种方法,第一个参数是您感兴趣的Path对象.您可以指定如果您不希望遵循链接,则NOFOLLOW_LINKS作为可选的第二个参数."

"java.nio.file.FileVisitor接口指定了可用于遍历目录和文件的方法.T是文件引用的类型,通常是类型Path.java.nio.file.SimpleFileVisitor类实现方法在FileVisitor接口中,它只访问树中的所有文件并重新抛出发生的任何I/O异常."

任何人都可以告诉我"通过链接"和"走一棵树"是什么意思?如果可能,请给我一个代码示例,提前谢谢!

java

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

catch块如何捕获对其他范围中的临时变量的引用?

你可以尝试一下.下面的程序编译并运行顺利.ex构造函数中的变量地址e与catch块中的临时变量的地址不同.然而,您可能会注意到exB行的值是通过e引用传递给的.谁能解释一下发生了什么?

#include<cstring>
#include<iostream>


using std::string;
using std::endl;
using std::cout;

class ThrowException;
ThrowException* TE_ptr;

class ThrowException{

    private:
        string msg;
        int b;
    public:
        ThrowException(string m="Unknown exception",int factor=0) throw(string,const char*);
        ~ThrowException(){        cout<<"destructor get called."<<endl;}
        friend std::ostream& operator<<(std::ostream& os,const ThrowException&TE);
};

ThrowException::ThrowException(string m, int f) throw(string,const char*):msg(m),b(f){
    cout<<"msg="<<msg<<'\n'<<"b="<<b<<endl;
    TE_ptr=this;
    if(b==1){
        string ex("b=1 not allowed.");
        cout<<"The address of e in constructor is "<<&ex<<endl;      //A
        throw ex;
    }
}

std::ostream&operator<<(std::ostream&os, const ThrowException &TE){
    os<<TE.msg<<'\n'<<TE.b<<endl;
}
int main(){

    try{ …
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么仍然可以访问成员函数,即使它被声明为"私有"?

可能重复:
公共虚拟函数在C++中派生为private

class B
{
    private:
    int b;
    public:
    B(int i);
    virtual void show()
    {
        cout<<"B::show()called. "<<b<<endl;
    }
};

B::B(int i=0)
{
    b=i;
}

class D:public B
{
    private:
    int d;
    void show()
    {
        cout<<"D::show() called. "<<d<<endl;
    }
    public:
    D(int i, int j);
};

D::D(int i=0, int j=0):B(i)
{
    d=j;
}

void fun(B&obj)
{
    obj.show();
}
/*if I redefine fun() as follow, the result would be the same
void fun(B*obj)
{
obj->show();
}
*/
int main()
{
    D *pd=new D(5,8); …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions access-specifier private-members

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

谁能告诉我为什么程序在try块中调用abort()?

下面的程序编译成功,但它无法运行并调用abort()函数,该函数抛出一条消息,警告"此应用程序已请求Runtime以不寻常的方式终止它.请在应用程序的支持团队中获取更多信息." , 为什么这样?

#include<cstring>
#include<iostream>

using std::string;
using std::endl;
using std::cout;

class ThrowException{

    private:
        string msg;
        int b;
    public:
        ThrowException(string m="Unknown exception",int factor=0) throw(string);        //A

};

ThrowException::ThrowException(string m, int f) throw(string):msg(m),b(f){                 //B
    if(b==1)
        throw "b=1 not allowed.";
}

int main(){

    try{
        ThrowException a("There's nothing wrong.", 1);
    }catch(string e){
        cout<<"The address of e in catch block is "<<&e<<endl;
    }    

}
Run Code Online (Sandbox Code Playgroud)

错误信息

c++

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

为什么以下函数可以访问类成员而不被声明为朋友或成员函数?

头文件:

// pe10-8arr.h -- header file for a simple list class

#ifndef SIMPLEST_
#define SIMPLEST_

// program-specific declarations
const int TSIZE = 45;      // size of array to hold title
struct film
{
    char title[TSIZE];
    int rating;
};

// general type definitions
typedef struct film Item;

const int MAXLIST = 10;
class simplist
{
private:
    Item items[MAXLIST];
    int count;
public:
    simplist(void);
 bool isempty(void);
 bool isfull(void);
    int itemcount();
 bool additem(Item item);
    void transverse( void (*pfun)(Item &item));
};

#endif
Run Code Online (Sandbox Code Playgroud)

代码使用标题:

#include "pe10-8arr.h" …
Run Code Online (Sandbox Code Playgroud)

c++

-3
推荐指数
2
解决办法
134
查看次数