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 

compostion design

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





PostPosted: Fri Jul 15, 2005 2:49 pm    Post subject: compostion design Reply with quote



Hello,
i have a simple graph (cGraph) class, which has vector of vertices
(cVertex). Now applications might want to add edges to these vertices
and may be change weights incase graph is weighted graph.
So application will get the corresponding vertex object based on vertex
indentifier (name or index) and then call methods in that object.

class cVertex;
class cGraph
{
public:
cVertex* getVertex(const char* identifier)
{ ... }
....
private:
vector<cVertex*> vertexList_;
};


My concern is i am returning reference(pointer in this case) to private
member variable and application can as well delete the object without
any knowledge of graph class.

alternate way is graph providing all interfaces of vertex and
internally doing it. but this cant be only if no alternate is
available. graph cant inherit from vertex, because vertex is not graph.


I am not sure returning const object will solve my problem.

i really appreciate your suggestions on this. hope i am clear.

thanks,
Ganesh.M.Ramaswamy


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

Back to top
Ioan C. Cordos
Guest





PostPosted: Fri Jul 15, 2005 4:08 pm    Post subject: Re: compostion design Reply with quote





[email]gmramaswamy (AT) gmail (DOT) com[/email] wrote:
Quote:
Hello,
i have a simple graph (cGraph) class, which has vector of vertices
(cVertex). Now applications might want to add edges to these vertices
and may be change weights incase graph is weighted graph.
So application will get the corresponding vertex object based on vertex
indentifier (name or index) and then call methods in that object.

class cVertex;
class cGraph
{
public:
cVertex* getVertex(const char* identifier)
{ ... }
....
private:
vector<cVertex*> vertexList_;
};


My concern is i am returning reference(pointer in this case) to private
member variable and application can as well delete the object without
any knowledge of graph class.

alternate way is graph providing all interfaces of vertex and
internally doing it. but this cant be only if no alternate is
available. graph cant inherit from vertex, because vertex is not graph.


I am not sure returning const object will solve my problem.

The constness can be cast out.

Quote:

i really appreciate your suggestions on this. hope i am clear.

You can return a reference to your internal object. This way nobody can
think of the need for releasing it.

Quote:

thanks,
Ganesh.M.Ramaswamy

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


Back to top
Sascha Müller
Guest





PostPosted: Sat Jul 16, 2005 10:20 am    Post subject: Re: compostion design Reply with quote



[email]gmramaswamy (AT) gmail (DOT) com[/email] wrote:
Quote:
class cVertex;
class cGraph
{
public:
cVertex* getVertex(const char* identifier)
{ ... }
....
private:
vector<cVertex*> vertexList_;
};

Have you considered an implementation without pointers? Like this:

class cVertex;
class cGraph
{
public:
cVertex &getVertex( const char* identifier )
{ ... }
private:
vector<cVertex> vertexList_;
}



Regards,

Sascha Müller

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


Back to top
Greg
Guest





PostPosted: Sat Jul 16, 2005 10:27 am    Post subject: Re: compostion design Reply with quote


Ioan C. Cordos wrote:
Quote:
gmramaswamy (AT) gmail (DOT) com wrote:
Hello,
i have a simple graph (cGraph) class, which has vector of vertices
(cVertex). Now applications might want to add edges to these vertices
and may be change weights incase graph is weighted graph.
So application will get the corresponding vertex object based on vertex
indentifier (name or index) and then call methods in that object.

class cVertex;
class cGraph
{
public:
cVertex* getVertex(const char* identifier)
{ ... }
....
private:
vector<cVertex*> vertexList_;
};


My concern is i am returning reference(pointer in this case) to private
member variable and application can as well delete the object without
any knowledge of graph class.

alternate way is graph providing all interfaces of vertex and
internally doing it. but this cant be only if no alternate is
available. graph cant inherit from vertex, because vertex is not graph.


I am not sure returning const object will solve my problem.

The constness can be cast out.


i really appreciate your suggestions on this. hope i am clear.

You can return a reference to your internal object. This way nobody can
think of the need for releasing it.


thanks,
Ganesh.M.Ramaswamy



Even with a reference to a data member a client may change its value or
otherwise render it incompatible with the owning class's design.

I would recommend making the vertex reference that the cGraph returns
to the client, a "handle", otherwise known as an opaque type. To do so
simply typedef a VertexRef as a pointer to a forwardly-declared, but
never defined, type:

typedef const cVertex * VertexRef;

Although the client still receives the vertex pointer, nothing about
the object it points to is published in the client API. Instead the
library publishes a set of routines or class interfaces that accept
VertexRef types as arguments, but aside from those routines there is
nothing interesting that a client in possesion of a VertexRef can do
with it.

The benefit of this design is that the client's use of the vertex
reference is always mediated by the implementation. In a sense the
client does not "own" the VertexRef, so it is appropriate to set
limitations on the client's use of one. And the issue is not that the
implementation doesn't trust the client, or doesn't think that the
client knows what they are doing. Rather the information is protected
simply to minimize the chance for error. And the client benefits from
this arrangement since an opaque type makes it easier to ensure the
integrity of the implementation's structured data.

Greg


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


Back to top
Le Chaud Lapin
Guest





PostPosted: Sat Jul 16, 2005 11:46 pm    Post subject: Re: compostion design Reply with quote

Sascha Müller wrote:
Quote:
Have you considered an implementation without pointers? Like this:

class cVertex;
class cGraph
{
public:
cVertex &getVertex( const char* identifier )
{ ... }
private:
vector<cVertex> vertexList_;
}

Yes. This is a good example where you can crete a design entirely
devoid of pointers (which IMO is always desirable). And with
templates, you no longer have to commit to the type of 'identifier':

template <typename Index, typename Vertex, typename Edge>
class Graph
{
// various map<>'s,
puublic :
// various member functions for adding/deleting vertices/edges
// various graph-theortic algorithms (Dijkstra/Prim, etc.)

} ;

An Index identifies a vertex. The Vertex itself could be a scalar or
an aggregate, like a Node. An Edge could contain whatever members you
wanted (weight, etc) that permit creation of various graph-theoretic
algorithms.

-Chaud Lapin-


[ 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.