c++ - Abstract base member variable in base class -
i want specify interface requires abstract class have type member variable.
i'll try replicate situation here:
class blob { int data[32]; }; class worker { string name; abstract void workon(blob&) = 0; } class abstract { vector<shared_ptr<w>> workerlist; blob allthestuff; abstract void somethingelse() = 0; void doallthework() { (w : workerlist) { w->workon(allthestuff); } } }; class b_blob : public blob { int moredata[4096]; }; class bulbasaurtrainingcamp : public abstract { b_blob allthestuff; void somethingelse() {} // implemented // class accept bulbasaurs workerlist }; class bulbasaur : worker { bulbasaur(): name("fushigidane") {} void workon(blob& b) { // bulbasaurs cover *all* workspace crap (int i=0; i<sizeof(b.data[0])/sizeof(b.data); ++i) { b.data[i] = *((int*)&("crap")); } (i=0; i<sizeof(b.moredata[0])/sizeof(b.moredata); ++i) { b.moredata[i] = *((int*)&("crap")); } }
here, abstract bas class has blob
, instance of bulbasaurtrainingcamp
has derived b_blob
. appears since gave same name, compiler accepts it.
is there name this? want know behavior when this. have overridden blob
b_blob
?
i not sure whether there inaccessible base blob
instance hanging around inside of bulbasaurtrainingcamp
. expectation each bulbasaur
write 16512 (not 16384) bytes of crap
across 2 member variables of b_blob
. hoping c++ appears sensible thing. it's case of, "it compiles think should happy, i'm still not totally sure it's doing think should doing".
#include<iostream> #include<vector> using namespace std; int main() { class base { public: int samename; base(int x):samename(x){} }; class derived : public base { public: int diffname; int samename; derived(int x,int i,int j):base(x),diffname(i),samename(j){} }; derived example(1,2,3); cout<<example.samename<<endl; cout<<example.diffname<<endl; cout<<example.base::samename<<endl; }
the result 3 2 1. hope example helpful.
Comments
Post a Comment