derived-class

copy & swap in base and derived class

倖福魔咒の 提交于 2021-02-07 03:15:59
问题 I recently read about copy & swap and am now trying to implement the ctors in a base and derived class. I have the four constructors in both my base and derived class, however I am unsure how to implement the assignment operator of the derived class. explicit Base(int i) : m_i{i} {} Base(const Base & other) : m_i{other.m_i} Base(Base && other) : Base(0) { swap(*this, other); } Base & operator=(Base other) { swap(*this, other); return *this; } friend void swap(Base & a, Base & b) noexcept {

copy & swap in base and derived class

南楼画角 提交于 2021-02-07 03:14:39
问题 I recently read about copy & swap and am now trying to implement the ctors in a base and derived class. I have the four constructors in both my base and derived class, however I am unsure how to implement the assignment operator of the derived class. explicit Base(int i) : m_i{i} {} Base(const Base & other) : m_i{other.m_i} Base(Base && other) : Base(0) { swap(*this, other); } Base & operator=(Base other) { swap(*this, other); return *this; } friend void swap(Base & a, Base & b) noexcept {

copy & swap in base and derived class

自闭症网瘾萝莉.ら 提交于 2021-02-07 03:11:18
问题 I recently read about copy & swap and am now trying to implement the ctors in a base and derived class. I have the four constructors in both my base and derived class, however I am unsure how to implement the assignment operator of the derived class. explicit Base(int i) : m_i{i} {} Base(const Base & other) : m_i{other.m_i} Base(Base && other) : Base(0) { swap(*this, other); } Base & operator=(Base other) { swap(*this, other); return *this; } friend void swap(Base & a, Base & b) noexcept {

copy & swap in base and derived class

无人久伴 提交于 2021-02-07 03:07:51
问题 I recently read about copy & swap and am now trying to implement the ctors in a base and derived class. I have the four constructors in both my base and derived class, however I am unsure how to implement the assignment operator of the derived class. explicit Base(int i) : m_i{i} {} Base(const Base & other) : m_i{other.m_i} Base(Base && other) : Base(0) { swap(*this, other); } Base & operator=(Base other) { swap(*this, other); return *this; } friend void swap(Base & a, Base & b) noexcept {

Passing derived class to base function

[亡魂溺海] 提交于 2021-01-28 17:41:51
问题 I'm having trouble passing a derived class to a function which accepts the base class as argument. The base class is consists of "obstacles" which are to be placed on a "board" void Board::setvalue(int length, int width, Obstacle& obstacle); However, this causes the compiler to give the "no known conversion for argument..."-error. Reading up around the site i found that i should be passing the derived object as a const, this however causes problems because a const can't be assigned to the

Passing derived class to base function

时光怂恿深爱的人放手 提交于 2021-01-28 17:40:56
问题 I'm having trouble passing a derived class to a function which accepts the base class as argument. The base class is consists of "obstacles" which are to be placed on a "board" void Board::setvalue(int length, int width, Obstacle& obstacle); However, this causes the compiler to give the "no known conversion for argument..."-error. Reading up around the site i found that i should be passing the derived object as a const, this however causes problems because a const can't be assigned to the

Using boost::function with a parameter to shared pointer to derived class

梦想的初衷 提交于 2021-01-28 03:22:34
问题 Using C++ with g++ 5.4.0 on Ubuntu 16.04. I have a class A, and a class B that derives from class A. Function f1 takes a shared pointer to class A as parameter. Function f2 takes a shared pointer to class B as parameter and return the same type as f1. Using boost::function, another function F takes a function such as f1 as parameter. The code looks like this : result_t f1 ( const boost::shared_ptr<A> a ); result_t f2 ( const boost::shared_ptr<B> b ); typedef boost::function < result_t (const

How to access derived class members from an interface?

流过昼夜 提交于 2020-08-19 05:43:36
问题 I have three classes; Stamp, Letter and Parcel that implement an interface IProduct and they also have some of their own functionality. public interface IProduct { string Name { get; } int Quantity { get; set; } float Amount { get; } } public class Stamp : IProduct { public string Name { get { return "Stamp"; } } public int Quantity { get; set; } public float Amount { get; set; } public float UnitPrice { get; set; } } public class Letter : IProduct { public string Name { get { return "Letter"

How to access derived class members from an interface?

时光怂恿深爱的人放手 提交于 2020-08-19 05:42:45
问题 I have three classes; Stamp, Letter and Parcel that implement an interface IProduct and they also have some of their own functionality. public interface IProduct { string Name { get; } int Quantity { get; set; } float Amount { get; } } public class Stamp : IProduct { public string Name { get { return "Stamp"; } } public int Quantity { get; set; } public float Amount { get; set; } public float UnitPrice { get; set; } } public class Letter : IProduct { public string Name { get { return "Letter"