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 

Private method has been invokated as interface

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





PostPosted: Mon May 08, 2006 8:21 pm    Post subject: Private method has been invokated as interface Reply with quote



In program below a private method has been invokated as interface.

It is techically clear, but it is intuitively unclear.

------ foobar.cpp ------
#include <iostream>
using namespace std;


class Base
{
public:
virtual void foo() = 0;
};


class Derived : public Base
{
private:
void foo() { cout << "foo(): I am private" << endl; }
};


int main ()
{
Base* p = new Derived();

// ----------------------------------------------------------
// Checks public Base::foo() in compile-time;
// invokes private Derived::foo() in run-time
p->foo();
// ----------------------------------------------------------

return 0;
}


------ foobar.cpp ------

------ Run ------

$ ./a.exe

foo(): I am private

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


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


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





PostPosted: Tue May 09, 2006 1:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote



Alex Vinokur wrote:
Quote:
In program below a private method has been invokated as
interface.

It is techically clear, but it is intuitively unclear.

It's not very intuitive why you would want to do such a thing,
if that's what you mean. Many coding guidelines have a rule
against it (and possibly some compilers warn).

But what's your question? It may not be a useful possibility,
but it is orthogonal and coherent to allow it.

--
James Kanze kanze.james (AT) neuf (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

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





PostPosted: Tue May 09, 2006 8:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote



Alex Vinokur wrote:
Quote:
In program below a private method has been invokated as interface.

It is techically clear, but it is intuitively unclear.

------ foobar.cpp ------
#include <iostream
using namespace std;


class Base
{
public:
virtual void foo() = 0;
};


class Derived : public Base
{
private:
void foo() { cout << "foo(): I am private" << endl; }
};


int main ()
{
Base* p = new Derived();

// ----------------------------------------------------------
// Checks public Base::foo() in compile-time;
// invokes private Derived::foo() in run-time
p->foo();
// ----------------------------------------------------------

return 0;
}


------ foobar.cpp ------

------ Run ------

$ ./a.exe

foo(): I am private

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

This article explains it: http://www.ddj.com/dept/cpp/184403760


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





PostPosted: Tue May 09, 2006 8:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote

Alex Vinokur wrote:

Quote:
In program below a private method has been invokated as interface.

It is techically clear, but it is intuitively unclear.

No, it's perfectly clear.
The Base class in your example defines an interface and provides no
implementation (it uses the public pure virtual function to express this).
The Derived class provides an implementation (the body for the
overriding function), but has no public interface.
Taking them together, you have a working program.

The only thing that would be unintuitive in your example is this:

Base *p1 = new Derived();
Derived *p2 = new Derived();

p1->foo(); // OK
p2->foo(); // error!

This problem results from the fact that the Derived class modifies some
details of the interface which was already defined in the Base class.

In order to avoid this and making the distinction between the interface
and the implementation even more strict, here's the idiom that you can
find useful:

class Base
{
public:
void foo() { do_foo(); } // the interface
private:
virtual void do_foo() = 0; // no implementation
};

class Derived : public Base
{
public:
// no interface here!

private:
// the implementation:
virtual void do_foo() { /* ... */ }
};


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

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





PostPosted: Tue May 09, 2006 11:22 pm    Post subject: Re: Private method has been invokated as interface Reply with quote

In article <1147102381.813481.135920 (AT) j73g2000cwa (DOT) googlegroups.com>, Alex
Vinokur <alexvn (AT) users (DOT) sourceforge.net> writes
Quote:
In program below a private method has been invokated as interface.

It is techically clear, but it is intuitively unclear.


And the problem is?
Quote:
------ foobar.cpp ------
#include <iostream
using namespace std;


class Base
{
public:
virtual void foo() = 0;
};


class Derived : public Base
{
private:
void foo() { cout << "foo(): I am private" << endl; }
};

And this is a perfectly reasonable idiom if you do not want objects of

type Derived to be used directly but only by use of pointers or
references to Base.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


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





PostPosted: Wed May 10, 2006 12:21 am    Post subject: Re: Private method has been invokated as interface Reply with quote

Alex Vinokur wrote:
Quote:
In program below a private method has been invokated as interface.

It is techically clear, but it is intuitively unclear.

[snip example of declaring a public virtual in a base class, and then
overriding it with a private member in a derived class].

I am sorry, but I don't understand what your point is.

Yes, it is legal. No, it is not a good idea. So don't do that.


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





PostPosted: Wed May 10, 2006 1:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote

James Kanze wrote:
Quote:
Alex Vinokur wrote:
In program below a private method has been invokated as
interface.

It is techically clear, but it is intuitively unclear.

It's not very intuitive why you would want to do such a thing,
if that's what you mean. Many coding guidelines have a rule
against it (and possibly some compilers warn).

But what's your question?

From http://cplus.about.com/od/beginnerctutorial/l/aa070602b.htm :
"Private indicates that a member or method may be accessed only be

class methods and not by other parts of the program or from other
classes"

Quote:
From http://www.icce.rug.nl/documents/cplusplus/cplusplus03.html#l42 :
"the keyword private defines all subsequent fields as only accessible

by the code which is part of the struct (i.e., only accessible to its
member functions)."

Quote:
From http://www.cppreference.com/keywords/private.html :
"Private data of a class can only be accessed by members of that class,

except when friend is used"

Syntaxically private method of derived class in my example (see first
message in the thread) was accessed not by members of that class, but
from application. Is it actually private method?

Quote:
It may not be a useful possibility,
but it is orthogonal and coherent to allow it.



[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


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





PostPosted: Wed May 10, 2006 9:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote

Martin Bonner wrote:

Quote:
[snip example of declaring a public virtual in a base class, and then
overriding it with a private member in a derived class].

I am sorry, but I don't understand what your point is.

Yes, it is legal. No, it is not a good idea. So don't do that.

I disagree. It is a perfectly natural technique for implementing private
callback interfaces (although derivation is usually private in this case).

class Publisher
{
public:
void Subscribe (Subscriber* s);
// unsubscribing left out for brevity
};

class Subscriber
{
public:
virtual void Notify (Message m) = 0;
};

class MySubscriber : private Subscriber
{
public:
MySubscriber (Publisher p) { p.Subscribe (this); }
// rest of public interface

private:
// this is not intended for MySubscriber's clients
virtual void Notify (Message m);
};

Do you have a better way to implement this relationship and express the
intent of the design? In languages like Java or C#, this is impossible
because access level is tied to overriding. What is really annoying
about these languages is that they also prohibit you from circumventing
the restriction like this (C++):

class Subscriber
{
public:
void Notify (Message m) { this->DoNotify (m); }

private:
virtual void DoNotify (Message m) = 0;
};

because of another restriction that does not allow implementation in
interfaces.

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.


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





PostPosted: Wed May 10, 2006 10:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote

Alex Vinokur wrote:

Quote:

Syntaxically private method of derived class in my example (see first
message in the thread) was accessed not by members of that class, but
from application. Is it actually private method?


Yes, it is private. The various sources you quoted are simplifying
things, and slurring some of the details. Access control applies to the
static type of the object that is being used, not to its dynamic type.
When you have a pointer to base you can use things that are public in
the base (the static type), regardless of the actual type of the object
that it points to (the dynamic type).

--

Pete Becker
Roundhouse Consulting, Ltd.

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





PostPosted: Fri May 12, 2006 12:21 am    Post subject: Re: Private method has been invokated as interface Reply with quote

Gerhard Menzl wrote:
Quote:
Martin Bonner wrote:

[snip example of declaring a public virtual in a base class, and then
overriding it with a private member in a derived class].

I am sorry, but I don't understand what your point is.

Yes, it is legal. No, it is not a good idea. So don't do that.

I disagree. It is a perfectly natural technique for implementing private
callback interfaces (although derivation is usually private in this case).

Yup. Francis made a similar comment.

[snip example]

Quote:
Do you have a better way to implement this relationship and express the
intent of the design?

No. I think it is the best approach for that situation. I do think it
is worth a comment in the code (which you had in the example I
snipped), because it is (in my experience) unusual, and even
experienced programmers may be initiallly confused by it.


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





PostPosted: Fri May 12, 2006 12:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote

Martin Bonner wrote:

Quote:
No. I think it is the best approach for that situation. I do think
it is worth a comment in the code (which you had in the example I
snipped), because it is (in my experience) unusual, and even
experienced programmers may be initiallly confused by it.

Not in my experience. It's far easier to comprehend than, say, the
Curiously Recurring Template Pattern, and people don't bother to comment
that.

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.


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





PostPosted: Fri May 12, 2006 12:21 pm    Post subject: Re: Private method has been invokated as interface Reply with quote

Martin Bonner wrote:
Quote:
Gerhard Menzl wrote:
Martin Bonner wrote:

[snip example of declaring a public virtual in a base
class, and then overriding it with a private member in a
derived class].

I am sorry, but I don't understand what your point is.

Yes, it is legal. No, it is not a good idea. So don't do
that.

I disagree. It is a perfectly natural technique for
implementing private callback interfaces (although
derivation is usually private in this case).

Yup. Francis made a similar comment.

[snip example]

Do you have a better way to implement this relationship and
express the intent of the design?

No. I think it is the best approach for that situation. I do
think it is worth a comment in the code (which you had in the
example I snipped), because it is (in my experience) unusual,
and even experienced programmers may be initiallly confused by
it.

The key thing which made his use logical was the fact that the
inheritance was private as well. IMHO, this is even more
important than the comment -- private inheritance is a sign that
everything concerning the inhertance is an implementation
detail. It also means that a user of MySubscriber can't simply
use static_cast< Subscriber* >( ptrMySubscriber ) to get around
the private.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


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