I profiled my code and found out that my class, which implements Comparable<T>, spends 8x more cpu time in
compareTo(Object)
than in
compareTo(T)
I assume that the slowdown is because of virtual table lookup for this method.
Is there a way to force static invocation of the function? (like in non virtual C++ methods)
I still wa...
Suppose I have two C++ classes:
class A
{
public:
A() { fn(); }
virtual void fn() { _n = 1; }
int getn() { return _n; }
protected:
int _n;
};
class B : public A
{
public:
B() : A() {}
virtual void fn() { _n = 2; }
};
If I write the following code:
main()
{
B b;
int n = b.getn();
}
One might expect that n is set ...
I have an abstract class in a library. I'm trying to make it as easy as possible to properly implement a derivation of this class. The trouble is that I need to initialize the object in a three-step process: grab a file, do a few intermediate steps, and then work with the file. The first and last step are particular to the derived class....
If I have one base class and I derive 10 different concrete derived classes from it then will each and every concrete derived class have a different vtable?
...
I've been having a discussion with my coworkers as to whether to prefix overridden methods with the virtual keyword, or only at the originating base class.
I tend to prefix all virtual methods (that is, methods involving a vtable lookup) with the virtual keyword. My rationale is threefold:
Given that C++ lacks an override
keyword, the...
hi guys, i have the following classes:
class A {
protected:
A *inner;
public:
....
virtual void doSomething() = 0;
....
}
class B: public A {
...
void doSomething() {
if(inner != NULL)
inner->doSomething();
}
...
}
When I use inner->doSomething() I get a segmentation fault.
What should I...
Ola the 'flow!
I have been using Moq recently in my development process and I like what I am able to achieve.
However, I find myself making my methods (and properties for the mostpart) virtual so that I can replace them with a mock in my tests.
Other than "you are making all your methods and properties overrideable", what real world r...
First let's establish this.
I have
public abstract class Foo
{
public static void StaticMethod()
{
}
}
public class Bar : Foo
{
}
is it valid to call
Bar.StaticMethod();
???
If so, let's expand previous example:
public abstract class Foo
{
public static void StaticMethod()
{
}
public abstract void VirtualMethod();
...
I have a class which doesn't currently need to be thread-safe, but in future we might want to make a thread-safe version. The way I see it, I can either make it thread-safe now by putting locks around the relevant functions, or I can make them virtual now and put locks around them in overrides in a descendent class later on. That is, t...
This is an extension for this question asked an hour ago.
We cannot modify the access modifiers, when overriding a virtual method in derived class. Consider Control class in System.Web.UI namespace
public class Control : IComponent, IDisposable,...
{
protected internal virtual void CreateChildControls()
{ }
.
.
}
Now Con...
Consider this simple Java class:
class MyClass {
public void bar(MyClass c) {
c.foo();
}
}
I want to discuss what happens on the line c.foo().
Original, Misleading Question
Note: Not all of this actually happens with each individual invokevirtual opcode. Hint: If you want to understand Java method invocation, don't read just...
Hi,
There is a thing I do not understand well: when virtual method is called, the base method is called as well?
Because when I use public override WinForm OnPaint method, in its body base.OnPaint(e) is called. I do not understand it, I thought virtual methods overrides the original one.
If it is not usually called, why it is called in ...
I can't do this
interface InterfaceA
{
void MethodA();
}
class ClassA : InterfaceA
{
virtual void InterfaceA.MethodA()
// Error: The modifier 'virtual' is not valid for this item
{
}
}
Where the following works
class ClassA : InterfaceA
{
public virtual void MethodA()
{
}
}
Why? How to circumvent th...
I have a class designed to do import/export of data in one of a few different formats. Each format should have exactly the same interface, so I'm implementing it as a base class with a bunch of virtual methods and a derived class for each specific format:
#ifndef _IMPORTEXPORT_H_
#define _IMPORTEXPORT_H_
#include "stdio.h"
enum EXPORT...
I create a parent class that calls it's own virtual member. But this virtual member is overridden by child class.
class Parent {
public:
void doSomething() {
doVirtual();
}
protected:
virtual void doVirtual() {}
};
class Child : public Parent {
protected:
virtual void doVirtual() {}
};
Parent *c = new Child();
...
I have a set of classes, where each class represents a diferent type of a database field. For example, a very basic subset of it would be:
public abstract class DbObject { internal DbObject() { } }
public class DbInteger : DbObject
{
public int Data { get; set; }
public DbInteger(int data) { this.Data = data; }
}
public class ...
Just starting to use Java. I find a lot of similarities with .NET, but I see that all methods in Java are virtual by default.
So the question is what can I do to make them non virtual ? Is the final keyword the one and right solution ?
...
Consider the following code (it's a little long, but hopefully you can follow):
class A
{
}
class B : A
{
}
class C
{
public virtual void Foo(B b)
{
Console.WriteLine("base.Foo(B)");
}
}
class D: C
{
public override void Foo(B b)
{
Console.WriteLine("Foo(B)");
}
public void Foo(A a)
{
...
I want to know if i don't put override key word before the method in derived class method m1(), then what is the default value before this, or will it throw a compile time error?
class A { virtual void m1(){} }
class B: A { void m1(){} }
...