abstract - How to "hide" a virtual method? (C++) -



abstract - How to "hide" a virtual method? (C++) -

there post quite similar title here, understood actual problem there different.

i know if possible forcefulness user of class wrote override method @ same time should not possible phone call method (but called within class).

for example, if want following:

class abstrdatasource { private: int index; protected: int currentdata; public: int getdata(){return currentdata;} void loaddata(int i){ // check valid index here if (index != i){doload(i);} this->index = i; } virtual void doload(int i)=0; };

in loaddata() can check index in valid range , bookkeeping, while actual loading has supplied user via overriding doload(). because pure virtual methods part of interface, public, how forcefulness implementation of doload() visible own class?

my thought hide object in wrapper:

class datasupplier { public: datasupplier(abstrdatasource* s) : source(s){} void loaddata(int i){source->loaddata(i);} int getdata(){return source->getdata();} private: abstrdatasource* source; };

and instead of using abstract class utilize wrapper:

int somecalculation(datasupplier* a,datasupplier* b){ homecoming a->getdata() + b->getdata(); }

however, not help. lets sec person provides implementation of abstract class:

class impldatasource : public abstrdatasource{ public: void doload(int i){this->currentdata = i;} };

then 3rd person still has access doload():

void main(){ abstrdatasource* ads = new impldatasource(); datasupplier* ds1 = new datasupplier(ads); datasupplier* ds2 = new datasupplier(ads); ads->doload(10); // <- how avoid ?? ds1->loaddata(12); ds2->loaddata(12); somecalculations(ds1,ds2); }

maybe there way accomplish using access specifiers...?

edit: got helpful answers, think did not state question clear enough. long impldatasource declares doload() protected or private, fine. however, looking @ abstrdatasource, there no hint implementation of doload() should private (even if abstract doload protected or private actual implementation can have access). know if possible somehow "enforce" implementation of abstract doload() private. maybe thinking complicated , easiest way add together comment documentation of abstract method ("implement private or may fail").

make method doload() protected, can called within superclass, , overloads (and hides) pure virtual method of base of operations class.

c++ abstract

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -