Questions tagged [virtual]

An extensible or simulated artifact

Virtual memory: Giving the appearance of having greater system memory by paging infrequently used portions of memory out to disk, and reloading them from disk only when referenced.

Virtual hard disk: A software simulation of a hard disk.

Virtual method: a function that can be overridden in descendant classes to extend or replace existing functionality. This is a key aspect of polymorphism.

Virtual reality: A simulation of experience achieved by coordinating visual, audible, and tactile stimuli

3155 questions
67
votes
5 answers

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 abstract) contains a number of implemented methods…
Ahmad
  • 12,886
  • 30
  • 93
  • 146
65
votes
9 answers

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; virtual void g(){/*Do some Derived related code*/}…
Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
63
votes
1 answer

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 this decision?
Nawaz
  • 353,942
  • 115
  • 666
  • 851
60
votes
7 answers

C++ virtual function from constructor

Why the following example prints "0" and what must change for it to print "1" as I expected ? #include struct base { virtual const int value() const { return 0; } base() { std::cout << value() << std::endl; } …
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
56
votes
4 answers

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. Can a template class have a pure virtual function…
Anthony
  • 9,451
  • 9
  • 45
  • 72
56
votes
6 answers

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 virtual void setData(std::string id, T data); template…
Vincent
  • 57,703
  • 61
  • 205
  • 388
55
votes
1 answer

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 required to still do the v-table lookup for b->f();, or…
tmlen
  • 8,533
  • 5
  • 31
  • 84
52
votes
8 answers

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() { std::cout << "Hi"; } }; int main(int argc,…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
52
votes
4 answers

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 could accidentally end up writing a member…
Barry
  • 286,269
  • 29
  • 621
  • 977
51
votes
3 answers

What does 'has virtual method ... but non-virtual destructor' warning mean during C++ compilation?

#include using namespace std; class CPolygon { protected: int width, height; public: virtual int area () { return (0); } }; class CRectangle: public CPolygon { public: int area () { return (width * height); } …
qazwsx
  • 25,536
  • 30
  • 72
  • 106
51
votes
3 answers

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 1 error C2575: 'operator <<' : only member functions and bases can be …
inna karpasas
  • 601
  • 2
  • 7
  • 6
51
votes
5 answers

Calling virtual function from destructor

Is this safe ? class Derived: public PublicBase, private PrivateBase { ... ~Derived() { FunctionCall(); } virtual void FunctionCall() { PrivateBase::FunctionCall(); } } class PublicBase { virtual…
cprogrammer
  • 5,503
  • 3
  • 36
  • 56
50
votes
8 answers

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 Parent { public: void say() { std::cout…
anonymous
  • 511
  • 1
  • 4
  • 4
47
votes
9 answers

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 in the member-function. But in the context of…
Shadow
  • 1,042
  • 2
  • 15
  • 23
47
votes
3 answers

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: # base * …
Forrest
  • 548
  • 1
  • 4
  • 10