C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Questions re: returning locally scoped objects

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
JohnD
Guest





PostPosted: Fri Aug 27, 2004 3:11 am    Post subject: Questions re: returning locally scoped objects Reply with quote



Hello,

I'm struggling with an issue, be it bad design or unclear understanding
of the language, or both.

I have a Session object in which I create a sub-object of type
Connection. Both are templates and I'm trying to avoid using pointers
and dynamically allocating the connection on the heap. The idea would
be that the connection would be closed when the object goes out of scope.

Here is a small snippet:

template<typename T>
class Session
{
public:

typedef T Connection;

Connection createConnection() {
Connection c;
// do some stuff with c for a connection
return Connection(c);
}
};

// Client of Session:
Session::Connection c = sess.createConnection();

// ... do stuff with a connection

The questions I have revolve around the return line. I don't want to
create a connection that lives as a private member variable of session
so is creating a temporary like that preferred, or are other
alternatives better?

I don't want to go the route of trying to create a generic factory, but
I also don't want a member variable of Session either.

Thanks for any and all suggestions.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Fri Aug 27, 2004 11:11 am    Post subject: Re: Questions re: returning locally scoped objects Reply with quote



"JohnD" <john_nospam (AT) tech-testing (DOT) com> wrote...
Quote:
I'm struggling with an issue, be it bad design or unclear understanding
of the language, or both.

I have a Session object in which I create a sub-object of type
Connection. Both are templates and I'm trying to avoid using pointers
and dynamically allocating the connection on the heap. The idea would
be that the connection would be closed when the object goes out of scope.

Here is a small snippet:

template class Session
{
public:

typedef T Connection;

Connection createConnection() {
Connection c;
// do some stuff with c for a connection
return Connection(c);

Why not just

return c;

? It does the same, but much less typing.

Quote:
}
};

// Client of Session:
Session::Connection c = sess.createConnection();

// ... do stuff with a connection

The questions I have revolve around the return line. I don't want to
create a connection that lives as a private member variable of session
so is creating a temporary like that preferred, or are other
alternatives better?

It depends on your goals. For example, if Connections are more or less
independent from the session, meaning that the session that created them
doesn't have to know about their activities, a separate object is fine.
If you do want the session to control the connections closer, it might
be better to keep them as members (and you probably want a collection of
Connections in a Session).

Quote:
I don't want to go the route of trying to create a generic factory, but
I also don't want a member variable of Session either.

If you don't want a member, don't create a member. What's the problem?
Why are you still asking questions? You don't want it who are we to tell
you to do it?

V


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
JohnD
Guest





PostPosted: Sat Aug 28, 2004 2:40 am    Post subject: Re: Questions re: returning locally scoped objects Reply with quote



Victor Bazarov wrote:

Quote:
Why not just

return c;

? It does the same, but much less typing.

I get compiler warnings regarding returning a temporary object. I could
simply ignore those and return anyway, as you state.

Quote:
If you don't want a member, don't create a member. What's the problem?
Why are you still asking questions? You don't want it who are we to tell
you to do it?

I was simply looking for ideas and how other people address/handle
similar situations.

Thanks for the feedback.
John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Frédéric Lachasse
Guest





PostPosted: Sun Aug 29, 2004 11:11 am    Post subject: Re: Questions re: returning locally scoped objects Reply with quote

"JohnD" <john_nospam (AT) tech-testing (DOT) com> wrote

Quote:
Hello,

I'm struggling with an issue, be it bad design or unclear understanding
of the language, or both.

I have a Session object in which I create a sub-object of type
Connection. Both are templates and I'm trying to avoid using pointers
and dynamically allocating the connection on the heap. The idea would
be that the connection would be closed when the object goes out of scope.

Here is a small snippet:

template class Session
{
public:

typedef T Connection;

Connection createConnection() {
Connection c;
// do some stuff with c for a connection
return Connection(c);
}
};

