0

So let's say I have a class called classname and this class has char* private data member.

In the class that I am taking, the professor wants us to do it like the one below, essentially creating a completely new array.

classname(const classname & source)
{
         c_string = new char[strlen(source.c_string)+1];

         strcpy(c_string, source.c_string);

}

But what if the two objects essentially share the same single array(that has already been created), like this one.

classname(const classname & source)
{
         c_string = source.c_string;
}

I understand this doesn't necessarily do any 'copying' and having different pointers (that belong to different objects) might be risky. But is it considered a copy constructor? Or is it so that in order for it to be a copy constructor the second array must also be created?

From what I've been reading I feel like the second one is the case, but I need a definite NO THAT'S NOT A COPY CONSTRUCTOR to be completely over this in my mind. Thank you.

Luc Aux
  • 149
  • 10
  • 5
    it is a copy constructor, because it has proper signature – Yola Feb 02 '18 at 08:02
  • 6
    Copy constructor is defined by its signature, *not* the content in the function body. – llllllllll Feb 02 '18 at 08:03
  • 2
    Correct syntax does not imply correct design. There are things compiler can do. The rest is the designer's job. –  Feb 02 '18 at 08:04
  • Possible duplicate of [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) –  Feb 02 '18 at 08:06
  • 2
    You are doing a [shallow copy rather than a deep copy](https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) but it's still a copy-constructor. – acraig5075 Feb 02 '18 at 08:06
  • 1
    Another example of a *legal* copy constructor `classname(const classname & source) { /* This space intentionally blank */ }`. You probably won't write that, but this is what the compiler will synthesise for a class with no data members – Caleth Feb 02 '18 at 09:34

1 Answers1

4

As commenters have all correctly pointed out, what you have is a copy constructor, regardless of the function's content.

It's a copy constructor because the standard says so (see 12.8.2):

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (...).

Since you have a non template constructor for classname, whose signature matches const classname&, you have a copy constructor. Maybe not a great copy constructor, maybe a broken one which doesn't even copy, but a copy constructor nonetheless.

Since I can't say it better than those who have commented, I'll simply quote them:

Copy constructor is defined by its signature, not the content in the function body.- @liliscent

 

You are doing a shallow copy rather than a deep copy but it's still a copy-constructor.-@acraig5075

Tas
  • 7,023
  • 3
  • 36
  • 51