c++ - pass reference to class inherited from abstract base class -



c++ - pass reference to class inherited from abstract base class -

i want pass pointer class inherited abstract base of operations class, exc_bad_access error (when calling function f()) in next (much simplified) code

class { double *pointer; public: a(double *p) { pointer = p; }; virtual void f() = 0; }; class b :public a{ double *pointer; public: b(double *p_) : a(p_){}; void f(){std::cout << pointer[0] << std::endl;}; };

if phone call e.g. this

double p[2] = {1.,2.}; b b(p); b.f();

the problem doesn't seem there if base of operations class not abstract, can't figure out what's wrong above code. help in solving this, , maybe suggesting different way of achieving kind of construction appreciated!

the problem "pointer[0]" in f() ends accessing pointer fellow member of b class, not pointer fellow member of a superclass.

and since it's not initialized, in example, it's undefined behavior.

just remove declaration of pointer in b.

c++ inheritance abstract-class

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -