I have a few classes and I'm trying to understand how the vptr and vtable work in this situation.
class RGB {
short value[3];
};
class AbstractImage{
protected:
int n_pixels;
public:
virtual void show() = 0;
virtual AbstractImage* getMe() = 0;
virtual ∼AbstractImage() {};
};
template <typename T> class Image: public AbstractImage {
T* data;
public:
Image<T>(int n) { n_pixles=n; data=new T[n_pixles];}
virtual ∼Image<T>() { delete[] data; }
Image<T>(const Image<T>& rhs) {
n_pixels = rhs.n_pixels;
data = new T[n_pixels];
copyData(rhs);
}
Image<T>& operator=(const Image<T>& rhs) {
n_pixels = rhs.n_pixels;
delete[] data;
data = new T[n_pixels];
copyData(rhs);
return *this;
}
virtual void show() {/*some code*/}
virtual Image<T>* getMe() {return this;}
private:
void copyData(const Image<T>& rhs) {
for(int i=0l i<n_pixels;i++) {
data[i] = rhs.data[i];
}
}
};
typedef class Image<RGB> ColorImage;
typedef class Image<short> BWImage;
I am trying to figure out how the stack and heap should be after running the following implementation:
int main() {
AbstractImage* A = new ColorImage(4);
ColorImage B = colorImage(4);
A->show();
}
from my understanding there are 2 vptr created:
- B::vpointer - On the stack
- A::vpointer - On the heap
Do they have the same value? (contain the same address?) How many vtables are there here?