virtual

Why vptr is not static?

こ雲淡風輕ζ 提交于 2019-11-29 03:18:08
问题 Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ? Instead of associating the vptr with the object, why not associate it with the class ? 回答1: The runtime class of the object is a property of the object itself. In effect, vptr represents the runtime class, and therefore can't be static . What it

虚继承

。_饼干妹妹 提交于 2019-11-29 01:43:55
  尽管在派生列表中同一个基类只能出现一次,但实际上派生类可以多次继承同一个类。派生类可以通过它的两个直接基类分别继承同一个间接基类,也可以直接继承某个基类,然后通过另一个基类再一次间接继承该类。   举个例子,IO标准库的istream和ostream分别继承了一个共同的名为base_ios的抽象基类。该抽象基类负责保存流的缓冲内容并管理流的条件状态。iostream是另外一个类,它从istream和ostream直接继承而来,可以同时读写流的内容。因为istream和ostream都继承自base_ios,所以iostream继承了base_is两次,一次是通过istream,另一次是通过ostream。   在默认情况下,派生类中含有继承链上每个类对应的子部分。如果某个类在派生过程中出现了多次,则派生类中将包含该类的多个子对象。   这种默认的情况对某些形如iostream的类显然是行不通的。一个iostream对象肯定是希望在同一个缓冲区进行读写操作,也会要求条件状态能同时反映输入和输出操作的情况。假如在iostram对象中真的包含了base_ios的两份拷贝,则上述的共享行为就无法实现而了。    在C++语言中我们通过虚继承(virtual inheritance)的机制解决上述问题。虚继承的目的是令某个类做出声明,承诺愿意共享它的基类。其中,共享的基类子对象成为虚基类

Can we make a class copy constructor virtual in C++

懵懂的女人 提交于 2019-11-29 01:04:50
Can we make a class copy constructor virtual in C++? How to use? No you can't, constructors can't be virtual. C++03 - 12.1 Constructors 4) A constructor shall not be virtual (10.3) or static (9.4). [...] If you need something like this, you can look up the virtual constructor idiom here . No you cannot. Furthermore, the whole concept does not make sense. Virtual functions are functions that are dispatched based on the value of an object (the dynamic type of the object). When a constructor is called, the object does not yet have a value (because it has not yet been constructed). Therefore, no

Conda: Creating a virtual environment

 ̄綄美尐妖づ 提交于 2019-11-29 00:56:46
问题 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 * /Users/fwrenn/anaconda3 test_env /Users/fwrenn/anaconda3/envs/test_env source ~/anaconda3/bin/activate test_env _CONDA_ROOT=/Users/fwrenn/anaconda3: Command not found. Badly placed ()'s. I can't figure out the problem. Searching on here has solutions that say adding lines to your bash_profile,

Out-of-Line Virtual Method

Deadly 提交于 2019-11-29 00:21:42
问题 What exactly is a out-of-line virtual method and why does it affect link times? http://llvm.org/docs/CodingStandards.html says If a class is defined in a header file and has a vtable (either it has virtual methods or it derives from classes with virtual methods), it must always have at least one out-of-line virtual method in the class. Without this, the compiler will copy the vtable and RTTI into every .o file that #includes the header, bloating .o file sizes and increasing link times. 回答1:

多态中的虚析构函数

只愿长相守 提交于 2019-11-29 00:17:38
为什么析构函数要声明成virtual呢? 因为,如果delete一个基类的指针时, 如果它指向的是一个子类的对象,那么析构函数不为虚就会导致无法调用子类析构函数,从而导致资源泄露。 如果一个类要被使用成 多态 的,那么这个 virtual是必须 的。比如: #include <iostream> using namespace std; class Animal { char* ap; public: Animal() { ap = new char; std::cout << "Animal ctor" << std::endl; } virtual void foo() { std::cout << "Animal::foo" << std::endl; } virtual ~Animal() { std::cout << "Animal dtor" << std::endl; delete ap; } }; class Dog : public Animal { char* dp; public: Dog() { dp = new char; std::cout << "Dog ctor" << std::endl; } virtual void foo() { std::cout << "Dog::foo" << std::endl; } virtual ~Dog() {

Why is it allowed to call derived class' private virtual method via pointer of base class?

穿精又带淫゛_ 提交于 2019-11-28 23:31:39
# include <iostream> using namespace std; class A { public: virtual void f() { cout << "A::f()" << endl; } }; class B:public A { private: virtual void f() { cout << "B::f()" << endl; } }; int main() { A *ptr = new B; ptr->f(); return 0; } This code works correctly and prints B::f(). I know how it works, but why is this code allowed? Access control is performed at compile time, not runtime. There's no way in general for the call to f() to know the runtime type of the object pointed to by ptr , so there's no check on the derived class's access specifiers. That's why the call is permitted. As for

When should you call base.Method() in overridden method, and how to mark this when you write code in team?

北城以北 提交于 2019-11-28 22:55:15
When using some framework/api, sometimes it's pretty unclear if you must call base.Method if you override it, for example you can be pretty sure that you should call base.Maethod() when you are overriding event invocater, to propagate the event, in other situations it can be not so clear especially when there is no source code available, and no comments. I wounder how other programmers decide should they call base method or not in this situation, and if you are about to write some framework how to inform other programmers that you expect base method to be called or not in virtual members. Alex

Are abstract methods and pure virtual functions the same thing?

☆樱花仙子☆ 提交于 2019-11-28 21:55:43
问题 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 (not abstract or virtual), but contains a pure virtual function. Is this class then abstract ? 回答1: Yes , they are the same thing. In C++, an abstract method is just another way of describing the characteristics of a pure virtual function. Both

Virtual Printer Driver for Windows

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 20:57:02
can you please help me with the following questions... If I need a virtual printer that will convert a PostScript stream to a different format, do I have to implement a virtual printer from scratch or implement a rendering plug-in? The rendering plug-in seems to support only certain customizations. Also the data invariably goes to the spooler which is not needed in this case. If I implement a virtual printer driver does it completely replace the Microsoft PostScript Driver or the Microsoft Universal Driver? Since my driver is virtual, does it matter if I write a PostScript compliant or a