How to let C++ compiler raise a warning or error when a const is assigned to a const reference?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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, g++ can't generate any warning or error for this bug. Various
options have been tried on version 3.3, 3.4, 4.0, and 4.1. None them
works. I'm wondering if there is a option to select such that g++ 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();
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux