我有这个代码(钻石问题):
#include <iostream>
using namespace std;
struct Top
{
void print() { cout << "Top::print()" << endl; }
};
struct Right : Top
{
void print() { cout << "Right::print()" << endl; }
};
struct Left : Top
{
void print() { cout << "Left::print()" << endl; }
};
struct Bottom: Right, Left{};
int main()
{
Bottom b;
b.Right::Top::print();
}
Run Code Online (Sandbox Code Playgroud)
我想print()在Top课堂上打电话.
当我尝试编译它时,我得到错误:'Top' is an ambiguous base of 'Bottom'在这一行:b.Right::Top::print();
为什么它不明确?我明确规定,我想Top从Right与不从 …
阅读本文后,我尝试进行以下转换static_cast:
class A;
class B {
public:
B(){}
B(const A&) //conversion constructor
{
cout << "called B's conversion constructor" << endl;
}
};
class A {
public:
operator B() const //conversion operator
{
cout << "called A's conversion operator" << endl;
return B();
}
};
int main()
{
A a;
//Original code, This is ambiguous,
//because both operator and constructor have same cv qualification (use -pedantic flag)
B b = a;
//Why isn't this ambiguous, Why …Run Code Online (Sandbox Code Playgroud) 为了证明我试过这个:
int main()
{
{
char a;
printf("Address of a %d \n",&a);
}
char b;
printf("Address of b %d \n",&b);
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,首先创建了b(因为外部块比内部执行得更早),并且当执行到达内部块时,创建了a.输出上面的代码:
Address of a 2686766
Address of b 2686767
Run Code Online (Sandbox Code Playgroud)
(在x86上测试(堆栈向下增长,因此首先创建具有更大地址的变量)).
但是这个怎么样?:
int main()
{
{
char a;
printf("Address of a %d \n",&a);
} // I expected that variable a should be destroyed here
{
char b;
printf("Address of b %d \n",&b);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Address of a 2686767
Address of b …Run Code Online (Sandbox Code Playgroud) C89 long long不存在(已添加C99)C++03(和C++98)long long不存在(它被添加C++11)现在,如果我编译这个:
typedef long long myType;
int main()
{
myType a;
}
Run Code Online (Sandbox Code Playgroud)
使用(g++ sourceFile.cpp -std=c++03 -pedanticOR gcc sourceFile.c -std=c89 -pedantic)它会发出警告,表示当前选择的标准不支持long long
但是,如果我编译它(使用相同的标志):
#include <stdint.h> //in case of C
#include <cstdint> //in case of C++
int main()
{
int64_t a;
}
Run Code Online (Sandbox Code Playgroud)
即使stdint.h(cstdint仅包含stdint.h并使名称在内部可见std)包含,我也不会收到任何警告
...
typedef long long int64_t;
...
Run Code Online (Sandbox Code Playgroud)
我想知道这是如何工作的.