-3

My teacher teached me, that when im using one of the big three (i.e. copy constructor\default\assignment), so the compiler is not going suplly me the other for free and I have to implement them by myself. But I tried to implement a constructor like this:

A(int a): integer(a){};

And I succeed however to do this:

A my_first_ob(100);
A my_second_ob(my_first_ob);

How it is possible? Maybe someone can clear me the things here?

fdwfg wdfwdfv
  • 199
  • 1
  • 2
  • 9
  • 1
    Your teacher told you a non default constructor stops the auto generation of the copy constructor? – NathanOliver Jan 17 '17 at 13:08
  • 2
    No, the rule of three does not mean that. –  Jan 17 '17 at 13:10
  • 4
    Possible duplicate of [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) –  Jan 17 '17 at 13:11
  • I think your teacher meant the destructor instead of the (default) constructor – Rakete1111 Jan 17 '17 at 13:14
  • @NickyC That is not a correct dupe. A correct dupe would be: http://stackoverflow.com/questions/3734247/what-are-all-the-member-functions-created-by-compiler-for-a-class-does-that-hap – NathanOliver Jan 17 '17 at 13:17
  • @NathanOliver Perhaps you are right. Perhaps [What is The Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) is more an FYI than a dupe. –  Jan 18 '17 at 01:32

1 Answers1

0

The constructor you implemented is not a copy constructor or a default constructor, so it has nothing to do with what (you think) your teacher said.

I believe either you or your teacher have mixed up two rules.

One rule is

  • If you implement a non-default constructor but no default constructor, the "compiler-generated" default constructor is implicitly deleted.
    If you want one you need to define it yourself. (In C++11, you can use = default;.)

(There are more situations that cause "special member functions" to be deleted, but this is the one most people stumble across first.)

The other rule ("the rule of three", which is more a rule of thumb) is

  • If you need to implement a copy constructor, destructor, or assignment operator, you probably need to implement all three, so do it immediately.
molbdnilo
  • 55,783
  • 3
  • 31
  • 71