object-slicing

Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse) from Deconstructor

醉酒当歌 提交于 2019-12-02 19:24:35
问题 I am quite lost right now. I made a vector class. Everything works how I would like it to work, until the end. When the destructor is called I get an error message: Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse). I've seen quite a few questions like this one on SO, but what I have tried hasn't worked. part of .h. private: int* _myArray; int _size; int _capacity; #include "File.h" const string RETURN_CARRIAGE_STR = "\n"; const string SIZE_STR = "Size "; const string CAPACITY_STR

Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse) from Deconstructor

十年热恋 提交于 2019-12-02 09:08:34
I am quite lost right now. I made a vector class. Everything works how I would like it to work, until the end. When the destructor is called I get an error message: Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse). I've seen quite a few questions like this one on SO, but what I have tried hasn't worked. part of .h. private: int* _myArray; int _size; int _capacity; #include "File.h" const string RETURN_CARRIAGE_STR = "\n"; const string SIZE_STR = "Size "; const string CAPACITY_STR = "Capacity "; const int INITIAL_CAPACITY = 2; int main(void) { cout << "\nCreating a vector Sam of

How can I make the method of child be called: virtual keyword not working?

♀尐吖头ヾ 提交于 2019-12-01 21:33:25
The following is my code, #include<iostream> #include<string> using namespace std; class TestClass { public: virtual void test(string st1, string st2); }; class ExtendedTest: public TestClass { public: virtual void test(string st1, string st2); }; void TestClass::test(string st1, string st2="st2") { cout << st1 << endl; cout << st2 << endl; } void ExtendedTest::test(string st1, string st2="st2") { cout << "Extended: " << st1 << endl; cout << "Extended: " << st2 << endl; } void pass(TestClass t) { t.test("abc","def"); } int main() { ExtendedTest et; pass(et); return 0; } When I run the code,

Is object slicing ever useful?

女生的网名这么多〃 提交于 2019-11-30 17:27:28
Object slicing happens when we assign or copy an object of derived class to an object of its base class, losing the derived part of it in the process. It has been explained in more depth here: What is the slicing problem in C++? . (Myself, I don't see it as a problem , rather a natural consequence of language's value semantics, but that's not the point of this question.) What I wonder is: are there ever situations where you'd use it purposedly? A situtation where it is the "right tool for the job"? Sure, it can be useful when wanting to drop the derived portion of class, perhaps to drop

Why do virtual functions need to be passed with a pointer and not by value(of the object)?

拜拜、爱过 提交于 2019-11-30 13:54:07
I think I understand the concept of virtual methods and vtables, but I don't understand why there is a difference between passing the object as a pointer(or reference) and passing it by value (which kind of scraps the vtable or something?) Why would something like this work: Material* m = new Texture; poly->setMaterial(m); // methods from Texture are called if I keep carrying the pointer around And not this?: Material m = Texture(); poly->setMaterial(m); // methods from Material are called if I pass the value around Because if you pass by value, then object slicing will occur, and runtime

References and Object Slicing

江枫思渺然 提交于 2019-11-30 08:11:48
问题 I don't have my Effective C++ with me and this is bugging me so much that I have to ask for my own sanity. Given class Foo : public Bar{} void MyFunc(Bar &_input); If I pass in a Foo , am I tangling with the slicing problem or have I avoided it? 回答1: Not a problem, because you're passing in a reference. You're not creating a new object, just letting MyFunc access the original object. 回答2: Since you are passing the reference - no , unless you later assign to an instance of Bar . 回答3: Slicing

Is object slicing ever useful?

旧时模样 提交于 2019-11-30 01:11:14
问题 Object slicing happens when we assign or copy an object of derived class to an object of its base class, losing the derived part of it in the process. It has been explained in more depth here: What is the slicing problem in C++?. (Myself, I don't see it as a problem , rather a natural consequence of language's value semantics, but that's not the point of this question.) What I wonder is: are there ever situations where you'd use it purposedly? A situtation where it is the "right tool for the

Preventing slicing in copy constructor

亡梦爱人 提交于 2019-11-29 14:35:55
I want to copy a vector of type Foo objects but the objects can be several different derived types of Foo. I can't figure out how to copy without slicing. Here's my toy code #include "stdafx.h" #include <memory> #include <vector> #include <string> #include <iostream> class Foo { public: Foo() { m_x = "abc"; } Foo( const Foo &other ) { m_x = other.m_x; } virtual std::string ToString() { return m_x; } std::string m_x; }; class FooDerivedA : public Foo { public: FooDerivedA() : Foo() { m_y = 123; } std::string ToString() { return m_x + ", " + std::to_string( m_y ); } int m_y; }; class FooDerivedB

C++ slicing causing leak / undefined behavior / crash

此生再无相见时 提交于 2019-11-29 12:50:29
Is there any example of the C++ object slicing effect which can cause undefined behavior, memory leak or crash in an otherwise correct set of code? For example when class A and B (inherited from A ) are correct and sound, but calling a void f(A a) demonstrably causes nasty things. It is needed for forming a test question. The goal is to know if the participant is aware of the slicing phenomenon or not, using an example code snippet whose correctness must not be a matter of opinion. If A is indeed "correct and sound", then slicing (copying the base sub-object) is well-defined and will not cause

overriding virtual function return type differs and is not covariant

时光毁灭记忆、已成空白 提交于 2019-11-29 06:48:36
Ah, SO came back just in time. I am getting a strange error: 'B::blah': overriding virtual function return type differs and is not covariant from 'A::blah' Here is the code causing the problem: class A { public: class Inner { }; virtual Inner blah() = 0; }; class B : public A { public: class Inner2 : public Inner { }; Inner2 blah() { return Inner2(); } }; I looked up the error, and according to a page I found on the Microsoft website , one of the ways types can be covariant is if: the class in the return type of B::f is the same class as the class in the return type of D::f or, is an