473,480 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to use friend ?

Hi,
I have a parser interface , which is
class IParser{
public:
void readHeader(Document& doc)= 0;
void readCC(CC& cc) = 0;
};
Now, there are 3 kinds of parser which extends (implements) IParser,
one for XML, ne for Config, & one for binary.
They need to get the Document & CC class and fill the data, in the
document and CC.

Now document data's (like name, version) are private, and have a get
method. Once the document is set by the IParser implementation, it
should not be changed.
document class is like
class Document{
private:
std::string _name;
std::string _version;
public:
std::string getName() const{
return _name;
}
std::string getVersion() const{
return _version;
}
};
Now, declaring IParser as friend in document class is not helping, as
the actual implementation like class XMLParser, class ConfigParser are
not friend of Document, thus cant access Document private variables.
The classes are decalred like,
class XMLParser :public IParser{
public:
void readHeader(Document& doc){
...
}
void readCC(CC& cc){
...
}
};

How to do this with or without friend? i.e. any class derived from
IParser can access the private variables of document?
any other better way this can be implemented? I had thought to create
the Document class inside readHeader, and initialize parameters
throught ctor, but Document class has a large number of such member
variables, and difficult to initialize all of them through ctor.
thanks in advance for advice ...
abir

Aug 8 '06 #1
8 1461
"toton" <ab*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
: Hi,
: I have a parser interface , which is
: class IParser{
: public:
: void readHeader(Document& doc)= 0;
: void readCC(CC& cc) = 0;
: };
: Now, there are 3 kinds of parser which extends (implements) IParser,
: one for XML, ne for Config, & one for binary.
: They need to get the Document & CC class and fill the data, in the
: document and CC.
:
: Now document data's (like name, version) are private, and have a get
: method. Once the document is set by the IParser implementation, it
: should not be changed.
....
: Now, declaring IParser as friend in document class is not helping, as
: the actual implementation like class XMLParser, class ConfigParser are
: not friend of Document, thus cant access Document private variables.
....
: How to do this with or without friend? i.e. any class derived from
: IParser can access the private variables of document?
: any other better way this can be implemented?

If "IParser" was the root of a hierarchy of classes that have special
access to manipulating documents, I would include in IParser protected
methods that allow subclasses to perform on a Document all the
required operations.

But in this case, IParser-s seem more like factory objects, they
act to create document objects which are then accessed using
a specific interface.
Can the parser create document objects by calling a constructor ?
If needed, parser could fill in a DocumentData structure, which
is then passed to the constructor of Document.
[ maybe Document can have DocumentData as a member ]

Alternativley, the Document instance could be calling the
IParser interface to obtain data packets that it will
take care of adding to itself.
Of course, I have no definitive answer given the provided
level of detail.
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 8 '06 #2

Ivan Vecerina wrote:
"toton" <ab*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
: Hi,
: I have a parser interface , which is
: class IParser{
: public:
: void readHeader(Document& doc)= 0;
: void readCC(CC& cc) = 0;
: };
: Now, there are 3 kinds of parser which extends (implements) IParser,
: one for XML, ne for Config, & one for binary.
: They need to get the Document & CC class and fill the data, in the
: document and CC.
:
: Now document data's (like name, version) are private, and have a get
: method. Once the document is set by the IParser implementation, it
: should not be changed.
...
: Now, declaring IParser as friend in document class is not helping, as
: the actual implementation like class XMLParser, class ConfigParser are
: not friend of Document, thus cant access Document private variables.
...
: How to do this with or without friend? i.e. any class derived from
: IParser can access the private variables of document?
: any other better way this can be implemented?

If "IParser" was the root of a hierarchy of classes that have special
access to manipulating documents, I would include in IParser protected
methods that allow subclasses to perform on a Document all the
required operations.

