virtual void deleteCandidate(FUN* fun) { Sub* sub = dynamic_cast<Sub*>(fun); delete sub->getParent(); delete sub; } // deleteCandidate
int main() { Base a(2); a.print(); Sub s; s.print(); return 0; }
int main() { Sub s; s.foo(); SuperA * superPtrA = static_cast<SuperA *>(&s); SuperB * superPtrB = static_cast<SuperB *>(&s); Sub * subPtr = static_cast<Sub *>(superPtrA); subPtr = static_cast<Sub *>(superPtrB); return sizeof(Sub); }
void RuntimeEnvironment::RegisterNativeFunction(const rstring &name, int argumentCount, NativeFunction nf) { if (FindSub(name) != -1) return; int idx = AddSub(name); Sub *sub = GetSub(idx); sub->SetArgumentsCount(argumentCount); sub->SetNativeFunction(nf); }
int main(int argc, char const *argv[]) { Super* sp = new Super(); Sub* sb = new Sub(); sp->func(); sp->super_func(); sb->func(); sb->super_func(); // オーバーライドしていない. 継承元の関数をそのまま使う. delete sp; delete sb; return 0; }
int main( int argc, char *argv[]) { Environment::initialize("calculator.log", AXIS2_LOG_LEVEL_TRACE); string endpointUri = "http://localhost:9090/axis2/services/Calculator"; string clientHome = AXIS2_GETENV("WSFCPP_HOME"); if(clientHome.empty()) { cout<<"Please set the WSFCPP_HOME environment variable"<<endl; } CalculatorStub *stub = new CalculatorStub(clientHome, endpointUri); Add *addIn = new Add(); addIn->setA(10); addIn->setB(10); AddResponse *addResponse = stub->add(addIn); if(addResponse) { cout<<"Calculation, 10 + 10 result is "<<addResponse->getAddReturn()<<endl; } Sub *subIn = new Sub(); subIn->setA(20); subIn->setB(10); SubResponse *subResponse = stub->sub(subIn); if(subResponse) { cout<<"Calculation, 20 - 10 result is "<<subResponse->getSubReturn()<<endl; } Mul *mulIn = new Mul(); mulIn->setA(15); mulIn->setB(15); MulResponse *mulResponse = stub->mul(mulIn); if(mulResponse) { cout<<"Calculation, 15 * 15 result is "<<mulResponse->getMulReturn() <<endl; } Div *divIn = new Div(); divIn->setA(100); divIn->setB(10); DivResponse *divResponse = stub->div(divIn); if(divResponse) { cout<<"Calculation, 100/10 result is "<<divResponse->getDivReturn()<<endl; } delete stub; }
Sub* OpenSub::select() { if(list_.empty()) return nullptr; ogdf::ListIterator<Sub*> itMin = list_.begin(); for(ogdf::ListIterator<Sub*> it = list_.begin(); it.valid(); ++it) { Sub *s = *it; if (s->status() == Sub::Dormant) { s->newDormantRound(); if (s->nDormantRounds() < master_->minDormantRounds()) continue; } if (master_->enumerationStrategy(s, *itMin) > 0) itMin = it; } Sub* min = *itMin; list_.del(itMin); updateDualBound(); return min; }
int main(int argc, char** argv) { // 6.1 // Private member can NOT be called by subclass Sub object; object.put(100); // error: ‘void Super::put(int)’ is inaccessible within this context object.put(object.get() + 1); cout << object.get() << endl; // 'storage' is not accessible by declare 'private', should be 'protected' object.print(); // error: ‘int Super::storage’ is protected within this context // 'storage' is still not accessible even as 'protected', should be public // cout << object.storage << endl; // 6.2 /* // subclass pointer CAN be converted to superclass pointer implicitly. Pet *a_pet1 = new Cat("Tom"); Pet *a_pet2 = new Dog("Spike"); Pet *a_pet3 = new Bird("Poly"); a_pet1 -> Run(); a_pet2 -> Run(); a_pet3 -> Run(); // is not allowed here! since a_pet1 is superclass pointer // a_pet1 -> MakeSound(); // error: ‘class Pet’ has no member named ‘MakeSound’ // a_pet2 -> MakeSound(); // error: ‘class Pet’ has no member named ‘MakeSound’ // Use static_cast to explicitly convert superclass pointer to subclass pointer static_cast<Cat *>(a_pet1) -> MakeSound(); static_cast<Dog *>(a_pet2) -> MakeSound(); static_cast<Bird *>(a_pet3) -> Laugh(1); cout << static_cast<Cat *>(a_pet1) -> CatName << endl; cout << static_cast<Dog *>(a_pet2) -> DogName << endl; // static_cast does NOT do any type check. which may cause unintended error // Interesting result: the common base class member is saved and right, the extra target subclass members are just concatenated static_cast<Cat *>(a_pet2) -> MakeSound(); // dog meow static_cast<Dog *>(a_pet1) -> MakeSound(); // cat bark static_cast<Dog *>(a_pet3) -> MakeSound(); // bird MakeSound() NOT Laugh(int) cout << static_cast<Cat *>(a_pet2) -> CatName << endl; cout << static_cast<Dog *>(a_pet1) -> DogName << endl; // Caution cout << static_cast<Dog *>(a_pet3) -> DogName << endl; // No compile time error, debug shows out of bounds // Runtime error: "Segmentation fault; core dumped;" means that you tried to access memory that you do not have access to. // Runtime stops here, no further execution due to out of bounds Dog *a_birddog = static_cast<Dog *>(a_pet3); a_birddog -> MakeSound(); Bird *a_bird = static_cast<Bird *>(a_pet3); a_bird -> Laugh(1); // dynamic_cast can downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid complete object of the target type. // dynamic_cast<Cat *>(a_pet1) -> MakeSound(); // error: cannot dynamic_cast ‘a_pet1’ (of type ‘class Pet*’) to type ‘class Cat*’ (source type is not polymorphic) // dynamic_cast<Dog *>(a_pet2) -> MakeSound(); // error: cannot dynamic_cast ‘a_pet2’ (of type ‘class Pet*’) to type ‘class Dog*’ (source type is not polymorphic) */ // 6.3 /* Cat *a_cat; Dog *a_dog; a_cat = new Cat("Kitty"); a_dog = new Dog("Doggie"); a_cat -> MakeSound(); static_cast<Pet *>(a_cat) -> MakeSound(); a_dog -> MakeSound(); static_cast<Pet *>(a_dog) -> MakeSound(); // dynamic_cast == Implicit conversion dynamic_cast<Pet *>(a_dog) -> MakeSound(); // the Pet says: Shh! Shh! // Implicit conversion Pet *a_pet = a_cat; a_pet -> MakeSound(); // the Pet says: Shh! Shh! */ // Assessment: 70% // Q1: Compilation fails /* Y y; cout << y.v; // error: 'int X::v' is private */ // Q2: 0 is wrong, Compilation fails is right /* Y *y = new Y(); // error: 'Y::Y()' is private // Explicit Constructors have to be public // Y *y = new Y; // error: 'Y::Y()' is private cout << y->v; // error: 'int X::v' is protected delete y; */ // Q3: Compilation fails /* Z *z = new Z(); Y *y = new Y(); z = y; // error: cannot convert 'Y*' to 'Z*' in assignment cout << (z == y); // error: comparison between distinct pointer types 'Z*' and 'Y*' lacks a cast */ // Q4: Y /* X *x = new Y(); static_cast<Y *>(x) -> shout(); */ // Q5: X is wrong, Z is right // If base is virtual, all subclasses are virtual as well, no matter how many inheritance /* Y *y = new Z(); dynamic_cast<X *>(y) -> shout(); // Z y -> shout(); // Z */ // Q6: 1 /* A a; Do(a); a.inc(); cout << a.val; */ // Q7: 2 /* A a; Do(&a); cout << a.inc(); */ // Q8: 22 /* A a,b = a; cout << a.get() << b.get(); */ // Q9: Compilation fails is wrong, 33 is right /* A a(2); // There is an explicit copy constructor, no error here A b(a); cout << a.get() << b.get(); */ // Q10: 1 /* A a; B b; a.set(1); b.kill(a); cout << a.get(); */ // Assessment Retake: 100% // Q4: X /* Y *y = new Z(); dynamic_cast<X *>(y) -> shout(); */ // Q6: 220 // If no override, use the parent one, even it's virtual A *a = new C();; Do(a); return 0; }
int main_2() { Sub sub; sub.Hello(); return 0; }
void GfxBody::_updateRenderQueue(Ogre::RenderQueue* queue) { bool shadow_cast = ogre_sm->_getCurrentRenderStage() == Ogre::SceneManager::IRS_RENDER_TO_TEXTURE; if (!enabled || fade < 0.000001 || firstPerson) return; bool do_wireframe = (wireframe || gfx_option(GFX_WIREFRAME)); bool do_regular = !(do_wireframe && gfx_option(GFX_WIREFRAME_SOLID)); // fade is used by both shadow_cast and regular pass // we could potentially move this out of the frame loop if it affects performance for (unsigned i=0 ; i<subList.size() ; ++i) { Sub *sub = subList[i]; sub->setCustomParameter(0, Ogre::Vector4(fade,0,0,0)); } if (shadow_cast) { // Add each visible Sub to the queue for (unsigned i=0 ; i<subList.size() ; ++i) { Sub *sub = subList[i]; GfxMaterial *m = sub->material; if (!sub->getCastShadows()) continue; if (!m->getCastShadows()) continue; renderMaterial = m->regularMat; queue->addRenderable(sub, 0, 0); } } else { // Add each visible Sub to the queue for (unsigned i=0 ; i<subList.size() ; ++i) { Sub *sub = subList[i]; // car paint for (int k=0 ; k<4 ; ++k) { const GfxPaintColour &c = colours[k]; sub->setCustomParameter(2*k+1, Ogre::Vector4(c.diff.x, c.diff.y, c.diff.z, c.met)); sub->setCustomParameter(2*k+2, Ogre::Vector4(c.spec.x, c.spec.y, c.spec.z, 0.0f)); } GfxMaterial *m = sub->material; if (do_regular) { /* TODO: include other criteria to pick a specific Ogre::Material: * bones: 1 2 3 4 * vertex colours: false/true * vertex alpha: false/true * Fading: false/true * World: false/true */ if (fade < 1 && m->getSceneBlend() != GFX_MATERIAL_OPAQUE) { renderMaterial = m->fadingMat; } else { renderMaterial = m->regularMat; } int queue_group = RQ_GBUFFER_OPAQUE; switch (m->getSceneBlend()) { case GFX_MATERIAL_OPAQUE: queue_group = RQ_GBUFFER_OPAQUE; break; case GFX_MATERIAL_ALPHA: queue_group = RQ_FORWARD_ALPHA; break; case GFX_MATERIAL_ALPHA_DEPTH: queue_group = RQ_FORWARD_ALPHA_DEPTH; break; } queue->addRenderable(sub, queue_group, 0); if (!m->emissiveMat.isNull() && sub->emissiveEnabled) { renderMaterial = m->emissiveMat; switch (m->getSceneBlend()) { case GFX_MATERIAL_OPAQUE: queue_group = RQ_FORWARD_OPAQUE_EMISSIVE; break; case GFX_MATERIAL_ALPHA: queue_group = RQ_FORWARD_ALPHA_EMISSIVE; break; case GFX_MATERIAL_ALPHA_DEPTH: queue_group = RQ_FORWARD_ALPHA_DEPTH_EMISSIVE; break; } queue->addRenderable(sub, queue_group, 0); } } if (do_wireframe) { renderMaterial = m->wireframeMat; queue->addRenderable(sub, RQ_FORWARD_ALPHA, 0); } } } renderMaterial.setNull(); }
int main(){ Op two(2.0); Base* n2 = &two; Op one1(1.0); Base* n11 = &one1; Op fourteen(14.0); Base* n14 = &fourteen; Op three(3.0); Op seven(7.0); Base* n7 = &seven; Op four(4.0); Op six(6.0); Base* n6 = &six; Base* n4 = &four; Sqr Root; Div divide; Base* di = ÷ Add addition; Base* ad = &addition; Mult multi; Base* mu = &multi; Sub subtr; Base* su = &subtr; // Root.addright(n2); Root.addleft(mu); addition.addleft(n11); addition.addright(n14); divide.addleft(n6); divide.addright(su); subtr.addleft(n4); subtr.addright(n2); multi.addleft(ad); multi.addright(di); cout << Root.evaluate() << endl; cout << "Vector test with selection sort: " << endl; Vector_C vcontainer; Ssort selectionS; vcontainer.add_element(n6); vcontainer.add_element(di); vcontainer.add_element(mu); vcontainer.add_element(n11); vcontainer.add_element(n7); vcontainer.add_element(ad); vcontainer.set_sort_function(&selectionS); vcontainer.sort(); vcontainer.print(); cout << "Vector test with bubble sort: " << endl; Vector_C vcontainer2; Bsort BubbleS; vcontainer2.add_element(n6); vcontainer2.add_element(di); vcontainer2.add_element(mu); vcontainer2.add_element(n11); vcontainer2.add_element(n7); vcontainer2.add_element(ad); vcontainer2.set_sort_function(&BubbleS); vcontainer2.sort(); vcontainer2.print(); cout << "List test with selection sort: " << endl; List_C lcontainer; lcontainer.add_element(n6); lcontainer.add_element(di); lcontainer.add_element(n14); lcontainer.add_element(n11); lcontainer.add_element(n7); lcontainer.add_element(ad); lcontainer.add_element(mu); lcontainer.set_sort_function(&selectionS); lcontainer.sort(); lcontainer.print(); cout << "List test with bubble sort: " << endl; List_C lcontainer2; lcontainer2.add_element(n6); lcontainer2.add_element(di); lcontainer2.add_element(mu); lcontainer2.add_element(n14); lcontainer2.add_element(n11); lcontainer2.add_element(n7); lcontainer2.add_element(ad); lcontainer2.set_sort_function(&BubbleS); lcontainer2.sort(); lcontainer2.print(); return 0; }