I have the following contrived example (coming from real code):
template <class T>
class Base {
public:
Base(int a):x(a) {}
Base(Base<T> * &other) { }
virtual ~Base() {}
private:
int x;
};
template <class T>
class Derived:public Base<T>{
public:
Derived(int x):Base<T>(x) {}
Derived(Derived<T>* &other): Base<T>(other) {}
};
int main() {
Derived<int> *x=new Derived<int>(1);
Derived<int> y(x);
}
When I try to compile this, I get:
1X.cc: In constructor βDerived<T>::Derived(Derived<T>*&) [with T = int]β:
1X.cc:27: instantiated from here
1X.cc:20: error: invalid conversion from βDerived<int>*β to βintβ
1X.cc:20: error: initializing argument 1 of βBase<T>::Base(int) [with T = int]β
1) Obviously, gcc is confused by the constructors. If I remove the link from the constructors, then the code compiles. Therefore, I suppose something went wrong with links to link pointers. Can someone tell me what is going on here?
2) A little unrelated question. If I did something terrible, like "delete another" in the constructor (bear with me), what happens when someone passes me a pointer to something on the stack?
E.g. Derived<int> x(2);
Derived<int> y(x);
where
Derived(Derived<T>*& other) { delete other;}
How can I make sure the pointer is legitimately pointing to something on the heap?
source
share