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

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) {
}

Related

How to initialize an `const int` member in a base class when constructing child? [duplicate]

This question already has answers here:
How can I initialize base class member variables in derived class constructor?
(7 answers)
Closed 24 days ago.
#include <iostream>
class A {
public:
const int HP = 200;
A(){
std::cout << this->HP << std::endl;
}
};
class B : public A {
public:
B();
};
Constructing a B object will construct the A potion first.
I expect to reinitialize HP so that B() can print new HP.
Is this feasible for a base class const member ?
I need a member shared between base and child but to be constant in one instance of class, any suggestions?
You can add a constructor to A (and if needed also to B) that accepts the value for the const int member.
Then when you invoke A constructor from B constructor via the initializer list, you can pass this const value to the base.
Code example:
#include <iostream>
class A {
public:
const int HP = 200;
A() {
std::cout << this->HP << std::endl;
}
//----vvvvvv----vvvvvv--
A(int hp) : HP(hp) {
std::cout << this->HP << std::endl;
}
};
class B : public A {
public:
B() {};
//----vvvvvv----vvvvv--
B(int hp) : A(hp) {}
};
int main()
{
B b1;
B b2{ 99 };
}
Output:
200
99
Note: B can also be left as is (without an additional constructor), and pass the required const value to the base.

how to initialize base class with members of derived class [duplicate]

This question already has answers here:
warning: derived class's member variable is initialized after base class
(3 answers)
C++: Construction and initialization order guarantees
(5 answers)
Closed 9 months ago.
Beginner here, my simplified code:
A.h
class A{
public:
A(int x);
private:
int _x;
}
A.cpp
A::A(int x)
: _x(x)
{
}
B.h
class B : public A{
public:
B()
private:
int _y = 1;
}
B.cpp
B::B()
: A(1) //works
: A(_y) //doesn't work
{
}
Why does the initialization with a member of B not work, but with a bare integer it does.
I mean I can just go for the working method but I'd like to know the cause.
Thanks for any helpers!:)
Vlad has given you the reason, here's a workaround that avoids the need to duplicate your magic number:
class B : public A{
public:
B();
private:
static const int initial_y = 1;
int _y = initial_y;
};
B::B()
: A(initial_y)
{
}
Demo
A derived class's base classes are fully initialized before any of the derived class's data members are initialized.
In the case of:
class B : public A{
public:
B();
private:
int _y = 1;
}
B::B()
: A(_y)
{
}
Data members that have initial values in their declarations are implicitly handled by the constructor's member initialization list. So, in this case, the compiler treats the above code as-if you had written it like this instead:
class B : public A{
public:
B();
private:
int _y;
}
B::B()
: A(_y), _y(1)
{
}
As you can see, A(_y) is called before _y is initialized, which is undefined behavior.

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

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
};

Is it possible for a class to set the value of a variable that it inherited from another class in the parameter of its constructor?

Class A contains the protected int x. Class B extends class A. Now what class B wants to do is set the value of x as a passing argument in its own constructor. When I try to do that, I get the error:
""x" is not a non-static data member or base class of class "B"".
#include <string>
#include <iostream>
class A {
protected:
int x;
public:
A()
{
}
};
class B : public A {
public:
B(int x)
: x(x)
{
}
};
int main()
{
}
You can "set" it, but not initialize it, because it has already been initialized when the base class object gets initialized. You can "set" it like this:
B(int x)
{
this->x = x; // assignment, not initialization
}
It would make more sense for one of A's constructors to take care of the initialization of A::x:
A(int x) : x(x) {}
and then use that in B:
using A::A; // allows B b{42};

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.