But in this case, IParser-s seem more like factory objects, they
act to create document objects which are then accessed using
a specific interface.
Can the parser create document objects by calling a constructor ?
If needed, parser could fill in a DocumentData structure, which
is then passed to the constructor of Document.
[ maybe Document can have DocumentData as a member ]
IParser is the interface, which all parser will follow. a ParserFactory
class is also there to return the appropriate parser based on the file
extension.
Only any kind of IParser need to manipulate Document class.
and yes, document class is in the root of hierarchy, doesn't get
inherited.
In language like Java, it can be done having them in same package, and
declaring all the Document fields package private.
Alternative, what I thought now, is to accept an IParser object in the
document set method, so that only any kind of IParser can set the
document fields.
another alternative may be declaring the private fields in document as
protected, and in parser implementation, inheriting Document protected
way, but that breaks the UML rule, like Parser becomes a document,
which is bad.
I am looking a generalized way of giving access of the fields to a
class and its childs (which friend doesn't give) ...
Alternativley, the Document instance could be calling the
IParser interface to obtain data packets that it will
take care of adding to itself.
Of course, I have no definitive answer given the provided
level of detail.
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 8 '06 #3
"toton" <ab*******@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
:
: Ivan Vecerina wrote:
: "toton" <ab*******@gmail.comwrote in message
: news:11**********************@m73g2000cwd.googlegr oups.com...
: : Hi,
: : I have a parser interface , which is
: : class IParser{
: : public:
: : void readHeader(Document& doc)= 0;
: : void readCC(CC& cc) = 0;
: : };
: : Now, there are 3 kinds of parser which extends (implements)
IParser,
: : one for XML, ne for Config, & one for binary.
: : They need to get the Document & CC class and fill the data, in the
: : document and CC.
: :
: : Now document data's (like name, version) are private, and have a get
: : method. Once the document is set by the IParser implementation, it
: : should not be changed.
: ...
: : Now, declaring IParser as friend in document class is not helping,
as
: : the actual implementation like class XMLParser, class ConfigParser
are
: : not friend of Document, thus cant access Document private variables.
: ...
: : How to do this with or without friend? i.e. any class derived from
: : IParser can access the private variables of document?
: : any other better way this can be implemented?
: >
: If "IParser" was the root of a hierarchy of classes that have special
: access to manipulating documents, I would include in IParser protected
: methods that allow subclasses to perform on a Document all the
: required operations.
: >
: But in this case, IParser-s seem more like factory objects, they
: act to create document objects which are then accessed using
: a specific interface.
: Can the parser create document objects by calling a constructor ?
: If needed, parser could fill in a DocumentData structure, which
: is then passed to the constructor of Document.
: [ maybe Document can have DocumentData as a member ]
:
: IParser is the interface, which all parser will follow. a ParserFactory
: class is also there to return the appropriate parser based on the file
: extension.
I understood this. I was indicating that IParser seems to act
as a factory for Document objects.

: Only any kind of IParser need to manipulate Document class.
Well, I guess that other classes use it.
Note that if your point is that only IParser "modifies"
a Document, then a better approach might be to expose
only const-references to Document to the rest of the
application.

: and yes, document class is in the root of hierarchy, doesn't get
: inherited.
: In language like Java, it can be done having them in same package, and
: declaring all the Document fields package private.
Putting all classes in the same package, you can assume that all
classes know about each other. The equivalent of this would be
to declare each IParser subclass as 'friend' of Document.

: Alternative, what I thought now, is to accept an IParser object in the
: document set method, so that only any kind of IParser can set the
: document fields.
: another alternative may be declaring the private fields in document as
: protected, and in parser implementation, inheriting Document protected
: way, but that breaks the UML rule, like Parser becomes a document,
: which is bad.
Both approaches are bad.

: I am looking a generalized way of giving access of the fields to a
: class and its childs (which friend doesn't give) ...
This would be achieved with my first suggestion:
having protected methods in the IParser base class that
allow to modify Document.

again:
: Of course, I have no definitive answer given the provided
: level of detail.

Ivan ;)
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 8 '06 #4

Ivan Vecerina wrote:
"toton" <ab*******@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
:
: Ivan Vecerina wrote:
: "toton" <ab*******@gmail.comwrote in message
: news:11**********************@m73g2000cwd.googlegr oups.com...
: : Hi,
: : I have a parser interface , which is
: : class IParser{
: : public:
: : void readHeader(Document& doc)= 0;
: : void readCC(CC& cc) = 0;
: : };
: : Now, there are 3 kinds of parser which extends (implements)
IParser,
: : one for XML, ne for Config, & one for binary.
: : They need to get the Document & CC class and fill the data, in the
: : document and CC.
: :
: : Now document data's (like name, version) are private, and have a get
: : method. Once the document is set by the IParser implementation, it
: : should not be changed.
: ...
: : Now, declaring IParser as friend in document class is not helping,
as
: : the actual implementation like class XMLParser, class ConfigParser
are
: : not friend of Document, thus cant access Document private variables.
: ...
: : How to do this with or without friend? i.e. any class derived from
: : IParser can access the private variables of document?
: : any other better way this can be implemented?
: >
: If "IParser" was the root of a hierarchy of classes that have special
: access to manipulating documents, I would include in IParser protected
: methods that allow subclasses to perform on a Document all the
: required operations.
: >
: But in this case, IParser-s seem more like factory objects, they
: act to create document objects which are then accessed using
: a specific interface.
: Can the parser create document objects by calling a constructor ?
: If needed, parser could fill in a DocumentData structure, which
: is then passed to the constructor of Document.
: [ maybe Document can have DocumentData as a member ]
:
: IParser is the interface, which all parser will follow. a ParserFactory
: class is also there to return the appropriate parser based on the file
: extension.
I understood this. I was indicating that IParser seems to act
as a factory for Document objects.

: Only any kind of IParser need to manipulate Document class.
Well, I guess that other classes use it.
Note that if your point is that only IParser "modifies"
a Document, then a better approach might be to expose
only const-references to Document to the rest of the
application.
Can you give me a little more detail how to exactly do it? I believe
some kind of pattern exists for this, irrespective of language of
implementation, only not finding it ( apparently not present in GOF
book) ...
Again, if you need a little detail,
The actual file contains headers and several CC (kind of data,
basically a set of ints with a start & end mark)
I have IParser interface, which reads a document and puts the header
in the Document class, and reads CC's one by one on a readCC call and
store them in a deque.

The rest of the program gets the Document , and deque of CC's and
process them, but don't modify. The gets the Document & CC's from a
Session class which holds them.

Thus, any IParser implementation can read a file, given the name, can
read the header, create a Document from it and put it in Session.
similarly, upon call to readCC, it can read a CC form the file and put
it in the deque of Session. Also actually Session creates an IParser
implementation based on the file extension.
The remaining program can use the Document & CC deque returned by the
Session. For simplicity I can assume Session is a singleton, which
holds all the processing data.
The remaing things are flexible, how to have association between the
classes etc.
I am searching for some good pattern for this rather than the hacks
which I had thought :) .
: and yes, document class is in the root of hierarchy, doesn't get
: inherited.
: In language like Java, it can be done having them in same package, and
: declaring all the Document fields package private.
Putting all classes in the same package, you can assume that all
classes know about each other. The equivalent of this would be
to declare each IParser subclass as 'friend' of Document.
I dont know how many class will be derived from IParser, thus cant
entry a friend for each in the Document class.
: Alternative, what I thought now, is to accept an IParser object in the
: document set method, so that only any kind of IParser can set the
: document fields.
: another alternative may be declaring the private fields in document as
: protected, and in parser implementation, inheriting Document protected
: way, but that breaks the UML rule, like Parser becomes a document,
: which is bad.
Both approaches are bad.
I know :) , I had said last one is bad to denote it is "extremely bad"
....
: I am looking a generalized way of giving access of the fields to a
: class and its childs (which friend doesn't give) ...
This would be achieved with my first suggestion:
having protected methods in the IParser base class that
allow to modify Document.

again:
: Of course, I have no definitive answer given the provided
: level of detail.

Ivan ;)
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 9 '06 #5
"toton" <ab*******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
: : Only any kind of IParser need to manipulate Document class.
: Well, I guess that other classes use it.
: Note that if your point is that only IParser "modifies"
: a Document, then a better approach might be to expose
: only const-references to Document to the rest of the
: application.
: Can you give me a little more detail how to exactly do it? I believe
: some kind of pattern exists for this, irrespective of language of
: implementation, only not finding it ( apparently not present in GOF
: book) ...
Well, one illustration could be:
class MyDocHandler
{
public:
//...

Document const& loadDocument( string const& fileName )
{
auto_ptr<IParserparser = createParserForFile( fileName );
parser.load( curDoc_ );
return curDoc_;
}

private:
Document curDoc_;
};
Only DocHandler has access to the document in non-const form.
The rest of the application would be unable to modify
the document instance, having only a const-reference to it.

