Do C++ abstract classes need to obey the rule of five? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
When implementing an abstract class like this:
class Base
{
public:
virtual ~Base() = default;
virtual void foo() = 0;
};
Does this interface have to obey the rule of five i.e. do I have to add a copy constructor, copy assignment operator, move constructor and move assignment operator?
I'd figure that an instace of type Base can not be instantiated due to the pure virtual member function and thus providing default implementations for the other special member functions might serve no real purpose.
Is there any use-case/example that would require me to provide the other special member functions?

"abstract" is irrelevant here. A class needs its own copy constructor, copy assignment operator, etc. if it has data that won't be properly copied by the default versions. Full stop. The presence or absence of pure virtual functions does not change this. Your example doesn't have any data, so doesn't have an issue here.

Actually it is the contrary. I would consider deleting copying and assignment of a class that is supposed to be only an interface class to avoid slicing. Consider
class Base {
public:
virtual ~Base() {}
};
class D1 : public Base {
int i;
public:
~D1() override {}
};
class D2 : public Base {
int i;
double d;
public:
~D2() override {}
};
you could write something like this
vector<Base> vec;
D1 d;
D2 e;
vec.push_back(d);
vec.push_back(e);
. You would try to squeeze an object of size D2 into a much smaller object of type base. By deleting copy and assignment you do prevent the user or yourself doing that.

Related

c++ why would you pass a reference to a base class to a constructor of a derived class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In c++ is there a reson why you would pass a reference to a base class to a constructor of a derived class? Why would you want to do this?
Here is a basic example:
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
// This class ingerits from Base and implements fun()
class Derived: public Base
{
int y;
public:
Derived(Base& object);
void fun() { cout << "fun() called"; }
};
Typically, arguments are passed to constructors because the state of the arguments can be used to initialize the state of the object that is being constructed. Same applies to this case.
Non-const reference arguments can be (and nearly always are) used to modify the referred object.
In c++ is there a reason why you would pass a reference to a base class to a constructor of a derived class?
There is usually one reason why reference to an object would be passed to a constructor, does not really matter if that object type related to consttructed one or not - to construct this type you need information from that object. Using lvalue reference instead of const one could mean either bad design or ctor would need to modify passed object or keep non-const reference/pointer to it.
I would think the question is "why pass the base class reference instead of the derived class reference?". If so, the reason Base& is passed instead of Derived& is that the former allows you to pass an OtherDerived& reference, given that OtherDerived inherits Base. This is called polymorphism and is quite a thing in C++.
Here's pretty much the same question, but with a function instead of a constructor.

