小编Ale*_*Dan的帖子

函数指针存储的地址在哪里

我知道函数指针存储函数的地址.

int fun(int x){
 //return something
} 
int (pfun*)(int)=&fun;

int main(){

std::cout << &fun << "\n"; // this print out 1
std::cout << fun << "\n" ; // this print out 1
std::cout << &pfun << "\n"; // this print out 0x0022ff40
std::cout << pfun << "\n" ; // this print out 1

 }
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:
1)如果fun()甚至没有地址,pfun如何指向fun().
2)例如在运行时使用指针函数时的动态绑定.编译器是否将pfun值更改为真正的指针,如0X .....因此在运行时将知道要调用哪个函数,因为编译后名称不存在?

c++ pointers function

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

c ++非完全构造的对象释放

代码:

#include<iostream>
using namespace std;
class A{
    public:
    A(){cout << "A() Constructor " << endl;
        throw 1;
    }

};


int main(){
A* p=0;
cout << p << endl; // p value is 0
try{
p=new A(); // object is not fully constructed, no address is returned to p. for future deallocation

}
catch(...){cout << "Exception" << endl;}
cout << p << endl; // this will output that p has the value 0,proof that no address was returned to p.


} …
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling exception

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

谁决定一个字节的大小,是编译器还是CPU?

我知道一个字节是CPU内存的最小可寻址代码单元,所以x86-32机器中的字节大小是8位,但是c ++标准规定sizeof(char)==1 bytes>=8 bits,所以我可以有一个x86-32机器的编译器具有sizeof(char)==16 bits哪个意思那个字节是16位.

所以在这个例子中,字节是关于CPU的8位,从编译器的角度来看 16位.那么字节的定义是什么,它的大小是什么?

c c++ byte

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

该函数如何在c ++中返回工作

我想知道如果我调用函数f(3),编译器如何保存这个临时int;

int f (int x) { return x; } 
Run Code Online (Sandbox Code Playgroud)

以及编译器将如何解决这个问题:

int a=f(3);
Run Code Online (Sandbox Code Playgroud)

就像做一个int a = x; (我知道x已经被破坏了)或者它确实创建了一个名为f(3)的临时变量,就像这样int f(3)=x;

int& a=f(3);
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用?

c++ function

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

在调用析构函数后使用对象

可能重复:
可以在其范围之外访问局部变量的内存吗?

码:

#include <iostream>
using namespace std;

class B{
    public:
    int b;
    B():b(1){}
    ~B(){cout << "Destructor ~B() " << endl;}
};

class A{
    public:
    B ob;
    A()try{throw 4;}
    catch(...){cout << "Catched in A() handler : ob.b= " << ob.b<<endl;}
};




int main()try{
A t;


}
catch(...){cout << "CATCHED in Main" <<  endl;}
Run Code Online (Sandbox Code Playgroud)

输出:

Destructor ~B() 
Catched in A() handler : ob.b= 1
CATCHED in Main
Run Code Online (Sandbox Code Playgroud)

我的问题是如何访问其析构函数调用完成b的对象的成员变量ob.

c++ destructor exception

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

为什么访问变量的地址会改变另一个变量的地址?

代码1:

int main(){
   int a=1;
   int b=2;
   cout << "&a: "<<&a << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出1:

&a: 0x22ff48
Run Code Online (Sandbox Code Playgroud)

代码2:

int main(){
   int a=1;
   int b=2;
   cout << "&a: "<<&a << endl;
   cout << "&b: "<<&b << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出2:

&a: 0x22ff4c
&b: 0x22ff48
Run Code Online (Sandbox Code Playgroud)


所以我的问题是为什么a当我打印出varibale的地址时,varibale的地址发生了变化b

c++ memory memory-address

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

静态成员变量初始化c ++

  1. 当我没有将值初始化为静态成员变量时,为什么编译器会给我一个错误?它不应该被初始化为0吗?
  2. 为什么我必须在类外初始化成员变量?(这是非法的,因为如果你这样做,并在main函数中更改这个静态成员变量的值,你创建了这个clas的对象,它会将静态mamber变量重新赋值给旧值)而const static成员变量在类中是合法的(这是可能的,因为你无论如何都不能改变这个静态成员变量的值)?

错误:对class_name :: a的未定义引用

c++ static

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

从派生类重写Base类的const方法

class Base{
//...
    public:
    int get()const{ // const
    // Do something.
     }
    int get(int x){
    // Do Someting.
     }

    //...

};
class Derived:public Base{
//....
     public:
     int get(){ // not const (not the same signature as the one is the base class) 
     //Dosomething
     }
      //...

};
Run Code Online (Sandbox Code Playgroud)

我知道Derived类中的get()将隐藏Base类中的get()和get(int x)方法.所以我的问题是:
1)这是否意外超载或覆盖?
2)在派生类中使get()const会改变一些东西(隐藏或不隐藏Base类方法).

从c ++书中引用:

"当你打算覆盖它时,通过忘记包含关键字const来隐藏基类方法是一个常见的错误.const是签名的一部分,而离开它会改变签名,因此隐藏方法而不是覆盖它."

c++ overriding overloading class

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