P
PengYu.UT
Hi,
I have the following which has a bug (see the commented line).
The one with the bug has the output of
10
-1073752704
The one without the bug has the output of
10
10
However, the compiler that I'm using can't generate any warning or
error for this bug. I'm wondering if there is a option to select such
that the compiler can tell this error?
Thanks,
Peng
#include <iostream>
class A{
public:
A(const int a) : _a(a) { //should be: A(const int &a) : _a(a) {
}
void print() const {
std::cout << _a << std::endl;
}
private:
const int &_a;
};
class B{
public:
B(const A &a) : _a(a) {
};
void print() const {
_a.print();
}
private:
const A &_a;
};
int main(){
int i = 10;
A a(i);
a.print();
B b(a);
b.print();
}
I have the following which has a bug (see the commented line).
The one with the bug has the output of
10
-1073752704
The one without the bug has the output of
10
10
However, the compiler that I'm using can't generate any warning or
error for this bug. I'm wondering if there is a option to select such
that the compiler can tell this error?
Thanks,
Peng
#include <iostream>
class A{
public:
A(const int a) : _a(a) { //should be: A(const int &a) : _a(a) {
}
void print() const {
std::cout << _a << std::endl;
}
private:
const int &_a;
};
class B{
public:
B(const A &a) : _a(a) {
};
void print() const {
_a.print();
}
private:
const A &_a;
};
int main(){
int i = 10;
A a(i);
a.print();
B b(a);
b.print();
}