Questions tagged [virtual]
An extensible or simulated artifact
virtual
3,162
questions
436
votes
12
answers
328k
views
Virtual/pure virtual explained
What exactly does it mean if a function is defined as virtual and is that the same as pure virtual?
281
votes
7
answers
223k
views
Why use 'virtual' for class properties in Entity Framework model definitions?
In the following blog: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
The blog contains the following code sample:
public class Dinner
{
...
194
votes
6
answers
260k
views
Can you write virtual functions / methods in Java?
Is it possible to write virtual methods in Java, as one would do in C++?
Or, is there a proper Java approach which you can implement that produces similar behavior? Could I please have some examples?
186
votes
20
answers
158k
views
C++ static virtual members?
Is it possible in C++ to have a member function that is both static and virtual? Apparently, there isn't a straightforward way to do it (static virtual member(); is a compile error), but is there at ...
106
votes
5
answers
85k
views
navigation property should be virtual - not required in ef core?
As I remember in EF navigation property should be virtual:
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
...
100
votes
5
answers
90k
views
How to do virtual file processing?
So for creating files I use the following:
fileHandle = open('fileName', 'w')
then write the contents to the file, close the file.
In the next step I process the file.
At the end of the program, I ...
96
votes
12
answers
95k
views
What are Virtual Methods?
Why would you declare a method as "virtual".
What is the benefit in using virtual?
94
votes
5
answers
29k
views
CRTP to avoid dynamic polymorphism
How can I use CRTP in C++ to avoid the overhead of virtual member functions?
90
votes
4
answers
81k
views
c++ overloaded virtual function warning by clang?
clang emits a warning when compiling the following code:
struct Base
{
virtual void * get(char* e);
// virtual void * get(char* e, int index);
};
struct Derived: public Base {
virtual ...
88
votes
6
answers
32k
views
Why doesn't polymorphism work without pointers/references?
I did find some questions already on StackOverflow with similar title, but when I read the answers, they were focusing on different parts of the question, which were really specific (e.g. STL/...
85
votes
4
answers
111k
views
The difference between virtual, override, new and sealed override
I'm pretty confused between some concepts of OOP: virtual, override, new and sealed override. Can anyone explain the differences?
I am pretty clear that if the derived class method is to be used, one ...
79
votes
5
answers
85k
views
virtual assignment operator C++
Assignment Operator in C++ can be made virtual. Why is it required? Can we make other operators virtual too?
75
votes
1
answer
77k
views
virtual inheritance [duplicate]
What is the meaning of "virtual" inheritance?
I saw the following code, and didn't understand the meaning of the keyword virtual in the following context:
class A {};
class B : public virtual A;
74
votes
1
answer
24k
views
Difference between target google APIs and target android
I'm developing android with SDK 2.3.3 using Eclipse IDE.
As you all know to run my app, I should generate virtual device.
From 'create new AVD' window, I can see many targets.
There are 2 targets for ...
72
votes
7
answers
16k
views
What are the performance implications of marking methods / properties as virtual?
Question is as stated in the title: What are the performance implications of marking methods / properties as virtual?
Note - I'm assuming the virtual methods will not be overloaded in the common case;...
68
votes
5
answers
60k
views
Are abstract methods and pure virtual functions the same thing?
As far as I know, both abstract methods and pure virtual functions do NOT provide any functionality ... So can we say they're both the same thing ?
Also, suppose a class (not necessarily declared as ...
66
votes
9
answers
64k
views
virtual function call from base class
Say we have:
class Base
{
virtual void f() {g();};
virtual void g(){//Do some Base related code;}
};
class Derived : public Base
{
virtual void f(){Base::f();} override;
...
64
votes
1
answer
36k
views
Comparison : interface methods vs virtual methods vs abstract methods
What are the advantages and disadvantages of each of these?
interface methods
virtual methods
abstract methods
When one should choose what? What are the points one should keep in mind when making ...
61
votes
7
answers
40k
views
C++ virtual function from constructor [duplicate]
Why the following example prints "0" and what must change for it to print "1" as I expected ?
#include <iostream>
struct base {
virtual const int value() const {
return 0;
}
base(...
58
votes
4
answers
263k
views
Conda: Creating a virtual environment
I'm trying to create a virtual environment. I've followed steps from both Conda and Medium.
Everything works fine until I need to source the new environment:
conda info -e
# conda environments:
#
...
57
votes
4
answers
45k
views
Are pure virtual methods allowed within a template class?
Once before, I was certain that you couldn't do this, but the other day I was playing around with some code and it seemed to compile and work. I just want to verify that I am not just getting lucky. ...
56
votes
6
answers
67k
views
C++ Virtual template method
I have an abstract class (I know that it will not compile this way, but it's for comprehension of what I want to do) :
class AbstractComputation {
public:
template <class T> virtual ...
55
votes
1
answer
11k
views
Is final used for optimization in C++?
class A {
public:
virtual void f() = 0;
};
class B : public A {
public:
void f() final override { };
};
int main() {
B* b = new B();
b->f();
}
In this case, is the compiler ...
54
votes
5
answers
49k
views
Calling virtual function from destructor
Is this safe ?
class Derived: public PublicBase, private PrivateBase
{
...
~Derived()
{
FunctionCall();
}
virtual void FunctionCall()
{
PrivateBase::FunctionCall();
...
53
votes
4
answers
30k
views
Requiring virtual function overrides to use override keyword
C++11 added override to ensure that member functions you write that you intend to override base-class virtual functions actually do (or won't compile).
But in a large object hierarchy, sometimes you ...
52
votes
3
answers
39k
views
Making operator<< virtual?
I need to use a virtual << operator. However, when I try to write:
virtual friend ostream & operator<<(ostream& os,const Advertising& add);
I get the compiler error
Error ...
52
votes
8
answers
18k
views
Accessing class members on a NULL pointer
I was experimenting with C++ and found the below code as very strange.
class Foo{
public:
virtual void say_virtual_hi(){
std::cout << "Virtual Hi";
}
void say_hi()
{
...
52
votes
3
answers
55k
views
What does 'has virtual method ... but non-virtual destructor' warning mean during C++ compilation?
#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
virtual int area ()
{ return (0); }
};
class CRectangle: public CPolygon {
...
50
votes
8
answers
45k
views
Overriding vs Virtual
What is the purpose of using the reserved word virtual in front of functions? If I want a child class to override a parent function, I just declare the same function such as void draw(){}.
class ...
47
votes
9
answers
68k
views
How to achieve "virtual template function" in C++
first off: I have read and I know now that a virtual template member function is not (yet?) possible in C++. A workaround would be to make the class a template and then use the template-argument also ...
47
votes
1
answer
12k
views
Out-of-Line Virtual Method
What exactly is a out-of-line virtual method and why does it affect link times?
LLVM Coding Standards says
If a class is defined in a header file and has a vtable (either it has
virtual methods ...
46
votes
4
answers
62k
views
Can we have a virtual static method ? (c++) [duplicate]
Possible Duplicate:
C++ static virtual members?
Can we have a virtual static method (in C++) ? I've tried to compile the following
code :
#include <iostream>
using namespace std;
class ...
45
votes
7
answers
25k
views
Why are private virtual methods illegal in C#?
Coming from a C++ background, this came as a surprise to me. In C++ it's good practice to make virtual functions private. From http://www.gotw.ca/publications/mill18.htm: "Guideline #2: Prefer to ...
41
votes
8
answers
15k
views
Pure virtual functions in C++11
In C++98, the null pointer was represented by the literal 0 (or in fact any constant expression whose value was zero). In C++11, we prefer nullptr instead. But this doesn't work for pure virtual ...
40
votes
12
answers
54k
views
Alternative to c++ static virtual methods
In C++ is not possible to declare a static virtual function, neither cast a non-static function to a C style function pointer.
Now, I have a plain ol' C SDK that uses function pointers heavily.
I ...
40
votes
6
answers
29k
views
Can we make a class copy constructor virtual in C++
Can we make a class copy constructor virtual in C++? How to use?
40
votes
5
answers
17k
views
Print address of virtual member function
I am trying to print the address of a virtual member function.
If I know which class implements the function I can write:
print("address: %p", &A::func);
But I want to do something like this:
A ...
39
votes
2
answers
27k
views
c++ : code explanation for method prototype with const = 0
I hava a class declaration with a piece of code that I do not understand :
class Weapon
{
public:
virtual void attack() const = 0;
};
What does the const = 0 part means ?
39
votes
5
answers
19k
views
How to design a C++ API for binary compatible extensibility
I am designing an API for a C++ library which will be distributed in a dll / shared object. The library contains polymorhic classes with virtual functions. I am concerned that if I expose these ...
37
votes
3
answers
11k
views
How to limit memory of a OS X program? ulimit -v neither -m are working
My programs run out of memory like half of the time I run them. Under Linux I can set a hard limit to the available memory using ulimit -v mem-in-kbytes. Actually, I use ulimit -S -v mem-in-kbytes, so ...
36
votes
6
answers
86k
views
How do I get the complete virtual path of an ASP.NET application
How do I know the the complete virtual path that my application is currently hosted? For example:
http://www.mysite.com/myApp
or
http://www.mysite.com/myApp/mySubApp
I know the application path of ...
36
votes
10
answers
24k
views
C# virtual static method
Why is static virtual impossible? Is C# dependent or just don't have any sense in the OO world?
I know the concept has already been underlined but I did not find a simple answer to the previous ...
35
votes
6
answers
23k
views
Detect virtual keyboard vs. hardware keyboard
I have been thinking about this a while now, and I can't figure a way to deal with it. Is there any way to detect if the user uses a virtual (software) keyboard or a traditional (hardware) keyboard?
...
34
votes
3
answers
18k
views
Mongoose virtual fields included in toJSON by default: schemaOptions.toJSON.virtuals = true; still doesn't include virtual fields by default
I saw in another answer that in order to include the virtual fields you must do like https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/HjrPAP_WXYs
var schemaOptions = {
toJSON: {
...
34
votes
2
answers
7k
views
Call base method at beginning or end of method?
The only similar question to this I found was this and the answer suggested having to use Reflector to find out.
What about in just the majority of the cases? Generally, is the base method called ...
33
votes
5
answers
10k
views
vftable performance penalty vs. switch statement
C++ question here. I have a system where I'm going to have hundreds of mini-subclasses of a given superclass. They all will have a "foo" method that does something. Or... I'm going to have one ...
33
votes
7
answers
58k
views
Writing a Virtual Printer in .NET [closed]
I'm looking to create a virtual printer that passes data to my .NET application. I want to then create an installer that installs both the printer and the .NET application. It would we really nice to ...
32
votes
3
answers
19k
views
Pointers to virtual member functions. How does it work?
Consider the following C++ code:
class A
{
public:
virtual void f()=0;
};
int main()
{
void (A::*f)()=&A::f;
}
If I'd have to guess, I'd say that &A::f in this context would mean ...
31
votes
3
answers
27k
views
Virtual method tables
When discussing sealed classes, the term "virtual function table" is mentioned quite frequently. What exactly is this? I read about a method table a while ago (I don't remember the purpose of the ...
31
votes
3
answers
56k
views
override on non-virtual functions
The C++11 FDIS it says
If a virtual function is marked with the virt-specifier override and does not override a member function of
a base class, the program is ill-formed. [ Example:
struct B {
...