Link to home
Create AccountLog in
Avatar of highqllc
highqllc

asked on

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
Avatar of jkr
jkr
Flag of Germany image

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?
Avatar of highqllc
highqllc

ASKER

Hi jkr,

it is not my code that is generating the error, it is the compiler-generated copy constructor for the Person class.  
Yes, but in what context? (Code, please ;o)
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 &)'
>>I'm really not sure what code to send  you

The code around line 138 in Person.h...
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?
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.
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.
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....

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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?
hmmm... that's a good question.   JKR, what do you think?