virtual

C++ covariance in parameters

萝らか妹 提交于 2019-12-17 11:43:47
问题 I wanted to know why C++ does not support co-variance in parameters like in example below or if there is a way to achieve it? class base { public: virtual base* func(base * ptr) { return new base(); } }; class derived : public base { public: virtual derived* func(derived * ptr) override { return new derived(); } //not allowed }; 回答1: The return type is permissible since derived inherits from base , but the function parameter can't work - not all base instances will be a derived also. What's

Pointers to virtual member functions. How does it work?

半世苍凉 提交于 2019-12-17 11:00:48
问题 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 "the address of A's implementation of f()", since there is no explicit seperation between pointers to regular member functions and virtual member functions. And since A doesn't implement f(), that would be a compile error. However, it isn't. And not only that. The following code: void (A::*f)()=&A::f; A *a=new B; //

sizeof class with int , function, virtual function in C++?

只愿长相守 提交于 2019-12-17 10:23:30
问题 This is an online C++ test question, which has been done. #include<iostream> using namespace std; class A { }; class B { int i; }; class C { void foo(); }; class D { virtual void foo(); }; class E { int i ; virtual void foo(); }; class F { int i; void foo(); }; class G { void foo(); int i; void foo1(); }; class H { int i ; virtual void foo(); virtual void foo1(); }; int main() { cout <<"sizeof(class A) : " << sizeof(A) << endl ; cout <<"sizeof(class B) adding the member int i : " << sizeof(B)

Why does a virtual function get hidden?

前提是你 提交于 2019-12-17 09:46:19
问题 I have the following classes: class A { public: virtual void f() {} }; class B : public A{ public: void f(int x) {} }; If I say B *b = new B(); b->f(); the compiler says error C2660: 'B::f' : function does not take 0 arguments. Shouldn't the function in B overload it, since it is a virtual function? Do virtual functions get hidden like this? EDIT : I indeed meant to inherit B from A, which shows the same behaviour. 回答1: Assuming you intended B to derive from A : f(int) and f() are different

virtual inheritance [duplicate]

倖福魔咒の 提交于 2019-12-17 07:12:19
问题 This question already has answers here : In C++, what is a virtual base class? (10 answers) Closed 6 years ago . 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; 回答1: Virtual inheritance is used to solve the DDD problem (Dreadful Diamond on Derivation). Look at the following example, where you have two classes that inherit from the same base class:

设计模式之建造者模式(Builder)

孤街醉人 提交于 2019-12-17 06:55:57
一个人活到70岁以上,都会经历这样的几个阶段:婴儿,少年,青年,中年,老年。并且每个人在各个阶段肯定是不一样的呀,我觉得可以说世界上不存在两个人在人生的这5个阶段的生活完全一样,但是活到70岁以上的人,都经历了这几个阶段是肯定的。实际上这是一个比较经典的建造者模式的例子了。 1.初识建造者模式 建造者模式实际上是常用的设计模式。顾名思义,builder的意思是建造者或者建筑工人,谈到建造自然会想到楼房。楼房是千差万别的,楼房的外形、层数、内部房间的数量、房间的装饰等等都不一样,但是对于建造者来说,抽象出来的建筑流程是确定的,往往建筑一座楼房包括下面的步骤:(1)打桩,建立基础(2)建立框架等。建造者模式的本质和建造楼房是一致的:即流程不变,但每个流程实现的具体细节则是经常变化的。建造者模式的好处就是保证了流程不会变化,流程即不会增加、也不会遗漏或者产生流程次序错误,这是非常重要的。我们熟知的楼歪歪事件,官方的解释就是由于先建立楼房后,再建设停车场造成的,这是典型的建造次序错乱。(看来这些人儿不知道建造者模式啊!!!) 我生活的地方有一个菜叫“锅包肉”。基本每个餐馆都有,但是每个餐馆的味道都不一样,原因是什么呢?因为这道菜的作法没有形成标准呗!每个人的作法都不一样,所以味道就不一样了。这实际上通过“建造者模式”让每个馆子的“锅包肉”都一样。同样的KFC做出来的东西

How to limit memory of a OS X program? ulimit -v neither -m are working

风流意气都作罢 提交于 2019-12-17 06:35:21
问题 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 I get a proper memory allocation problem in the program and I can abort. But... ulimit is not working in OSX 10.6. I've tried with -s and -m options, and they are not working. In 2008 there was some discussion about the same issue in MacRumors, but nobody proposed a good alternative. The should

How to do virtual file processing?

送分小仙女□ 提交于 2019-12-17 06:32:53
问题 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 end up with a "physical file" that I need to delete. Is there a way to write a "virtual" file that behaves exactly like a "physical" one (allowing it to be manipulated the same way) but does not exist at the end of the run in Python? 回答1: You might want to consider using a tempfile

How to achieve “virtual template function” in C++

左心房为你撑大大i 提交于 2019-12-17 05:39:56
问题 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 OOP, I find that the below example would not be very "natural" if the class was actually a template. Please note that the code is actually not working, but the gcc-4.3.4 reports: error: templates may not be ‘virtual’ #include <iostream> #include

C++ Virtual template method

烂漫一生 提交于 2019-12-17 03:09:07
问题 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 void setData(std::string id, T data); template <class T> virtual T getData(std::string id); }; class Computation : public AbstractComputation { public: template <class T> void setData(std::string id, T data); template <class T> T getData(std::string id, T data); }; So when I call setData<double>("foodouble", data)