• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

do I need to write my own copy constructor?

Hi,

I am confused about copy constructors.

The class I'm writing, Person, has various member vars and methods. One of its private members is an SQL connection object (provoided by an SQL library).

My Person class will not compile:

error C2248: 'SAConnection::SAConnection' : cannot access private member declared in class 'SAConnection'  <-- this is the SQL connection object

I think it is because the author of the SQL library explicitly prohibited the copy constructor. From his header file:

private:
      // disable copy constructor
      SACommand(const SACommand &);

So, it seems that my Person copy constructor (generated by the compiler) is not working because it is not allowed to copy the SACommand object.

How could I get around this?  

I think I need to explicitly define the Person copy constructor, but I do not know how to do this. Any tips?

thanks

Kevin
0
highqllc
Asked:
highqllc
  • 5
  • 5
  • 2
1 Solution
 
jkrCommented:
A copy constructor usually is created like

Person(const Person& r) {

  // assign r's members to their respective counterparts
}

However, I doubt this is the reason. What code is it that generates the above error?
0
 
highqllcAuthor Commented:
Hi jkr,

it is not my code that is generating the error, it is the compiler-generated copy constructor for the Person class.  
0
 
jkrCommented:
Yes, but in what context? (Code, please ;o)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
highqllcAuthor Commented:
hm...  I'm really not sure what code to send  you.  Here is the full VS error.  Line #138 is simply the closing brace of the Person class.  What other information could I send?

1>Person.h(138) : error C2248: 'SAConnection::SAConnection' : cannot access private member declared in class 'SAConnection'
1>        ..\..\SQLAPI\include\sqlapi.h(687) : see declaration of 'SAConnection::SAConnection'
1>        ..\..\SQLAPI\include\sqlapi.h(679) : see declaration of 'SAConnection'
1>        This diagnostic occurred in the compiler generated function 'Person(const Person &)'
0
 
jkrCommented:
>>I'm really not sure what code to send  you

The code around line 138 in Person.h...
0
 
highqllcAuthor Commented:
Line 138 is the last line of my Person.h header file.  I don't want to include the entire header file here because it's, well, 138 lines of code!   And, of course, my real class is not called Person, I'm just trying to simply everything for the purpose of this discussion.  So, basically, I have:

#include <sqlapi.h>

class Person {
   
public:
    Person(void);
   ~Person(void);

public:
    // some public members

private:
   SAConnection m_sqlCon;
   SACommand m_sqlCmd;
   // some other private members

};    <------ error right here


When I remove the two SQL-related member variables, everything is fine.   Is that enough info?
0
 
jkrCommented:
Yes, that helps a lot. Yet a user-defined copy constructor will not get you around that issue, since you'd have to manually copy the instances, which is likely to cause the same errors. I assume that you are using that class with some STL containers?

BTW, would it work for you to use

class Person {
   
public:
    Person(void);
   ~Person(void);

public:
    // some public members

private:
   SAConnection* m_psqlCon;
   SACommand* m_psqlCmd;
   // some other private members

};

and create the members via 'new' in your constructor? That should at least allow to get rid of that problem.
0
 
IchijoCommented:
Could you take the SAConnection object out of the class, initialize it outside of the class and provide a pointer to it in the class? Then you wouldn't have to worry about any memory management or copy constructors.
0
 
highqllcAuthor Commented:
Hi,

I need each person to have his own SAConnection.  So it would be nice that when a new Person object is created, a new SAConnection is created for him....

0
 
jkrCommented:
Well, what I meant was

class Person {
   
public:
    Person(void) { m_psqlCon = new SAConnection; m_psqlCmd = new SACommand;}
   ~Person(void);{ delete m_psqlCon; delete m_psqlCmd;}

public:
    // some public members

private:
   SAConnection* m_psqlCon;
   SACommand* m_psqlCmd;
   // some other private members

};

which would fulfil that requirement
0
 
IchijoCommented:
That will compile, but if you try to copy one Person object into another Person object using copy initialization, won't it cause the same memory to be deleted twice?
0
 
highqllcAuthor Commented:
hmmm... that's a good question.   JKR, what do you think?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now