// Client of Session:
Session::Connection c = sess.createConnection();

// ... do stuff with a connection

The questions I have revolve around the return line. I don't want to
create a connection that lives as a private member variable of session
so is creating a temporary like that preferred, or are other
alternatives better?

I don't want to go the route of trying to create a generic factory, but
I also don't want a member variable of Session either.

First red flag in your code: you say you want to close the connection in the
destructor of the Connection class, but then you copy the object around,
creating several Connection objects to the same connection, each will be
destroyed, closing the connection, probably when you do not want to...

So simple solution: do not allow copying of Connection objects. That means
that they cannot be created through a factory (which is what the
createConnection() method is) but through a simple constructor:

Connection(Session& s);

If however you need to pass the connection around, use instead a smart
pointer approach, either directly or wrapped by your Connection class.

--
Frédéric Lachasse - ECP86


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Roland Pibinger
Guest





PostPosted: Sun Aug 29, 2004 10:48 pm    Post subject: Re: Questions re: returning locally scoped objects Reply with quote

On 27 Aug 2004 22:40:31 -0400, JohnD <john_nospam (AT) tech-testing (DOT) com>
wrote:

Quote:
I was simply looking for ideas and how other people address/handle
similar situations.

You have a parent object (=Session) that creates and owns (a) child
object(s) (=Connection). The child(ren) in turn might create and own
other child objects and so on. It makes no sense to allow the user to
duplicate (copy) Session, Connection, ... objects. This hierarchical
(graph-like) structure is quite common in software design.
Here is my proposal (it does not use templates and it interenally
creates an object on the heap):

class Connection {
public:
// SomeChildObject& getSomeChildObject();
int foo() {/* ... */}

private:
friend class Session;

Connection (int arg) { data = arg; }
int data; // some connection state

Connection (const Connection&);
Connection& operator= (const Connection&);
};


class Session
{
public:

Connection& getConnection() {
if (!pconn) {
pconn = new Connection (333);
// do some stuff with pconn for a connection
}
return *pconn;
}

void closeConnection() {
delete pconn;
pconn = 0;
}

Session() : pconn (0) {}
~Session() { closeConnection(); }

private:
Connection* pconn;

Session (const Session&);
Session& operator= (const Session&);
};


int main() {
Session session;
// a Connection object is created and owned only by a Session object

Connection& connection = session.getConnection();
connection.foo();
// ...
}

Of course this can be extended to make Session handle more than one
Connection(s).

The main point is that you restrict the way (ie. by whom) objects can
be created giving the user a clear guidance how the library (the set
of interdependent objects) is to be used. Everything is cleaned up at
the latest when the 'root' object goes out of scope which makes the
solution exception safe.

Best wishes,
Roland Pibinger

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Balog Pal
Guest





PostPosted: Fri Sep 03, 2004 12:29 pm    Post subject: Re: Questions re: returning locally scoped objects Reply with quote

"JohnD" <john_nospam (AT) tech-testing (DOT) com> wrote


Connection createConnection() {
Connection c;
// do some stuff with c for a connection
return Connection(c);
}

Quote:
Victor Bazarov wrote:
Why not just
return c;
? It does the same, but much less typing.

I get compiler warnings regarding returning a temporary object. I could
simply ignore those and return anyway, as you state.

in the form above only a broken compiler can issue that warning. The return
happens by value, so it doesn't matter from where is it copied.

That warning comes if your function returns reference:

Connection & createConnection() { // rest is the same

In that case you return reference to an objet that is on the stack and is
destroyed before there's a chance to use it. Though it may appear usable
depending on what and how will overwrite the same memory on the stack. So
you introduce a serious and possibly subtle bug that way.

If you apply the cast as above, that *removes the warning* but not the
problem! Quit using casts unless you can give a really good explanation to
anyone why it is there and how it changes the situation. (often that
explanation is required as a comment at the site)

Paul



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.