Consider the following situation:
#include <iostream>
using namespace std;
class A {
public:
int y;
A(int &x) {
x = 2;
y = 1;
}
};
class B : public A {
public:
int *p;
B(int t) : A(*p) {}
};
int main() {
B b(2);
return 0;
}
When the constructor of B is called, p
has a junk value. So, when *p
is to be passed to A()
, it gives me a Segmentation Fault. I would want to initialize p = new int;
before calling A(*p)
- is this possible?
EDIT: Interestingly, calling B
's constructor without arguments didn't give a segmentation fault.