Learning the basics of C++ and have attempted to answer the following two questions.
1- *Define a templated struct called MyPair.Each MyPair object will contain two data fields, one called first of some type T1 (supplied as a template parameter),and the other called second of some type T2 (likewise suppliedas a template parameter).MyPair should have two constructors, one in which the initial values of first and second are specified as parameters to theconstructor, and another constructor (taking no parameters) in which first and second are initialized using the default constructors of types T1 and T2 respectively.*
2 - *Suppose that some class MyClass has no default constructor.Will that prevent you from constructing an object of typeMyPair<MyClass, int>? Justify your answer.*
I was under the impression if you do not supply a default constructor then the compiler will synthesize a constructor for you, so there will always be a constructor or is the second question hypothetical, and not a real scenario that would occur writing a C++ class?
No.
What is the type of first?
What is the type of second?
Where is the implementation of the default constructor?
How does one refer to a member via a pointer?
Your second constructor requires that type T has default constructor. That in itself is not an error, but you should prefer member initializer list syntax.
The compiler does not always supply a default constructor. For example, not if class has some custom constructor.
In regards to the second question would the issue arise when the member initializer list is called with type MyPair<MyClass, int> and the initializer list attempts to call the default constructor on MyClass which is not declared?
Can I not assign a default constructor that takes no parameters with MyPair(); ?
What? I don't understand the question.
In regards to the second question would the issue arise when the member initializer list is called with type MyPair<MyClass, int> and the initializer list attempts to call the default constructor on MyClass which is not declared?
Did you try commenting out the no argument constructor? If so what happened?
By the way to evaluate the second question you really need to actually declare an instance of your class.