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 

Circular dependencies

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





PostPosted: Sat Nov 27, 2004 4:00 am    Post subject: Circular dependencies Reply with quote



Hi everybody:

I'm implementing a general C++ framework and I have a basic question
about circular dependencies:

I am creating a base class Object, my Object class has a method
defined as:
virtual String toString();

where String is defined as:

class String : public Object

This is equal to the Java or .NET Object class implementation, but it
is not a good OO design.

How can I implement that functionality without having circular
dependencies?

Best regards



ernesto

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

Back to top
Antoun Kanawati
Guest





PostPosted: Sat Nov 27, 2004 12:34 pm    Post subject: Re: Circular dependencies Reply with quote



ernesto basc?n pantoja wrote:
Quote:
Hi everybody:

I'm implementing a general C++ framework and I have a basic question
about circular dependencies:

I am creating a base class Object, my Object class has a method
defined as:
virtual String toString();

where String is defined as:

class String : public Object

This is equal to the Java or .NET Object class implementation, but it
is not a good OO design.

How can I implement that functionality without having circular
dependencies?

In Java, the declaration

String toString()

means that toString returns a reference to String, which is equivalent
to returning a pointer or a live reference in C++, but quite different
from returning a whole object.

You can approximate the behavior with something like:

class String;
class Object {
public:
void toString(String &) const;
};

class String : public Object {
.....
};

String toString(const Object &o) {
String s; o.toString(s); return s;
}
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

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

Back to top
Dave Harris
Guest





PostPosted: Sun Nov 28, 2004 2:11 am    Post subject: Re: Circular dependencies Reply with quote



[email]oopscene (AT) gmail (DOT) com[/email] (ernesto basc?n pantoja) wrote (abridged):
Quote:
I am creating a base class Object, my Object class has a method
defined as:
virtual String toString();

where String is defined as:

class String : public Object

This is equal to the Java or .NET Object class implementation, but it
is not a good OO design.

How can I implement that functionality without having circular
dependencies?

It is not quite "equal to the Java", in that C++ has String returned by
value and in Java the same syntax returns by reference. Either way, at the
language level there is no problem writing this - you can forward declare
String and provide the implementation afterwards, and the compiler is
happy. I think you are asking, not how to do this, but what the
alternatives are.

You could use a lower level format, such as a raw char array instead of
the String. And/or you could make toString not be a member function. For
example:

int Object::toBuffer( char *buf, int bufLen ) const {
return snprintf( buf, bufLen, "Object(%p)",
reinterpret_cast<void *>(this) );
}

String toString( const Object &object ) {
const int bufLen = 256;
char buf[bufLen];
int len = object.toBuffer( buf, bufLen );

if (len >= bufLen) {
// Buffer was too small.
const static char trail[] = "...";
const int trailLen = strlen( trail );
strcpy( buf + bufLen - trailLen - 1, trail );
len = bufLen - 1;
}
return String( buf, len );
}

Now instead of s = obj.toString() you write s = toString(obj). (I have
tried to avoid buffer overrun problems, but the above code is not tested.)

However, I don't agree that circular dependency is necessarily bad OO
design. In this case it means that (a) Object cannot be reused without
String and (b) String cannot be reused without Object.

I imagine (b) is not a problem, or rather that the problem is offset by
the benefits. You could try making String not inherit from Object, a
choice available in C++ but not in Java or .NET. Microsoft's MFC library
does that; it can be viable. It depends on what you want Object for in the
first place. For example, do you need to add a String * to an array of
Object *?

(a) is probably not a problem because strings are so ubiquitous than any
application which uses Object will probably also use Strings. In
particular, the point of adding a function like toString to Object is to
require all Objects to be able to describe themselves, and they will
almost certainly want to use String for their character manipulation. If
you seriously want to allow them to use different string classes, or if
you think they may not need that self-describing facility at all, then
consider using the Visitor pattern instead.

In practice it is often best to just say, "Every module which uses Object
also uses String and vice versa", and move on. Don't apply design
guidelines unthinkingly.

-- Dave Harris, Nottingham, UK

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

Back to top
Gianluca Silvestri
Guest





PostPosted: Tue Nov 30, 2004 11:38 pm    Post subject: Re: Circular dependencies Reply with quote


"ernesto basc?n pantoja" <oopscene (AT) gmail (DOT) com> ha scritto nel messaggio
news:7a86e6ed.0411261243.9a066ac (AT) posting (DOT) google.com...
Quote:
Hi everybody:

I'm implementing a general C++ framework and I have a basic question
about circular dependencies:

I am creating a base class Object, my Object class has a method
defined as:
virtual String toString();

where String is defined as:

class String : public Object

This is equal to the Java or .NET Object class implementation, but it
is not a good OO design.

How can I implement that functionality without having circular
dependencies?

//Object.hpp

class String;
class Object
{
public:
virtual String toString() const;
}

//Object.cpp
#include "String.hpp"
String Object::toString() const
{
/*
*/
}

HTH
Gianluca



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