polymorphism

Can C++ compilers optimize repeated virtual function calls on the same pointer? [duplicate]

淺唱寂寞╮ 提交于 2019-12-13 12:53:05
问题 This question already has answers here : Hoisting the dynamic type out of a loop (a.k.a. doing Java the C++ way) (4 answers) Closed 6 years ago . Suppose I have the following code void f(PolymorphicType *p) { for (int i = 0; i < 1000; ++i) { p->virtualMethod(something); } } Will the compiler's generated code dereference p 's vtable entry for virtualMethod 1 or 1000 times? I am using Microsoft's compiler. edit here is the generated assembly for the real-world case I'm looking at. line-

Upcasting and Downcasting

吃可爱长大的小学妹 提交于 2019-12-13 10:40:49
问题 I've just coded this in C# to try and understand what's happening when you downcast/upcast an instance of a class. Class1.cs using System; public class Shapes { protected int _x; protected int _y; public Shapes() { _x = 0; _y = 0; } public virtual void Printing() { Console.WriteLine("In Shapes"); } } public class Circle: Shapes { public override void Printing() { Console.WriteLine("In Circle"); } } public class Square : Shapes { public new void Printing() { Console.WriteLine("In Square"); } }

Calling base function from derived

自作多情 提交于 2019-12-13 10:27:14
问题 I took this tutorial example Inheritance and Polymorphism in C and because I've customized it for my exact requirements it's throwing an error when I try to call base function. Question: Why does it fail in line 8 of employee.c and Possible resolution ((Employee *)self)->super.display(self); // Sementation fault: 11 Download Project main.c #include "person.h" #include "employee.h" #include <stdio.h> int main() { Person* person = newPerson("John Doe"); Employee* employee = newEmployee("Jane

How to access first class method from second class in objective c

江枫思渺然 提交于 2019-12-13 09:55:01
问题 I have written two classes which contains same method (print). I want to access first class print method using second class object. How i can achieve this? Code: @interface classA : NSObject -(void) print; @end @implementation classA -(void) print { NSLog(@"hello"); } @end @interface classB : classA -(void) print; @end @implementation classB -(void) print{ NSLog(@"hey"); } @end Now i created second class object like classB *B = [classB alloc]init]; 回答1: use delegates to access other classes

What do conditionals do to polymorphic objects in C++? (inclusion polymorphism)

空扰寡人 提交于 2019-12-13 08:41:42
问题 I ran into an interesting error and I'm pretty sure it has to do with inclusion polymorphism in the context of conditional statements. The highlights of the example are as follows: ClassParent *parentPointer; //Declare pointer to parent if(condition){ ClassChild1 = mychild; //Declare child1 object parentPointer = *mychild;//Parent pointer points to child } if(!condition){ ClassChild2 = mychild; //Declare child2 parentPointer = *mychild;//Parent pointer points to child2 } cout <<

Parsing XML object to multiple classes with different logic

折月煮酒 提交于 2019-12-13 05:28:57
问题 I have a lot of XML nodes generated by a 3rd party: <node id='8440' name='nodeOne' property_two='7.22' sorting_index='20'/> <node id='8440' name='nodeTwo' property_two='7.22' sorting_index='subItemThree;30;subItemTwenty;50'/> ... Every attribute has the same type and meaning among all nodes except one named sorting_index . Usually, it contains an int indicating, well, object's sorting index. In the first node above sorting index is 20 for an object named "nodeOne". Unfortunately, sometimes

Passing a Derived class reference to a function through a Base Class Pointer

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 05:08:31
问题 I'm pretty sure this is OOP 101 (maybe 102?) but I'm having some trouble understanding how to go about this. I'm trying to use one function in my project to produce different results based on what objet is passed to it. From what I've read today I believe I have the answer but I'm hoping someone here could sure me up a little. //Base Class "A" class A { virtual void DoThis() = 0; //derived classes have their own version }; //Derived Class "B" class B : public A { void DoThis() //Meant to

Java: If-else instanceof extended classes

痞子三分冷 提交于 2019-12-13 04:46:56
问题 I have an abstract class X and some classes who extend this class, call them A , B and C . In some other class Y I have a few methodcalls that depend on the type of the class. The if-else statement looks like this: public class Y implements InterfaceY { public Y(){ } public String doStuff (X x, Boolean flag) { String s = ""; if (x instanceof A) { doStuff((A) x)); } else if (x instanceof B) { doStuff((B) x)); } else if (x instanceof C) { doStuff((C) x, flag); } else { throw new Exeption(); }

How can I use SoapException Method on object of type Exception?

断了今生、忘了曾经 提交于 2019-12-13 04:39:11
问题 I have a Method that get paramater of type - Exception WriteException(Exception ex, int index, string s) { // my code here... } sometimes the method gets an Exception object and sometimes SoapException every time the exeption is of kind SoapException I want print: ex.Detail.InnerText but if ex is of type Exception . so after I recognize the type, how can I do SoapException ex.Detail.InnerText ? 回答1: WriteException(Exception ex, int index, string s) { var soapEx = ex as SoapException; if(null

Overloading << operator with polymorphism

有些话、适合烂在心里 提交于 2019-12-13 03:58:26
问题 I have read some similar questions and answers at SO, but don't really understand the answers there. My apologies, if this is a duplicate. Have a base class like this. class CParam { public: virtual void PrintData(ostream &OutStream) = 0; }; Now I inherit from this: class CUInt32:public CParam { public: void PrintData(ostream &OutStream); } void CUInt32::PrintData(ostream &OutStream) { // Write some data to OutStream Here } I overload operator << for CUInt32 class inline ostream &operator<<