-1

I have created a class, which has a constructor that takes a parameter. Am I right in thinking that I need a copy constructor here?

I have a class, which does not take pointers:

class xyz : public Node
{
public:
    xyz( uint8_t node);
    xyz(const xyz& cxyz);
    ~xyz();

private:
    uint8_t m_node;
    uint16_t m_gainxyz;

And the base:

class Node
{
public:

Node();
virtual ~Node();

protected:
    std::string  m_name;

And when I do this:

xyz xyz = Initxyz(node);

The compiler tells me to make a copy constructor.

where:

xyz PD::Initxyz(Source& inputNode)
{
    if (inputNode.getNodeNumber() > 10 )
        {
        xyz element(inputNode.getNodeNumber());
        element.setInputNode(inputNode);
        return element; 
        }
    else
        {
        std::cout << "ERROR IN XYZ CONFIGURATION" << std::endl;
        //Throw Exception;
        }
}

But according to what I have read on the web:

If the object has no pointers to dynamically allocated memory, a shallow copy is probably sufficient. Therefore the default copy constructor, default assignment operator, and default destructor are ok and you don't need to write your own.

http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

Is it also true that I must have one if I use a constructor with parameters?

user1876942
  • 1,411
  • 2
  • 20
  • 32
  • possible duplicate of [When do we have to use copy constructors?](http://stackoverflow.com/questions/3278625/when-do-we-have-to-use-copy-constructors) – Sachin Gupta May 15 '15 at 05:28
  • Let the compiler synthesize the copy constructor for you and so don't declare the copy constructor. Basically remove the line "xyz(const xyz& cxyz);" if you don't want to define it. – Nipun Talukdar May 15 '15 at 05:30
  • 1
    If `node` has type `Node`, then your problem isn't that you need a copy constructor, it's that you need a conversion constructor that takes a `Node` and returns an `xyz`. The former is supplied by default by the compiler; the latter is not. – ruakh May 15 '15 at 05:31
  • @NipunTalukdar -- Then it won't compile. The compiler will ask that I make that. – user1876942 May 15 '15 at 05:40
  • @ruakh The constructor takes a node number ID. – user1876942 May 15 '15 at 05:41
  • Based on the incomplete information you have provided, you don't need to write a copy constructor. The one generated by the compiler is fine. Is that what you're asking? – juanchopanza May 15 '15 at 05:45
  • @juanchopanza What info do you need? This is my question :) Why does the compiler tell me I need one when it seems that I don't – user1876942 May 15 '15 at 05:52
  • Enough information to reproduce the problem. Because you don't need to provide a copy constructor. You probably have some typo on code you haven't shown. – juanchopanza May 15 '15 at 05:54
  • 1
    "The compiler tells me to make a copy constructor." ??? You should actually post your code plus the compiler message. Because nothing you posted so far would generate such a message. – M.M May 15 '15 at 06:43
  • @MattMcNabb Yes, you're right. I thought I stripped it down to what I showed but I made a mistake. The error is that some libraries I use have put their copy constructor as private. – user1876942 May 15 '15 at 06:53

3 Answers3

0

I'll provide some normative reference in order to explain the thing formally.

If I correctly understand what you wanted, when you need to make the copy of your object then N4296::12.8/1 [class.copy]:

A class object can be copied or moved in two ways: by initialization (12.1, 8.5), including for function argument passing (5.2.2) and for function value return (6.6.3); and by assignment (5.18).

Actully, the copy constructor may be implicitly declared as deleted, so you'll give a compile-time error if you try to invoke it when you need to make a copy.

Relevant refernce:

An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/move constructor for a class X is defined as deleted (8.4.3) if X has:

(11.1) — a variant member with a non-trivial corresponding constructor and X is a union-like class,

(11.2) — a potentially constructed subobject type M (or array thereof) that cannot be copied/moved because overload resolution (13.3), as applied to M’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor,

(11.3) — any potentially constructed subobject of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or,

(11.4) — for the copy constructor, a non-static data member of rvalue reference type.

Where potentially construted subobject's defined as follows N4296::12/5

For a class, its non-static data members, its non-virtual direct base classes, and, if the class is not abstract (10.4), its virtual base classes are called its potentially constructed subobjects.

0

I have created a class, which has a constructor that takes a parameter. Am I right in thinking that I need a copy constructor here?

You do not need to provide a copy constructor for your classes. The compiler generated ones are sufficient.

Here is a working example based on your code:

#include <iostream>
#include <string>
#include <stdint.h>

class Node
{
public:

  Node() {}
  virtual ~Node() {}

protected:
  std::string  m_name;
};

class xyz : public Node
{
public:
    xyz( uint8_t node) {}

private:
    uint8_t m_node;
    uint16_t m_gainxyz;
};

xyz Initxyz(Node)
{
    return xyz(42);
}

int main()
{
    Node node;
    xyz xyz = Initxyz(node);
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • What you say is what I thought. I stripped my code down to only what I showed in hope of finding the bug. It didn't work. I will inspect my code more fully. I will post back when I know more. Thanks. – user1876942 May 15 '15 at 06:12
  • @user1876942 after stripping down your code, check that it still generates the same compiler errors before posting it – M.M May 15 '15 at 06:45
  • I found the error, one library I am using in the lower layers has put its copy constructor to private. Meaning, I guess that the compiler cannot use its own. – user1876942 May 15 '15 at 06:50
0

Copy constructor is required when you want to do deep copy instead of shallow copy.It means you are avoiding dangling pointer problem. Actually compiler is also provide default copy constructor but it works fine if you are not any string to constructor for copy. But If you want to copy string from one object of class to other object of class then you cant refer one memory address to two pointers. Try to do examples of copy constructor.