C++ protected member behaviour in Ada 95 
Author Message
 C++ protected member behaviour in Ada 95

Hi, how to get the same behaviour as C++ protected member in Ada 95 ?
ie  let's say :

package First_Package is

     procedure First_Procedure;

private -- so no client can access this part

    procedure Second_Procedure;

end First_Package;

----------------------------------------------------------------

package Second_Package is

 .....

end Second_Package;

I'd like to have a second package that could access the Second_Procedure
from the package first but without being a child of this package.
Do I have to declare a child package of First_Package in Second_Package ?



Sun, 15 Dec 2002 03:00:00 GMT  
 C++ protected member behaviour in Ada 95
Quote:
>I'd like to have a second package that could access the Second_Procedure
>from the package first but without being a child of this package.

  Easy.  If you want an arbitrary second package to be able to access
Second_Procedure, simply switch the order of the two lines:

Quote:
>private -- so no client can access this part
>   procedure Second_Procedure;

in First_Package.


Sun, 15 Dec 2002 03:00:00 GMT  
 C++ protected member behaviour in Ada 95



Quote:
> >I'd like to have a second package that could access the Second_Procedure
> >from the package first but without being a child of this package.
>   Easy.  If you want an arbitrary second package to be able to access
> Second_Procedure, simply switch the order of the two lines:

> >private -- so no client can access this part
> >   procedure Second_Procedure;

> in First_Package.

yes but I want to keep the procedure private from clients ..... just like
protected members in C++


Sun, 15 Dec 2002 03:00:00 GMT  
 C++ protected member behaviour in Ada 95

Quote:

>Hi, how to get the same behaviour as C++ protected member in Ada 95 ?
>ie  let's say :

>package First_Package is

>     procedure First_Procedure;

>private -- so no client can access this part

>    procedure Second_Procedure;

>end First_Package;

>----------------------------------------------------------------

>package Second_Package is

> .....

>end Second_Package;

>I'd like to have a second package that could access the Second_Procedure
>from the package first but without being a child of this package.
>Do I have to declare a child package of First_Package in Second_Package ?

In C++ inherited classes can access attributes in the protected part of
the parent class, but cannot access anything in the private part.

Packages is a different concept from classes, but Inherited packages in Ada
can access the private part of the parent, so private in Ada behaves more
like protected in C++. If you want to hide something from the child packages
in Ada, you can put it in the body of the (parent) package, and it will be
inaccessible from the inherited packages.

In you example, the second package could be inherited from the first.
The following dummy code shows this. The second package is able to
call the second (private) procedure, but not the third procedure
that is hidden in the body.

package First_Package is

   procedure First_Procedure;

private

   procedure Second_Procedure;

end First_Package;

package body First_Package is

   procedure First_Procedure is
   begin
      null;
   end;

   procedure Second_Procedure is
   begin
      null;
   end;

   procedure Third_Procedure is
   begin
      null;
   end;

end First_Package;

package First_Package.Second_Package is

   procedure Another_Procedure;

end First_Package.Second_Package;

package body First_Package.Second_Package is

   procedure Another_Procedure is
   begin
      Second_Procedure;
   end;

end First_Package.Second_Package;

with First_Package;
with First_Package.Second_Package;

procedure Main is
begin
   First_Package.Second_Package.Another_Procedure;
end;

--

ln -s /dev/null ~/.netscape/cookies



Sun, 15 Dec 2002 03:00:00 GMT  
 C++ protected member behaviour in Ada 95

Quote:

> private -- so no client can access this part

> procedure Second_Procedure;
> package Second_Package is

> .....

> end Second_Package;

> I'd like to have a second package that could access the
> Second_Procedure from the package first but without being a child of
> this package.

Let me get this straight. You want a procedure to be inaccessable to a
certian set of packages of your chooing, but accessable to another set.
Ada provides Child packages precisely to give you this functionality,
but you don't want to use them.

May I ask why?

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 15 Dec 2002 03:00:00 GMT  
 C++ protected member behaviour in Ada 95



Quote:


> > private -- so no client can access this part

> > procedure Second_Procedure;
> > package Second_Package is

> > .....

> > end Second_Package;

> > I'd like to have a second package that could access the
> > Second_Procedure from the package first but without being a child of
> > this package.

> Let me get this straight. You want a procedure to be inaccessable to a
> certian set of packages of your chooing, but accessable to another set.
> Ada provides Child packages precisely to give you this functionality,
> but you don't want to use them.

> May I ask why?

> --
> T.E.D.

I didn't want it because i didn't want, as a client of the package, to issue
a "with First_Package.Second_Package".
I found myself how to avoid it in the RM :
declare as everybody mentionned a child package First_Package.Second_Package
and rename this last package

with First_Package.Second_Package;
package myPackage renames First_Package.Second_Package;



Sun, 15 Dec 2002 03:00:00 GMT  
 C++ protected member behaviour in Ada 95

Quote:



> > May I ask why?

> I didn't want it because i didn't want, as a client of the package, to
> issue a "with First_Package.Second_Package".
> I found myself how to avoid it in the RM :
> declare as everybody mentionned a child package
> First_Package.Second_Package and rename this last package

I'd also like to point out that if you name the packages intelligently,
this doesn't have to be a problem either. Name the first package for the
general object or facility that the whole hierarchy provides, and the
second for what it adds to that facility. Often this is just a matter of
naming the first package using a word that was common to both packages,
and changing an underscore in the second package to a period. For
instance, your references to the second package would change from
something like Facility_Subfacility to Facility.Subfacility.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 15 Dec 2002 03:00:00 GMT  
 C++ protected member behaviour in Ada 95

Quote:

> package First_Package is

>      procedure First_Procedure;

> private -- so no client can access this part

>     procedure Second_Procedure;

> end First_Package;

> ----------------------------------------------------------------

> package Second_Package is

>  .....

> end Second_Package;

> I'd like to have a second package that could access the Second_Procedure
> from the package first but without being a child of this package.

Simply put, you can't. The whole point of putting something in the
private part is to prevent clients from accessing it. Only the package
and its children may access things in a package's private part.

I don't see the problem with using a child package. If X.Y is a package
name, it's a package name. Why is it worse to say

with X.Y;
...
   X.Y.Operation;

than to use a package name that doesn't contain a dot?

--
Jeff Carter
"I blow my nose on you."
Monty python & the Holy Grail



Sun, 15 Dec 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. LGL Seminar: GNU Ada 95 and Mixed Language Programming with Ada 95

2. LGL Seminar: GNU Ada 95 and Mixed Language Programming with Ada 95

3. Ada 95 and C++(again)

4. c++ 2 ADA 83/95 ?

5. ADA 95 and C++

6. Help me to chose between ADA 95 and C++

7. Help me to chose between ADA 95 and C++ (

8. ADA 95 C++ and Solaris

9. HELP >>Ada 95 /C++ / Embedded Softare Engineer

10. C++ to Ada 95 Source Code Translator

11. C++ to Ada 95

12. C++ to Ada 95

 

 
Powered by phpBB® Forum Software