The other option I had mentioned was that IParser would create
data in a DocumentData struct. A document is then created to
store "DocumentData" and encapsulate its manipulation.

But in my related applications, it is usually the Document
class that calls the Parser to obtain items/contents that
it will store within itself.

: Again, if you need a little detail,
Actually, the better design depends more on the overview
of the system (I should have said "information" rather than detail).
Yet, I hope the above may help a bit.
Cheers -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 9 '06 #6

Ivan Vecerina wrote:
"toton" <ab*******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
: : Only any kind of IParser need to manipulate Document class.
: Well, I guess that other classes use it.
: Note that if your point is that only IParser "modifies"
: a Document, then a better approach might be to expose
: only const-references to Document to the rest of the
: application.
: Can you give me a little more detail how to exactly do it? I believe
: some kind of pattern exists for this, irrespective of language of
: implementation, only not finding it ( apparently not present in GOF
: book) ...
Well, one illustration could be:
class MyDocHandler
{
public:
//...

Document const& loadDocument( string const& fileName )
{
auto_ptr<IParserparser = createParserForFile( fileName );
parser.load( curDoc_ );
return curDoc_;
}

private:
Document curDoc_;
};
Only DocHandler has access to the document in non-const form.
The rest of the application would be unable to modify
the document instance, having only a const-reference to it.
I even can't access getters for the Document class !!!!!!!!!.
What to do with this class?
The other option I had mentioned was that IParser would create
data in a DocumentData struct. A document is then created to
store "DocumentData" and encapsulate its manipulation.

