Why is "A & a = a" valid?
#include <iostream>
#include <assert.h>
using namespace std;
struct Base
{
Base() : m_member1(1) {}
Base(const Base & other)
{
assert(this != &other); // this should trigger
m_member1 = other.m_member1;
}
int m_member1;
};
struct Derived
{
Derived(Base & base) : m_base(m_base) {} // m_base(base)
Base & m_base;
};
void main()
{
Base base;
Derived derived(base);
cout << derived.m_base.m_member1 << endl; // crashes here
}
The above example is a synthesized version of a foggy constructor. I used the link from a member of the class Derived::m_basebecause I wanted to make sure that the element would be initialized by calling the constructor. One of the problems is that neither GCC nor MSVC give me a warning in m_base(m_base). But for me, the more serious thing is that the statement finds everything good, and the application crashes later (sometimes far from an error). Question: Is it possible to indicate such errors?