In the code below, class B has a member that is of type class A (varA1). I want to create a class B object where the member varA1 is intended to use the non-default constructor A(int v1) in class A.
#include <iostream>
using std::cout; using std::endl; using std::string;
class A {
public:
A() { cout << "A default constructor" << endl;}
A(int v1);
private:
int var1;
};
A::A(int v1) {
cout << "A int constructor" << endl;
var1 = v1;
}
class B {
public:
B() { cout << "B default constructor" << endl;}
B(int v1);
private:
int var1;
A varA1;
};
B::B(int v1) {
cout << "B int constructor" << endl;
var1 = v1;
A varA1(int v1);
}
int main()
{
A TestA(1);
B TestB(1);
return 0;
}
However when I run the code above I get the following output:
A int constructor
A default constructor
B int constructor
I must be doing some wrong here. What do I need to change so that the B class uses the non-default constructor A(int v1) in class A?
I am using ubuntu 14.04LTS. Both GNU G++ 4.9 and 5.1 gave the same results.
Thanks in advance for reading and answering.