But in my related applications, it is usually the Document
class that calls the Parser to obtain items/contents that
it will store within itself.

: Again, if you need a little detail,
Actually, the better design depends more on the overview
of the system (I should have said "information" rather than detail).
Yet, I hope the above may help a bit.
Cheers -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 10 '06 #7
"toton" <ab*******@gmail.comwrote in message
news:11********************@p79g2000cwp.googlegrou ps.com...
: Ivan Vecerina wrote:
: Document const& loadDocument( string const& fileName )
[...]
: I even can't access getters for the Document class !!!!!!!!!.
: What to do with this class?
You probably need to read about 'const' usage in C++.
In C++, the interface of a class can have a 'const' subset:
the operations that will not modify the contents of an instance.
A member function that does not modify the instance it is
called on is identified by appending 'const' to its signature:

class Document
{
public:
//.....

// const members: can be called on a const or non-const instance
std::string const& getTitle() const { return title_; }
Point getViewSize() const;

// non-const members: can only be called on a non-const instance
void loadContents( DocumentData const& sourceBuffer );

private:
std::string title_;
//.....
};

Point Document::getViewSize() const
{ .... }
const is one of the features I miss the most when working
with non-C++ code, it's worth taking advantage of.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Aug 10 '06 #8

Ivan Vecerina wrote:
"toton" <ab*******@gmail.comwrote in message
news:11********************@p79g2000cwp.googlegrou ps.com...
: Ivan Vecerina wrote:
: Document const& loadDocument( string const& fileName )
[...]
: I even can't access getters for the Document class !!!!!!!!!.
: What to do with this class?
You probably need to read about 'const' usage in C++.
In C++, the interface of a class can have a 'const' subset:
the operations that will not modify the contents of an instance.
A member function that does not modify the instance it is
called on is identified by appending 'const' to its signature:

class Document
{
public:
//.....

// const members: can be called on a const or non-const instance
std::string const& getTitle() const { return title_; }
Point getViewSize() const;
I had just forgot to type const exactly for the getter which I had
called. :)
// non-const members: can only be called on a non-const instance
void loadContents( DocumentData const& sourceBuffer );

private:
std::string title_;
//.....
};

Point Document::getViewSize() const
{ .... }
const is one of the features I miss the most when working
with non-C++ code, it's worth taking advantage of.
Me too...
Thanks ...
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Aug 10 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4419
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
15
6549
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
7
13972
by: Steve | last post by:
I'm just curious, why did give VB.Net a Friend keyword but not C#?
2
13003
by: K. Shier | last post by:
Friend variables "are accessible from within the program that contains their declaration and from anywhere else in the same assembly." according to the VS.Net help. This sounds like a pretty broad...
9
3718
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized...
2
2037
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
5
1808
by: Steven T. Hatton | last post by:
This note appears in the discussion of name hiding and uniqueness: §3.3 #4 This note is item #6 in the discussion of "Point of declaration" §3.3.1 #6 What exactly do these statements mean?...
3
3476
by: Filimon Roukoutakis | last post by:
Dear all, I have the following concept name { space1 { class Friend { };
5
1900
by: tuananh87vn | last post by:
hi, I'mm writing script for adding friend to a friend list.i'm using a form including friend_name (text), message (textarea) and below is my code: if(isset($_POST)) { ...
6
3143
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.