考虑下面的程序:
#include <string>
#include <iostream>
class B {
private:
std::string s;
public:
B() { s = fun(); }
std::string fun() { return "hello"; }
void print() {
std::cout << s;
}
};
int main(){
B b;
b.print();
}
输出是Hello
我的问题如下:
- 数据成员(在此情况下为's')的内存分配顺序是什么。
- 在构造函数中调用fun()时,对象是否存在。
我怀疑我如何在构造函数之前调用b
对象上的函数。
B():s(fun()) {}
。 - rustyxstd::stack
和家族),请在标准中提供一个章节。 - Algirdas Preidžius