Will it compile?

Hi guys,
I'm an exercise(as a code) I got few days ago and I'm doing it right now;
The question is in the file. Thank you guys!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class type, int n> class print{
	int x; 
public: 
	print(int i): x(i){};
	void printing(){
		for(int i=0;i<n;i++)
			cout<<x;
		cout<<endl;
	}
};

void main(){
	print<int,5> p1(1);
	print<int,12> p2(2);
	print p3(3); //won't compile
	print<int,5> p4(4);
	print p5(int,5); //won't compile
	p1.printing();
	p=&p4;
	p->printing();
	p2=p4 //won't compile
	p=&p2; //Why doesn't it compile? Just making p point to different place
}
Last edited on
print<int,5> *p; //this is compiles. Why?
//in this line, *p gets no arguments(so it can not use the C'tor in the
//class), but there is no default C'tor. So how come it compiles?

This creates a pointer, not an object of any print<> type. Pointers don't have constructors, so it is uninitialized. You could initialize it with
1
2
3
4
5
int main()
{
    print<int,5> p1(1);
    print<int,5> *p = &p1;
}

closed account (3qX21hU5)
in class print we wrote new C'tor, so the default C'tor was deleted.
//in this line, *p gets no arguments(so it can not use the C'tor in the
//class), but there is no default C'tor. So how come it compiles?


You didn't override the default constructor. You just made a different constructor.

In order to actually override the implicit default constructor that the compiler generates for you, you would have to write a constructor like this.
print() : x(0) {}; // Should give x a default value

Basically no parameters. That is the default constructor (A constructor with no parameters, or all parameters have a default argument). So the reason why line 18 compiles is because you still have the implicitly generated default constructor.


EDIT: Ignore my half asleep madness ;p


print p3(3); //won't compile


You have no template parameters. The same goes with the rest of them.
Last edited on
You didn't override the default constructor. You just made a different constructor.

In order to actually override the implicit constructor that the compiler generates for you, you would have to write a constructor like this.
print() : x() {};

This is incorrect. The compiler only generates a default constructor if no other constructors of any kind are defined.

By defining the constructor print(int i), the OP prevents the compiler from automatically generating a default constructor, even though print(int i) is not a default constructor.
Last edited on
I was taught that when I'm implementing a new constructor of any kind the compiler will override it's own default c'tor.
I was taught that when I'm implementing a new constructor of any kind the compiler will override it's own default c'tor.

Well, nothing is "overidden", but if you create a new c'tor of any kind, the compiler won't generate a default one.
closed account (3qX21hU5)
Ohh gezz it is way to early and I have been away from C++ for way to long ;p thank you for correcting me on that MikeyBoy ;p
No problem :)
Thanks guys, specially MikeyBoy.

the code was edited the code and a new question was added.

Thanks!
Topic archived. No new replies allowed.