All Questions

Tagged with
Filter by
Sorted by
Tagged with
1259 votes
17 answers
777k views

What is the difference between public, private, and protected inheritance?

What is the difference between public, private, and protected inheritance in C++?
user avatar
915 votes
17 answers
266k views

What is object slicing?

In C++, what is object slicing and when does it occur?
Frankomania's user avatar
  • 9,215
893 votes
17 answers
604k views

How do you declare an interface in C++?

How do I setup a class that represents an interface? Is this just an abstract base class?
Aaron Fischer's user avatar
865 votes
10 answers
1.1m views

What are the rules for calling the base class constructor?

What are the C++ rules for calling the base class constructor from a derived class? For example, I know in Java, you must do it as the first line of the subclass constructor (and if you don't, an ...
levik's user avatar
  • 116k
790 votes
9 answers
977k views

How to call a parent class function from derived class function?

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within each class there is a print ...
IaCoder's user avatar
  • 12.6k
328 votes
8 answers
385k views

Inheriting constructors

Why does this code: class A { public: explicit A(int x) {} }; class B: public A { }; int main(void) { B *b = new B(5); delete b; } Result in these errors: main.cpp: In ...
Sydius's user avatar
  • 13.9k
264 votes
3 answers
32k views

Why do I have to access template base class members through the this pointer?

If the classes below were not templates I could simply have x in the derived class. However, with the code below, I have to use this->x. Why? template <typename T> class base { protected: ...
Ali's user avatar
  • 57.5k
247 votes
6 answers
235k views

Struct inheritance in C++

Can a struct be inherited in C++?
user avatar
243 votes
13 answers
79k views

Thou shalt not inherit from std::vector

Ok, this is really difficult to confess, but I do have a strong temptation at the moment to inherit from std::vector. I need about 10 customized algorithms for vector and I want them to be directly ...
Armen Tsirunyan's user avatar
198 votes
2 answers
127k views

C++ inheritance - inaccessible base?

I seem to be unable to use a base class as a function parameter, have I messed up my inheritance? I have the following in my main: int some_ftn(Foo *f) { /* some code */ }; Bar b; some_ftn(&b); ...
bandai's user avatar
  • 2,015
184 votes
7 answers
221k views

How can I initialize base class member variables in derived class constructor?

Why can't I do this? class A { public: int a, b; }; class B : public A { B() : A(), a(0), b(0) { } };
amrhassan's user avatar
  • 2,385
181 votes
7 answers
93k views

Why should I declare a virtual destructor for an abstract class in C++?

I know it is a good practice to declare virtual destructors for base classes in C++, but is it always important to declare virtual destructors even for abstract classes that function as interfaces? ...
Kevin's user avatar
  • 7,926
163 votes
12 answers
101k views

Is there a way to instantiate objects from a string holding their class name?

I have a file: Base.h class Base; class DerivedA : public Base; class DerivedB : public Base; /*etc...*/ and another file: BaseFactory.h #include "Base.h" class BaseFactory { public: ...
Gal Goldman's user avatar
  • 8,769
157 votes
5 answers
63k views

Initialize parent's protected members with initialization list (C++)

Is it possible to use the initialization list of a child class' constructor to initialize data members declared as protected in the parent class? I can't get it to work. I can work around it, but it ...
Stephen's user avatar
  • 3,545
153 votes
6 answers
56k views

What is the point of a private pure virtual function?

I came across the following code in a header file: class Engine { public: void SetState( int var, bool val ); { SetStateBool( int var, bool val ); } void SetState( int var, int val ); ...
BeeBand's user avatar
  • 11.3k
143 votes
6 answers
237k views

Inheriting from a template class in c++

Let's say we have a template class Area, which has a member variable T area, a T getArea() and a void setArea(T) member functions. I can create an Area object of a specific type by typing Area<int&...
dtech's user avatar
  • 1
124 votes
5 answers
102k views

How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?

class A { public: void eat(){ cout<<"A";} }; class B: virtual public A { public: void eat(){ cout<<"B";} }; class C: virtual public A { public: void eat(){ cout&...
Moeb's user avatar
  • 10.6k
122 votes
5 answers
103k views

Do ALL virtual functions need to be implemented in derived classes?

This may seem like a simple question, but I can't find the answer anywhere else. Suppose I have the following: class Abstract { public: virtual void foo() = 0; virtual void bar(); } class ...
mikestaub's user avatar
  • 2,164
115 votes
5 answers
89k views

How to use base class's constructors and assignment operator in C++?

I have a class B with a set of constructors and an assignment operator. Here it is: class B { public: B(); B(const string& s); B(const B& b) { (*this) = b; } B& operator=(const ...
Igor's user avatar
  • 27k
110 votes
3 answers
36k views

Does Qt support virtual pure slots?

My GUI project in Qt has a lot of "configuration pages" classes which all inherit directly from QWidget. Recently, I realized that all these classes share 2 commons slots (loadSettings() and ...
ereOn's user avatar
  • 54.8k
107 votes
7 answers
69k views

Are static fields inherited?

When static members are inherited, are they static for the entire hierarchy, or just that class, i.e.: class SomeClass { public: SomeClass(){total++;} static int total; }; class ...
user avatar
102 votes
2 answers
50k views

Function with same name but different signature in derived class not found

I have a function with the same name, but with different signature in a base and derived classes. When I am trying to use the base class's function in another class that inherits from the derived, I ...
Igor's user avatar
  • 27k
102 votes
9 answers
69k views

Why does C++ not allow inherited friendship?

Why is friendship not at least optionally inheritable in C++? I understand transitivity and reflexivity being forbidden for obvious reasons (I say this only to head off simple FAQ quote answers), but ...
99 votes
4 answers
24k views

`std::variant` vs. inheritance vs. other ways (performance)

I'm wondering about std::variant performance. When should I not use it? It seems like virtual functions are still much better than using std::visit which surprised me! In "A Tour of C++" ...
The Moisrex's user avatar
  • 1,967
98 votes
1 answer
36k views

conversion from derived * to base * exists but is inaccessible

Why does the follwing code produce this error even though c is a struct and has a public inheritance by default?? struct c { protected: int i; public: c(int ii=0):i(ii){} virtual c *fun()...
user1232138's user avatar
  • 5,491
97 votes
7 answers
93k views

Inheritance: 'A' is an inaccessible base of 'B'

$ cat inheritance.cpp #include <iostream> using namespace std; class A { }; class B : private A { }; int main() { A* ab = new B; } $ $ g++ inheritance.cpp inheritance.cpp: In function '...
Lazer's user avatar
  • 92.6k
93 votes
3 answers
67k views

C++ virtual function return type

Is it possible for an inherited class to implement a virtual function with a different return type (not using a template as return)?
zzzbbx's user avatar
  • 10k
92 votes
5 answers
55k views

In C++, is it possible to forward declare a class as inheriting from another class?

I know that I can do: class Foo; but can I forward declare a class as inheriting from another, like: class Bar {}; class Foo: public Bar; An example use case would be co-variant reference return ...
anon's user avatar
  • 41.8k
91 votes
9 answers
45k views

Default inheritance access specifier

If I have for example two classes A and B, such that class B inherits A as follows: class B: public A In this case, I'm doing public inheritance. If I write the previous code as follows: class B: ...
Simplicity's user avatar
  • 48.3k
91 votes
3 answers
60k views

Derived template-class access to base-class member-data

This question is a furtherance of the one asked in this thread. Using the following class definitions: template <class T> class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(...
Shamster's user avatar
  • 2,172
90 votes
5 answers
32k views

Are virtual destructors inherited?

If I have a base class with a virtual destructor. Has a derived class to declare a virtual destructor too? class base { public: virtual ~base () {} }; class derived : base { public: virtual ~...
cairol's user avatar
  • 8,693
89 votes
6 answers
155k views

Using C++ base class constructors?

While working with templates I ran into a need to make a base class constructors accessible from inherited classes for object creation to decrease copy/paste operations. I was thinking to do this ...
minyor's user avatar
  • 1,027
88 votes
1 answer
21k views

Why is Default constructor called in virtual inheritance?

I don't understand why in the following code, when I instanciate an object of type daughter, the default grandmother() constructor is called ? I thought that either the grandmother(int) constructor ...
Simon Desfarges's user avatar
85 votes
20 answers
30k views

Why can't I inherit from int in C++?

I'd love to be able to do this: class myInt : public int { }; Why can't I? Why would I want to? Stronger typing. For example, I could define two classes intA and intB, which let me do intA + intA ...
Rocketmagnet's user avatar
  • 5,829
84 votes
9 answers
52k views

C++: Can a struct inherit from a class?

I am looking at the implementation of an API that I am using. I noticed that a struct is inheriting from a class and I paused to ponder on it... First, I didn't see in the C++ manual I studied ...
augustin's user avatar
  • 14.6k
81 votes
5 answers
17k views

C++ static polymorphism (CRTP) and using typedefs from derived classes

I read the Wikipedia article about the curiously recurring template pattern in C++ for doing static (read: compile-time) polymorphism. I wanted to generalize it so that I could change the return types ...
Samuel Powell's user avatar
80 votes
5 answers
51k views

Why should I use the "using" keyword to access my base class method?

I wrote the below code in order to explain my issue. If I comment the line 11 (with the keyword "using"), the compiler does not compile the file and displays this error: invalid conversion from 'char' ...
Julien Vaslet's user avatar
79 votes
7 answers
110k views

C++ Constructor/Destructor inheritance

EDIT : Summary of answers In the following, B is a subclass of A. It's a matter of terminology; ctors and dtors are not inherited, in the sense that the ctor/dtor of B will not be borrowed from A's ...
Jonathan H's user avatar
  • 7,761
78 votes
4 answers
201k views

C++ cast to derived class

How can i cast to a derived class? The below approaches all give the following error: Cannot convert from BaseType to DerivedType. No constructor could take the source type, or constructor ...
user346443's user avatar
  • 4,772
75 votes
8 answers
54k views

Prevent class inheritance in C++

Recently one of my friend asked me how to prevent class inheritance in C++. He wanted the compilation to fail. I was thinking about it and found 3 answers. Not sure which is the best one. 1) Private ...
ring0's user avatar
  • 811
75 votes
1 answer
77k views

virtual inheritance [duplicate]

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;
Gal Goldman's user avatar
  • 8,769
74 votes
11 answers
30k views

What's the point of a final virtual function?

Wikipedia has the following example on the C++11 final modifier: struct Base2 { virtual void f() final; }; struct Derived2 : Base2 { void f(); // ill-formed because the virtual function ...
fredoverflow's user avatar
73 votes
7 answers
34k views

Propagating 'typedef' from based to derived class for 'template'

I'm trying to define base class, which contains typedef's only. template<typename T> class A { public: typedef std::vector<T> Vec_t; }; template<typename T> class B : public A&...
dimba's user avatar
  • 27.2k
73 votes
7 answers
63k views

Are static variables in a base class shared by all derived classes?

If I have something like class Base { static int staticVar; } class DerivedA : public Base {} class DerivedB : public Base {} Will both DerivedA and DerivedB share the same staticVar or will ...
mpen's user avatar
  • 278k
71 votes
1 answer
29k views

What is constructor inheritance?

In C++11, what is meant by inheriting the constructor? If it is what i think it is (Base class constructor is brought in the scope of the derived class), what are its implications on my code? What are ...
badmaash's user avatar
  • 4,805
70 votes
8 answers
45k views

Why do we actually need Private or Protected inheritance in C++?

In C++, I can't think of a case in which I would like to inherit private/protected from a base class: class Base; class Derived1 : private Base; class Derived2 : protected Base; Is it really useful?...
Gal Goldman's user avatar
  • 8,769
70 votes
8 answers
25k views

Why should one not derive from c++ std string class?

I wanted to ask about a specific point made in Effective C++. It says: A destructor should be made virtual if a class needs to act like a polymorphic class. It further adds that since std::string ...
Sriram Subramanian's user avatar
67 votes
6 answers
160k views

Why does the base class constructor of a derived type get called?

#include <iostream> #include <stdio.h> using namespace std; // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(...
liv2hak's user avatar
  • 14.7k
63 votes
6 answers
34k views

How to define sealed class in C++?

How to stop the class to be inherited by other class.
Shashi's user avatar
  • 2,890
62 votes
5 answers
50k views

Can't downcast because class is not polymorphic?

Is it possible to have inheritance with no virtual methods? The compiler is saying that the following code is not polymorphic. Example: class A { public: int a; int getA(){return a;}; } class ...
wfbarksdale's user avatar
  • 7,546

1
2 3 4 5
209