virtual-functions

virtual assignment operator C++

Assignment Operator in C++ can be made virtual. Why is it required? Can we make other operators virtual too? ...

Resolving the "most derived" method in a virtual override

I have a simple base class and derived class: class Base { public virtual void Write(int value) { Console.WriteLine("base int: {0}", value); } public virtual void Write(string value) { Console.WriteLine("base string: {0}", value); } } class Derived : Base { public override void Write(int val...

Speeding up virtual function calls in gcc

Profiling my C++ code with gprof, I discovered that a significant portion of my time is spent calling one virtual method over and over. The method itself is short and could probably be inlined if it wasn't virtual. What are some ways I could speed this up short of rewriting it all to not be virtual? ...

does assignment operator work with different types of objects?

class A { public: void operator=(const B &in); private: int a; }; class B { private: int c; } sorry. there happened an error. is assignment operator valid ? or is there any way to achieve this? [There is no relation between A and B class.] void A::operator=(const B& in) { a = in.c; } Thanks a lot. ...

Why might my virtual function call be failing?

Update: This issue is caused by bad memory usage, see solution at the bottom. Here's some semi-pseudo code: class ClassA { public: virtual void VirtualFunction(); void SomeFunction(); } class ClassB : public ClassA { public: void VirtualFunction(); } void ClassA::VirtualFunction() { // Intentionally empty (code sm...

Why C# implements methods as non-virtual by default?

Unlike Java, why C# treats methods as non-virtual functions by default? Is it more likely to be a performance issue rather than other possible outcomes? I remind reading a paragraph from Anders Hejlsberg about the several advantages the existing architecture is bringing out. But, what about side effects? Is it really a good trade-off to...

Why all java methods are implicitly overridable?

In C++, I have to explicitly specify 'virtual' keyword to make a member function 'overridable', as there involves an overhead of creating virtual tables and vpointers, when a member function is made overridable (so every member function is implicitly not overridable for performance reasons). It also allows a member function to be hidde...

Command pattern without virtual functions (C++)

For performance reasons, I am using the the Curiously Reoccuring Template Pattern to avoid virtual functions. I have lots of small commands which execute millions of times. I am trying to fit this into the Command Pattern. I want to add tons of commands to a queue, and then iterate through them executing each one by one. Each Command...

Public virtual function derived private in C++

Hello All, I was trying to figure out what happens when a derived class declares a virtual function as private. The following is the program that I wrote #include <iostream> using namespace std; class A { public: virtual void func() { cout<<"A::func called"<<endl; } private: }; class B:public A { public:...

Practical usage of virtual functions in c#

What 's the practical usage of virtual functions in c#? ...

Why would I want to use a pure virtual function in C++?

I'm learning about C++ in a class right now and I don't quite grok pure virtual functions. I understand that they are later outlined in a derived class, but why would you want to declare it as equal to 0 if you are just going to define it in the derived class? ...

Covariant virtual functions and smart pointers

In C++, a subclass can specify a different return type when overriding a virtual function, as long as the return type is a subclass of the original return type (And both are returned as pointers/references). Is it possible to expand this feature to smart pointers as well? (Assuming a smart pointer is some template class) To illustrate:...

Correct Implementation of Virtual Functions in PHP?

Hi guys, at my working place (php only) we have a base class for database abstraction. When you want to add a new database table to the base layer, you have to create a subclass of this base class and override some methods to define individual behaviour for using this table. The normal behaviour should stay the same. Now I have seen ma...

sizeof on a class inheriting from a base class with a virtual function

For the following code fragment. /*This program demonstartes how a virtual table pointer * adds to a size of a class*/ class A{ }; class X{ public: void doNothing(){} private: char a; }; class Z:public X { public: void doNothing(){} private: char z; }; class Y{ public: ...

Objective-C protocols mimicking 'virtual-functions' yield compiler warnings?

In Objective-C, I'd like to force derived classes to implement a given interface without providing a default implementation (an implementation in the parent class). I understand that Protocols can be used for this, and I believe I understand how to use Protocols, but I'm apparently missing something... I have defined class Parent, and ...

How can I get polymorphic behavior in a C++ constructor?

I have a base class that I want to look like this: class B { // should look like: int I() { return someConst; } virtual int I() = 0; public B() { something(I()); } } The point being to force deriving classes to override I and force it to be called when each object is constructed. This gets used to do some bookkeeping and I...

Template class + virtual function = must implement?

This code: template <typename T> struct A { T t; void DoSomething() { t.SomeFunction(); } }; struct B { }; A<B> a; is easily compiled without any complaints, as long as I never call a.DoSomething(). However, if I define DoSomething as a virtual function, I will get a compile error saying that B doesn'...

c# inherited class initialization

so with this class i have public class Options { public bool show { get; set; } public int lineWidth { get; set; } public bool fill { get; set; } public Color fillColour { get; set; } public string options { get; set; } public virtual void createOptions() { options...

How can a C++ base class determine at runtime if a method has been overridden?

The sample method below is intended to detect whether or not it has been overridden in a derived class. The error I get from MSVC implies that it is simply wrong to try to get the function pointer to a "bound" member, but I see no logical reason why this should be a problem (after all, it will be in this->vtable). Is there any non-hacky ...

Trouble understanding C++ `virtual`

I'm having trouble understanding what the purpose of the virtual keyword in C++. I know C and Java very well but I'm new to C++ From wikipedia In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same ...