I'm new to c++ programming language. I wrote several patterns of code to understand c++ constructor and initializer. But I couldn't find out why one build fail and another doesn't. From my view, there seems no such difference that one fail and another doesn't. The code is the following 5 codes. What's the difference between code example 1-2, 2-3, 3-4, 4-5. Why one fail but another doesn't?
Additional Info:
I'm building these code with Xcode 8.2.
The Code
Example 1:
class A{
public:
A(int xxx) { }
};
int main(){
A a; // Fail here. I see this is because there is no default constructor in definition of class A.
}
Example 2:
class A{
public:
A(int xxx) { }
};
class B{
public:
A a; // No fail. Why? I supposed it will fail like Example 1.
};
int main(){
}
Example 3:
class A{
public:
A(int xxx) { }
};
class B{
public:
A a; // No fail here.
B(int xxx) { } // But fail here. Why? I just added constructor to Example 2.
};
int main(){
}
Example 4:
class A{
public:
A(int xxx) { }
};
class B{
public:
A a;
B(int xxx) : a(123) { } // No fail. Why Just adding ":a(123)" works.
};
int main(){
}
Example 5:
class A{
public:
A(int xxx) { }
};
class B{
public:
A a;
B(int xxx){ a = 123; } // Fail again. Why? I think ":a(123)" and "a=123" is same meaning.
};
int main(){
}