Consider the following C code:
<code class="cpp">class Instruction { public: virtual void execute() { } }; class Add: public Instruction { private: int a; int b; int c; public: Add(int x, int y, int z) {a=x;b=y;c=z;} void execute() { a = b + c; } };</code>
In a separate class:
<code class="cpp">void some_method() { vector<Instruction> v; Instruction* i = new Add(1,2,3) v.push_back(*i); }</code>
And in another class:
<code class="cpp">void some_other_method() { Instruction ins = v.back(); ins.execute(); }</code>
These classes share the Instruction vector. However, we encounter a concern regarding the execute function. Will it retain its Add type?
Unfortunately, it will not. vector
To resolve this issue, consider using vector
The above is the detailed content of Does Object Slicing Occur When Using a `vector` in C ?. For more information, please follow other related articles on the PHP Chinese website!