The Open Source Swiss Army Knife

/code/cPP/classes_cPP/
/code/cPP/classes_cPP/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /cPP   /classes_cPP 
Permalink: object_in_constructor.txt
Title: add
article options : please login   |  raw source view  

<header:how to create objects inside constructors>

my question on experts-exchange:

0--0-0000000000000000------------------------------
hi,
i only want to create another object inside a constructor but the compiler won't let me.
my header is

namespace dp {
class connection_user {

public
connection_user(string); connection_user(); ~connection_user(); string connectString; pqxx::connection C;

}; }

and there is a problem making C in connection_user(string) with the following

dp::connection_user::connection_user() {
std::cerr << " START dp::connection_user::connection_user() START" << std::endl;
// .... code whch makes connectString
C(connectString);
// more code
}

-------------end constructor --------------------------
but compiler won't allow the syntax

here is the compiler warning.
connection_user.cpp:In constructor `dp::connection_user::connection_user()':
connection_user.cpp:23: error: no match for call to `(pqxx::connection) (

std::string&)'
make[1]: *** [connection_user.o] &#50724;&#47448; 1
-------------- end error message -------------
though it is correct and works when i say pqxx::connection C(connectString) but that doesn't create the object in my class, but another object completely.

thanks one and all programmers.

0--0-0000000000000000------------------------------
Comment from Rosuav feedback
Date: 01/13/2005 05:10PM PST

Comment Accept

If you're trying to initialize the pqxx::connection object, you can't use the function call syntax. When you use:
C(connectString);

It translates into:
C.operator()(connectString);

To initialize it, you need the following:
dp::connection_user::connection_user():C(connectString) {
//rest of code

However this requires that you be able to build the connectString directly in that expression.

Alternatively, you'll have to code a default constructor for pqxx::connection, and have a separate initialize routine - which you could put in operator()() if you like, but I don't recommend it. Just create a new member function, and use:
C.init(connectString);

That'll fix everything - assuming you can cope with separating construction from initialization.


http://www.goingware.com/tips/parameters/membervars.html

How to Store and Initialize Member Variables
Minimize Dependencies by Storing Members as Pointers

What to you do if the member variable's header file is big and complex, or you
have a lot of member variables and don't want to slow down compilation and
encourage dependencies? The simple answer is to store the member variables as
pointers, and to allocate them with new in your class' constructor. (In
certain special cases you have references as member variables instead). You
will also need to be sure to delete them in your class' destructor. Here is a
first shot at it:


Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top