我知道函数指针存储函数的地址.
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 .....因此在运行时将知道要调用哪个函数,因为编译后名称不存在?
代码:
#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) 我知道一个字节是CPU内存的最小可寻址代码单元,所以x86-32机器中的字节大小是8位,但是c ++标准规定sizeof(char)==1 bytes>=8 bits,所以我可以有一个x86-32机器的编译器具有sizeof(char)==16 bits哪个意思那个字节是16位.
所以在这个例子中,字节是关于CPU的8位,从编译器的角度来看是 16位.那么字节的定义是什么,它的大小是什么?
我想知道如果我调用函数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)
为什么这不起作用?
可能重复:
可以在其范围之外访问局部变量的内存吗?
码:
#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.
代码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?
错误:对class_name :: a的未定义引用
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++ ×8
exception ×2
function ×2
byte ×1
c ×1
class ×1
destructor ×1
memory ×1
overloading ×1
overriding ×1
pointers ×1
static ×1