Question on template writing

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.*

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T1,typename T2> 
struct MyPair	
{  
	T first; 
	T second;

	MyPair();

	MyPair(T1 frist,T2 second){
   	this.first = first;
	this.second = second;
	}	
}; 


Is my code snippet correct?.

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?

Last edited on
I was under the impression if you do not supply a default constructor then the compiler will synthesize a constructor for you,

The compiler will only generate the no argument constructor if there is no other constructor, or the no argument constructor is marked as default.

or is the second question hypothetical, and not a real scenario that would occur writing a C++ class?

No it's not hypothetical, you need to answer the question.




Is my code snippet correct?

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.
Can I not assign a default constructor that takes no parameters with MyPair(); ?

1
2
3
4
5
6
7
8
9
10
11
12
template <typename T1,typename T2> 
struct MyPair	
{  
	T1 first; 
	T2 second;

	MyPair() = default;   //Default Constructor

	MyPair(T1 first, T2 second)
	: first(first),  second(second) {}
	
}


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?

Last edited on
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.

Topic archived. No new replies allowed.