Class Inheritance default constructor cannot be referenced [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Questions : 33978185 , 32569833 didn't helped me.
By the way, in question 33978185 this error is considered as a bug, but since im using VS2017 i think it should be fixed by now.
Question 40181845 is using structs and i dont understand it quite well...
Question 38469597 says that i should make class B - friend class, is that the case here too?
I tried to search for more solutions but i havent got what i need.
Consinder my level of programming knowledge of object oriented c++ : Advanced> Normal > Begginer.
I am Normal.
I have class A, and class B:
class A
{
protected:
int q;
int w;
public:
A(int);
functionE();
functionR();
};
class B:public A
{
public:
functionT();
};
in main when i want to create a object of class B, I get error:
the default constructor "objectname" cannot be referenced -- its a deleted function.
From my knowledge of object oriented programming, when some members are protected, they can be accessed by the inherited class, and if inheritance is public, it can access public function.
I want my class B to have bonus functionT() and nothing else, but as i said, be able to access protected members of class.
One solution is to actually code functionT() in class A, which removes the need for class B, but I don't want that.
What should i do?
When you're trying to create an object of class B, you get an error as you didn't declare any constructor. B can't have a default constructor, as A doesn't have one either (when you declared A(int) as A constructor, you implicitly deleted the default constructor).
You can either create a default constructor for A, or default it by A() = default
or create a constructor for B, like so
B() : A(0) {}
B(int i) : A(i) {}
By providing the constructor A(int) the default constructor A() gets deleted. However, when creating an instance of class B it must call the constructor of its superclass A. The only way you provide is calling the default constructor, which doesn't exist anymore and thus your code does not compile.
In order for it to compile, you need to tell B how it is supposed to construct A. You can for example add this constructor to B: B(int a) : A(a) {}
Since you have a custom constructor for class A it needs to be called by the constructor of class B.
The generated 'empty' constructor of class B tries to call the 'empty' one of class A, which is no longer available. So you need to write a constructor for B as well.
class B : public A
{
public:
B() : A(0) {}
/* or */
B(int v) : A(v) {}
};

C++ - Deleting a polymorphic pointer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
class A { int* a; };
class B : public A { int*b; };
int main() {
A* ptr = new B();
delete ptr;
}
class A is a pure virtual interface class and class B inherits from class A. When we delete ptr which destructor will be called? The one from the A class or the one from from the B class?
Comment: First of, why do you have code outside any function? Statements only make sense when there are within the body of a function, like main.
Assuming the statements you posted were supposed to go into main:
Answer:
delete ptr will call the destructor of A. The compiler will not 'think' any further than this.
Reason: All methods (including the destructor) are non-virtual by default. In your case, you did not specify that the destructor should be virtual. The compiler sees that you are calling the destructor on a A* pointer, so it calls the destructor of A.
What if I had specified that Class A destructor was virtual? Would it still call the destructor of Class A?
Answer: If it were virtual, it would call the destructor of B, because the actual type of the object would be determined during the execution of the program.
See more about virtual functions and polymorphism here.

C++ Inheriting base Class Destructor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a game, where i have a derived player class that inherites from a Parent GameObject Class, What i want to accomplish is calling the base class destructor inside of the derived class destructor, can i do that?
Example:
// Base Class
class A
{
public:
// other code goes here...
~A();
protected:
int a;
}
// ...
// ...
// Base Class Destructor
A::~A()
{
// sets a back to 0
a = 0;
}
// Derived Class
class B : public A
{
public:
// other code goes here...
~B();
}
// Derived Class Methods
B::~B()
{
// Calls for Base Class Destructor, How can i accomplish this
A::~A();
}
Parent class' destructor is automatically called. Calling order of destructors is opposite to order of constructors; so that, it's ok to rely on parent class' fields in destructor of derived class.
You should better declare destructor as virtual. It's needed to determine correct destructor if you delete object of derived class through base class pointer.
Try adding trace output in destructors to ensure what calling order of destructors is.

In c++, are the virtual function, function overriding and polymorphism related with each other? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Generally in C++, we see same name functions with same arguments in different classes along with virtual keyword. Is it an example of polymorphism? If it is, how and is there any other way to understand polymorphism in c++??
Suppose that you have the following scheme:
class Base {
public:
virtual int foo() = 0;
};
class Derived1 : public Base {
public:
virtual int foo() { return 1; }
};
class Derived2 : public Base {
public:
virtual int foo() { return 2; }
};
Lets say now, that you want to store objects of Derived1 and Derived2 in a container, lets say a std::vector and every time you iterate through the vector to call for each one of its objects function foo. Normally, you'd need two vectors, one for objects of class Derived1 and another one for objects of class Derived2.
Look now how polymorphism comes to our rescue. We can store the addresses of objects Derived1 and Derived2 as pointers to Base objects, and then store these pointers in a std::vector<Base*> v. Now every time we iterate the vector of pointers and call member function foo (e.g., for(auto base : v) base->foo()) the right function foo is called for each one of the objects. This is one of the many aspects of polymorphism which is called upcasting
I hope that the above example of runtime polymorphism gave you a clue, as for how virtual functions and function overriding are related to inheritance and polymorphism.
Update:
Templates are a form of compile time polymorphism take a look at this SO question what is the difference between templates and polymorphism.