Chained Converting Constructors [duplicate] - c++

This question already has answers here:
Implicit constructor argument conversion in C++11
(3 answers)
Closed 3 years ago.
class A{
public:
A(int i){}
};
class B{
public:
B(A){}
};
void test(B){};
int main() {
A a=1;
B b1=a;
test(1);
}
I have two classes, where the first class has a converting int Ctor, which means that I can construct it by "assigning" an integer (first line).
The second class has a converting Ctor with argument type A. It is possible to construct b1 with a (second line).
Is there any possibility to use multiple converting ctor in a chain, in such a way that the third row will compile?

You can explicitly construct B from int, but not implicitly:
test(B{1}); // ok
test(1); // Not ok
That being said, this half-way measure also works:
test({1}); // ok
And everything above works for only one implicitly generated middle class (A). Such a chain can not be further extended.

Related

Declaring an object with a parameterized constructor inside another class C++ [duplicate]

This question already has answers here:
C++ class member with constructor
(4 answers)
Avoid default constructor for member variable
(2 answers)
Explicitly initialize member which does not have a default constructor
(4 answers)
Closed 2 years ago.
I wish to have an object of one class contained inside a different class.
class A contained inside class B as in the simple example below.
The problem is that class A only has a parametrized constructor.
Is there another way to do declare the class A object in class B without having to use a pointer to class A ?
class A
{
public:
A(int var1, int var2);
private:
//...
};
class B
{
public:
B();
private:
A a; // Compiler error
A* a_ptr; // This will of course work fine. We can create a new A object with parameters any time using the a_ptr
};
A a; // Compiler error
except if the constructor of B explicitly calls the constructor of A with required arguments (e.g. B() : a(an_int, an_int) {...}), that requests a constructor without parameter or where all parameters have default value, but you do not have that constructor, only a constructor requiring 2 int
Is there another way to do declare the class A object in class B without having to use a pointer to class A ?
you do not declare but define / instantiate a new instance of A each time you instantiate a new instance of B
Note you can create later the instance of A without using a pointer as with a_ptr having for instance
std::vector<A> a;
and when you know the instance of A you need doing for instance
a.push_back(A(an-int, an-int));
but the fact you want only one instance is not visible in the definition of a (out of a welcome comment)

C++:difference between assigning a value to an object and a class [duplicate]

This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 4 years ago.
#include <iostream>
using namespace std;
class A{
public:
int a;
A() {a=0;}
A(int b) {a=b+1;}
};
class B{
public:
A a;
B():a(0) {}
};
int main(void) {
B *b = new B();
cout<<b->a.a<<endl;
return 0;
}
I replaced B():a(0) {} by B() {A(0)} and output changed from 1 to 0. I am wondering what's the difference between them?
B() : a(0) {} explicitly initializes the member B::a with the value 0. This is an example of a member initializer list. Basically, a(0) in this context calls a's constructor with the argument 0, which calls A::A(int), which, by your implementation, adds 1 to the argument and assigns the result to A::a. Thus in the first case, b->a.a == 1.
On the other hand, B() {A(0);} default-initializes B::a (because A has a default constructor) and creates a nameless local temporary object A(0); which is destroyed immediately without changing anything. It is functionally equivalent to B(){}, which is does nothing at all. You can omit this trivial constructor because the compiler can implicitly generate it for you. See special member functions for more information on how this works and under what conditions. Thus in the second case, you are calling the default constructor, which sets b->a.a == 0.

C++ instantiate a class [duplicate]

This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Most vexing parse
(1 answer)
Closed 9 years ago.
#include <iostream>
class Base
{
public:
int id;
};
int main()
{
Base b();
b.id = 1;
}
What is wrong with the object creation in the code above? What is the difference between Base b() and Base b?
This
Base b();
is parsed as a function declaration of a function called b() that returns a Base object. You need this:
Base b;
This will instantiate a Base object and call its default constructor. Note that this will not zero initialize base::id. You can fix this by providing a default constructor that does that:
class Base
{
public:
Base() : id(0) {}
int id;
};
and instantiate in the same way:
Base b;
The problem is that you are not instantiating an object, but rather declaring a function. This:
Base b(); // Function declaration! (not what you want)
Declares a function b() that returns an object of type Base and accepts no argument. Therefore, when you later try to access the member id, the compiler emits an error, because functions do not have members:
b.id = 1; // ERROR! b is a function, accessing member "id" makes no sense
If you want to create an instance of Base, instead, just remove the parentheses:
Base b; // Object instantiation! (what you want)
Notice that in C++11 you can use the uniform initialization syntax to create an instance of a class pretty much the way you were trying to do, but with curly braces instead of parentheses:
Base b{}; // Object instantiation! (what you want, in C++11)

Constructors comparison [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Initializing in constructors, best practice?
I'm new to C++.
Suppose we have this class definition:
Class MyClass {
int a;
int b;
//....
}
I would like to know which is the difference between the two class contructors:
public:
MyClass(int a, int b) : a(a), b(b) {}
and (i would say Java-style):
MyClass(int a, int b) {
this->a = a;
this->b = b;
}
I suppose the first is better in C++; right? why?
The first one (using initializer list) initializes the data members to the given values. The second one initializes them first, then assigns them the values. That is the reason the first is prefered. There is no unnecessary assignment operation there.
This is particularly important when your data members are expensive to construct and/or assign to. Also bear in mind that some types are not default constructable, making it mandatory to use the initializer list.

what compiler does internally for initializing a variable and assigning a variable while constructing object? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why should I prefer to use member initialization list?
Class A has a member variable i. i can be initialized or assigned during object creation.
A) Initialise
class A {
int i;
public:
A(int _i) : i(_i){}
}
B) assign
class A {
int i;
public:
A(int _i) : { i = _i}
}
My question is what is the basic difference between these 2 approach?
The difference lies in which C++ mechanism is used to initialize i in your class. Case (A) initializes it via constructor, and case (B) uses the assignment operator (or a copy constructor if no assignment operator is defined).
Most C++ compilers would generate exactly the same code for this particular example, because you're using int, which is a "plain old data" type. If i were a class type, it could make a great deal of difference.