Calling base class' constructor without defining it in derived class [duplicate] - c++

This question already has answers here:
Using C++ base class constructors?
(6 answers)
Closed 1 year ago.
class A {
public:
int _a
A(int a) : _a { a} { }
};
class B : public A { }
Why can't I initialize a variable of A type with using the inherited constructor?
B a(5);
Error: "No matching constructor for initialization of 'B'"

Because B doesn't have such a constructor taking int by default, unless inherit it explicitly:
class B : public A {
using A::A; // inheriting constructor
};

Related

Calling class constructor later in another class method in C++ [duplicate]

This question already has answers here:
no default constructor exists for class
(5 answers)
Closed 2 years ago.
I'm writing Class A, and it has a constructor that initializes values. I'm creating an instance of Class A in another Class B like so:
#include <iostream>
class A {
public:
int value;
A(int value) {
this->value = value;
}
void print_string() {
std::cout << "talking...";
}
};
class B {
public:
A new_a;
B(int b_value) {
new_a = A(b_value);
}
void print_from_A() {
new_a.print_string();
}
};
int main() {
B b(23);
b.print_from_A();
return 0;
}
However, I have syntax errors with this code, it says no default constructor exist for class A . What I want is:
1 to make instance of class A global in class B so other methods in class B can access it.
2 to initialize values in class A through it's constructor by calling it in, and passing the values through class B
There is no default constructor for A. You should use a member initializer lists
B(int b_value): new_a(b_value) {
}
Your constructor
B(int b_value) {
new_a = A(b_value);
}
tries to default initialize new_a and then to assign A(b_value) to it. But default initialization is not possible without default constructor. You should always use member initializer lists:
A(int value): value(value) {
}

C++: call cosntructor of base class in a derived class without a constructor [duplicate]

This question already has answers here:
Inheriting constructors
(8 answers)
Closed 2 years ago.
I want to make a simple wrapper B around a complex class A. A has many different constructors. The class B does not have any constructors, so how can I automatically pass the constructor arguments to the base class A, without needing to implement them all in B?
Here is a simple example of what I want to do:
class A
{
public:
A(int a);
A(const& A);
// etc ...
};
class B : public A
{
int b;
};
void main()
{
B b(0); // Since B does not have a constructor, I want it to dispatch to A::A(int)
}
note: A is a class from some package so I can not simply add my int b to A.
In B, you can inherit A's constructors:
class B : public A
{
using A::A;
int b;
};
Then B b(0); will work.

calling a member function of a class by member function of another class? [duplicate]

This question already has answers here:
calling a member function of different class from another class
(2 answers)
calling a class method from another class
(1 answer)
Closed 7 years ago.
i have two classes A & B.i want to call a member function of A by member function of B.
class A {
public:
void memberofa();
}
class b:
class B {
public:
void memberofb();
}
now i need to call memberofa from inside memberofb.
Any suggestions and syntaxes will be helpful
Something like this?
class A {
public:
A() {};
void memberofa()
{
//you cant make object of B here because compiler doesn't see B yet
//if you do want to make Object of B here, define this function somewhere
//after definition of B class
printf("printing from member of A\n");
};
};
class B {
public:
B() {};
void memberofb()
{
printf("printing from member of B\n");
A objA;
objA.memberofa();
};
};
int main()
{
A a;
B b;
b.memberofb();
return 0;
}
B inherit from A
B contain A object
A::memberofa is static function
A is singleton class
A inherit from B, B has memberofa and it is virtual function.

Inhereting constructors in C++ [duplicate]

This question already has an answer here:
Why is Default constructor called in virtual inheritance?
(1 answer)
Closed 9 years ago.
C++11 standard provides a way to inherit constructors from the base class. My question is regarding earlier standards. Suppose I inherit constructors in the following way:
class Base {
public:
Base() {};
Base(int b) { a = ++b;}
virtual int foo() {return 0;}
int a;
};
class A : public virtual Base {
public:
A() {}
A(int b): Base(b) {}
int foo() {return a*a;}
};
class C : public A {
public:
C() {}
C(int b ): A(b) {}
int foo() { return (a*a + a);}
};
Note that I am having virtual inheritance of the Base class. Now when I try to initialize a pointer to an object of type C then instead of calling Base(b) constructor, the code ends up calling Base() constructor. Here is the main function that I used:
int main(){
C *c = new C(5);
std::cout << c->Base::a << std::endl;
}
The output value of "a" is 0. However, when I remove the virtual keyword while inheriting the Base class, then Base(b) constructor is called and the value of "a" is 6. Can someone help me in understanding what is going on? Why is it that with virtual inheritance default constructor is called?
Virtual base classes are initialised based on the member initialiser list of the constructor of the most derived class.
In your case, when you're creating an instance of C, its Base subobject will be initialised based on the member-initialiser list in C's constructor; and since Base is not listed there, it will be default-initialised.
If you were creating an instance of A, then indeed A's member-initialiser list would be used.
So to call the Base constructor which you want, you'd have to modify C's constructor like this:
C(int b ): A(b), Base(b) {}

Why only base class default constructor is called in virtual base multiple inheritance? [duplicate]

This question already has answers here:
c++ virtual inheritance
(3 answers)
Closed 9 years ago.
In multiple inheritance, I have a virtual Base class which is inherited by class A and class B. A and B are base classes of AB. Please see the code below.
In constructor of A and B, Base(string) constructor is called. I am expecting to get following output:
Base::Base(std::string)
A::A()
B::B()
But I am getting following output:
Base::Base()
A::A()
B::B()
Why default constructor of Base is being called?
#include<iostream>
#include<string>
using namespace std;
class Base{
public:
Base(){
cout<<__PRETTY_FUNCTION__<<endl;
}
Base(string n):name(n){
cout<<__PRETTY_FUNCTION__<<endl;
}
private:
string name;
};
class A : public virtual Base {
public:
A():Base("A"){
cout<<__PRETTY_FUNCTION__<<endl;
}
private:
string name;
};
class B : public virtual Base {
public:
B():Base("B"){
cout<<__PRETTY_FUNCTION__<<endl;
}
private:
string name;
};
class AB : public A, public B{
};
int main(){
AB a;
}
The virtual base is constructed by the most-derived object. So AB's constructor call the Base constructor, but since you didn't specify a constructor for AB, its default constructor just calls the default constructor of Base.
You could call the string-constructor from AB like this:
struct AB : A, B
{
AB() : Base("hello"), A(), B() { }
};
Note that the constructors A::A() and B:B() do not call the Base constructor in this setup!