473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Can inserter/extractor operator overrides be made run-time polymorphic?

Since these operators can't be member functions, and since
friend functions can't be declared virtual, how do I make my
inserters and extractors polymorphic?

--Randy Yates

Jan 12 '06 #1
23 2476
Randy wrote:
Since these operators can't be member functions, and since
friend functions can't be declared virtual, how do I make my
inserters and extractors polymorphic?


Make another member virtual and call it in your operator <<.

struct Base {
virtual ostream& output(ostream&) const = 0;
};

ostream& operator << (ostream& os, Base const& b) {
return b.output(os);
}

struct Derived : Base { ...

V
Jan 12 '06 #2
On 12 Jan 2006 11:25:27 -0800, "Randy" <ya***@ieee.org> wrote:
Since these operators can't be member functions, and since
friend functions can't be declared virtual, how do I make my
inserters and extractors polymorphic?


by operator overloading, eg.

ostream& operator<< (ostream& out, const Base& base);
ostream& operator<< (ostream& out, const Derived& derived);
....

Best wishes,
Roland Pibinger
Jan 12 '06 #3
I get it. Thanks Victor.

--Randy

Jan 12 '06 #4
Really? So if I did that and I had

class Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);
}

and

class Derived : public Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);
}

and then

int main(int argc, char* argv[])
{
Base* b;
b = new Derived;
cout << b;
return 0;
}

the Derived inserter would be used and not the Base inserter?

Thanks Roland.

--Randy

Jan 12 '06 #5
TB
Randy sade:

int main(int argc, char* argv[])
{
Base* b;
b = new Derived;
cout << b;
cout << *b;
return 0;
}

TB
Jan 12 '06 #6
On 12 Jan 2006 12:20:22 -0800, "Randy" <ya***@ieee.org> wrote:
Really? So if I did that and I had

class Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);
}

and

class Derived : public Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);
friend ostream& operator<< (ostream& os, const Derived& derived);
}

and then

int main(int argc, char* argv[])
{
Base* b;
b = new Derived;
No need to 'new' anything. C++ objects can exist on the stack.
cout << b;
return 0;
}

the Derived inserter would be used and not the Base inserter?


It's just function overloading (like in Java or C#).

Best wishes,
Roland Pibinger
Jan 12 '06 #7
rp*****@yahoo.com (Roland Pibinger) writes:
On 12 Jan 2006 12:20:22 -0800, "Randy" <ya***@ieee.org> wrote:
Really? So if I did that and I had

class Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);
}

and

class Derived : public Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);


friend ostream& operator<< (ostream& os, const Derived& derived);


Right.
}

and then

int main(int argc, char* argv[])
{
Base* b;
b = new Derived;


No need to 'new' anything. C++ objects can exist on the stack.
cout << b;
return 0;
}

the Derived inserter would be used and not the Base inserter?


It's just function overloading (like in Java or C#).


I'm confused - how can it "just be function overloading?"
This is a run-time selection, not compile-time, right?

I thought the usual usage of the term "function overloading"
was reserved for compile-time overloading, while "polymorphism"
was used for run-time overloading. Am I mistaken?
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% <ya***@ieee.org> % *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jan 13 '06 #8
rp*****@yahoo.com (Roland Pibinger) writes:
[...]
On 12 Jan 2006 12:20:22 -0800, "Randy" <ya***@ieee.org> wrote:
int main(int argc, char* argv[])
{
Base* b;
b = new Derived;


No need to 'new' anything. C++ objects can exist on the stack.


True, but my point was to question run-time polymorphism, not
compile-time polymorphism.
--
% Randy Yates % "My Shangri-la has gone away, fading like
%% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
%%% 919-577-9882 %
%%%% <ya***@ieee.org> % 'Shangri-La', *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jan 13 '06 #9
"Randy Yates" <ya***@ieee.org> wrote in message
news:bq**********@ieee.org...
rp*****@yahoo.com (Roland Pibinger) writes:
On 12 Jan 2006 12:20:22 -0800, "Randy" <ya***@ieee.org> wrote:
Really? So if I did that and I had

class Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);
}

and

class Derived : public Base
{
...
friend ostream& operator<< (ostream& os, const Base& base);


friend ostream& operator<< (ostream& os, const Derived& derived);


Right.
}

and then

int main(int argc, char* argv[])
{
Base* b;
b = new Derived;


No need to 'new' anything. C++ objects can exist on the stack.
cout << b;
return 0;
}

the Derived inserter would be used and not the Base inserter?


It's just function overloading (like in Java or C#).


I'm confused - how can it "just be function overloading?"
This is a run-time selection, not compile-time, right?

I thought the usual usage of the term "function overloading"
was reserved for compile-time overloading, while "polymorphism"
was used for run-time overloading. Am I mistaken?


There is run time and compile time polymorphism AFAIK.
Jan 13 '06 #10
On Thu, 12 Jan 2006 19:21:30 -0800, "Jim Langston"
<ta*******@rocketmail.com> wrote:
I thought the usual usage of the term "function overloading"
was reserved for compile-time overloading, while "polymorphism"
was used for run-time overloading. Am I mistaken?


There is run time and compile time polymorphism AFAIK.


The function call is resolved at compile time. I guess Victor Bazarov
has the right answer how to make inserter/extractor "run-time
polymorphic". Sorry for causing confusion.

Best wishes,
Roland Pibinger
Jan 13 '06 #11
rp*****@yahoo.com (Roland Pibinger) writes:
On Thu, 12 Jan 2006 19:21:30 -0800, "Jim Langston"
<ta*******@rocketmail.com> wrote:
I thought the usual usage of the term "function overloading"
was reserved for compile-time overloading, while "polymorphism"
was used for run-time overloading. Am I mistaken?


There is run time and compile time polymorphism AFAIK.


The function call is resolved at compile time. I guess Victor Bazarov
has the right answer how to make inserter/extractor "run-time
polymorphic". Sorry for causing confusion.


Acknowledged. Thanks for the correction.
--
% Randy Yates % "So now it's getting late,
%% Fuquay-Varina, NC % and those who hesitate
%%% 919-577-9882 % got no one..."
%%%% <ya***@ieee.org> % 'Waterfall', *Face The Music*, ELO
http://home.earthlink.net/~yatescr
Jan 13 '06 #12
Victor et al.,

Why are you using the "const" qualifier in

ostream& operator << (ostream& os, Base const& b) {

? And also, is the placement of the reference operator "&"
after "const" required or can it also be after Base, e.g.,

ostream& operator << (ostream& os, Base& const b) {

Let me guess: The first way tells the object pointed to
is constant, the second tells the pointer is constant. ??

--Randy

Jan 13 '06 #13
Randy wrote:
Victor et al.,

Why are you using the "const" qualifier in

ostream& operator << (ostream& os, Base const& b) {
Same as always... b is not modified.
? And also, is the placement of the reference operator "&"
after "const" required or can it also be after Base, e.g.,

ostream& operator << (ostream& os, Base& const b) {

Let me guess: The first way tells the object pointed to
is constant, the second tells the pointer is constant. ??


Since there is no pointer, that is not possible. A reference is
initialised to refer to one object and cannot be changed.

Read the type from right to left:

int main()
{
Base base;
Base& const a = base; // a is a const reference to a Base,
'&' is // Ignored - all references are const
Base const& b = base; // b is a reference to a const Base
const Base& c = base; // c is a reference to a const Base;

Base* base_p;
Base const * const ap = base_p; // ap is a const ptr to a const Base
const Base * const bp = base_p; // bp is a const ptr to a const Base
Base * cp = base_p; // cp is a pointer to Base
Base * const dp = base_p; // dp is a const ptr to a Base
}

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 13 '06 #14
Randy wrote:
Victor et al.,

Why are you using the "const" qualifier in

ostream& operator << (ostream& os, Base const& b) {

?
The output is supposedly not changing the object. If it is so, why not
explicitly state that by declaring the object 'const'?

Also, having a 'const' here allows you to output a temporary.
And also, is the placement of the reference operator "&"
It's not really a "reference operator". It's just part of the declaration
syntax.
after "const" required or can it also be after Base, e.g.,

ostream& operator << (ostream& os, Base& const b) {
Learn the declarations. The declaration

Base & const b

(if allowed) would mean that 'b' is a _constant_ reference to non-constant
Base object. Since references are not objects, they cannot be
cv-qualified (made 'const' or 'volatile'). Hence it's not allowed.
Let me guess: The first way tells the object pointed to
is constant, the second tells the pointer is constant. ??


There are no pointers here.

V
Jan 13 '06 #15
> There are no pointers here.

Bullshit. What do you think a reference is? It is a pointer
in which the compiler automatically handles
dereferencing. From Schildt's "Teach Yourself C++"
2e, in reference to the function "void f(int &n)",

...each time n is used within f(), it is automatically
treated as a POINTER to the argument used to
call f().

I'm sure that is exactly what a implementation does
with references. Anything else (e.g., copying to a
temporary and then recopying when done) would be
insanely inefficient and pointless.

Pointer isn't a dirty word - don't be afraid to use it.

--Randy Yates

Jan 13 '06 #16
Randy wrote:
There are no pointers here.

Bullshit.


No. There were no pointers there.
What do you think a reference is? It is a pointer
No, it is a reference.
in which the compiler automatically handles
dereferencing. From Schildt's "Teach Yourself C++"
If you manage to derive any worthwhile knowledge from Schildt's books,
you're a genius. Most likely, you're just using wrong source of
information and get confused. If you want to be viewed seriously here,
do not quote from Schildt's books.
2e, in reference to the function "void f(int &n)",

...each time n is used within f(), it is automatically
treated as a POINTER to the argument used to
call f().

I'm sure that is exactly what a implementation does
I am not. Nowhere in the Standard can we read that a reference is
"a pointer that is automatically dereferenced" (or some such). And the
Standard is _infinitely_ more trusted source of information that Schildt's
books.
with references. Anything else (e.g., copying to a
temporary and then recopying when done) would be
insanely inefficient and pointless.
Who said anything about copying? Why can't a reference be implemented in
some other way, like an index or other indirection? If you can't think of
any other way of implementation than pointers, it doesn't mean there can
be none. FWIW, the function can be inlined, and then there are no real
arguments -- everything is as if the body of the function is inserted into
the calling scope and the formal arguments are replaced with the factual
ones.
Pointer isn't a dirty word - don't be afraid to use it.


I am not. No matter what you managed to pollute your brain with by
reading Schildt's books, there were no pointers there. Those were
references, references are not objects, and as such cannot be qualified
as const. Write it on the board 100 times, then report back to us.

V
Jan 13 '06 #17
Randy wrote:
There are no pointers here.
Bullshit. What do you think a reference is? It is a pointer
in which the compiler automatically handles
dereferencing.


It doesn't have to be. It could be implemented in a completely
different way. The standard mandates no such thing.
From Schildt's "Teach Yourself C++"
2e, in reference to the function "void f(int &n)",

...each time n is used within f(), it is automatically
treated as a POINTER to the argument used to
call f().
Nope. There is no requirement to implement a reference as a pointer
with syntactic sugar.
I'm sure that is exactly what a implementation does
with references.
Why are you so sure?
Anything else (e.g., copying to a
temporary and then recopying when done) would be
insanely inefficient and pointless.
Perhaps it done with magic? Why should you care?
Pointer isn't a dirty word - don't be afraid to use it.


Neither is "reference".

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 13 '06 #18
TB
Randy sade:
There are no pointers here.


Bullshit. What do you think a reference is? It is a pointer
in which the compiler automatically handles
dereferencing. From Schildt's "Teach Yourself C++"
2e, in reference to the function "void f(int &n)",

...each time n is used within f(), it is automatically
treated as a POINTER to the argument used to
call f().

I'm sure that is exactly what a implementation does
with references. Anything else (e.g., copying to a
temporary and then recopying when done) would be
insanely inefficient and pointless.

Pointer isn't a dirty word - don't be afraid to use it.

--Randy Yates


You might be confused. References might be handled with similarities
to pointers by the compiler and hardware, but the C++ Standard
provides references as a concept on its own, and should be
acknowledged as such by programmers.

Snipped from The C++ Standard 8.3.2/1
"a reference can be thought of as a name of an object."

How references are treated during execution of compiled code
is of no concern. A machine could just as well contain a name cache
holding aliases for objects in memory without dealing with address pointers.

TB
Jan 13 '06 #19
I reserve the right to investigate and understand "what's under the
hood" of any code-generating tool I use. If that is a problem for
the tool maker, I'll start looking for another tool, because
the day this becomes unknowable is the day we start
losing touch with reality. The implementation matters,
the hardware matters, the assembly code generated
matters.

It sounds to me like you all know damn well that a reference
is a pointer with some help from the compiler, but would
prefer to dance around the fact. If you really think it's
not, then find a compiler writer around here who can
explain how they actually implemented it.

--RY

Jan 13 '06 #20
On 2006-01-13, Randy <ya***@ieee.org> wrote:
There are no pointers here.


Bullshit. What do you think a reference is?


If you were implementing a reference, would it inherit from
pointer or contain an instance of pointer? Which relationship
implies is-a?

--
Neil Cerutti
Jan 13 '06 #21
Randy wrote:
I reserve the right to investigate and understand "what's under the
hood" of any code-generating tool I use. If that is a problem for
the tool maker, I'll start looking for another tool, because
the day this becomes unknowable is the day we start
losing touch with reality. The implementation matters,
the hardware matters, the assembly code generated
matters.

It sounds to me like you all know damn well that a reference
is a pointer with some help from the compiler, but would
prefer to dance around the fact. If you really think it's
not, then find a compiler writer around here who can
explain how they actually implemented it.


Dear Randy,

Don't crawl into a bottle here. Whatever the implementation of references
in any particular compiler is, does NOT matter. The syntax that would
declare a "constant reference" is _not_allowed_, your attempt to treat is
as if it were legal notwithstanding.

int * const p;

and

int const * p;

are different, yes, and both are legal declarations. However,

int const & r;

is legal (and declares a reference to a constant integer) and

int & const r;

is NOT legal. Conjuring some implementation detail to try to make this
legal is pointless. The Standard explicitly prohibits 'const' and
'volatile' to be applied to _a_reference_.

We're not "dancing around" anything. If you want to achieve a higher
level of understanding what we're talking about, chuck the Schildt's books
you got and get yourself a copy of the Standard and a couple of _decent_
editions, like Stroustrup's "TC++PL", and Josuttis' "TC++SL:T&R", and
Alexandrescu's "MC++D", etc.

V
Jan 13 '06 #22
On Fri, 13 Jan 2006 18:30:02 +0100, TB <TB@void.com> wrote:

See some of my other posts for what this attitude - ignorance actually
- is. It's baahahahahahaad. Void.com is certainly correct.
How references are treated during execution of compiled code
is of no concern.
(8-O) You've never worked on anything "important" have you. And if you
have, I hope never to work with you. In my world, people like you get
sane people hurt or worse.
TB


Totally Bogus?

Keep on raging - to stop the aging.
-Dale Carnegie. Of the brain, I append.
Jan 13 '06 #23
TB
JustBoo sade:
On Fri, 13 Jan 2006 18:30:02 +0100, TB <TB@void.com> wrote:

See some of my other posts for what this attitude - ignorance actually
- is. It's baahahahahahaad. Void.com is certainly correct.
How references are treated during execution of compiled code
is of no concern.


(8-O) You've never worked on anything "important" have you. And if you
have, I hope never to work with you. In my world, people like you get
sane people hurt or worse.


If you can't separate language from implementation, then
right back at you. Take it to alt.flame or somewhere
else if you wish to continue down this path.

TB
Jan 13 '06 #24

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

Similar topics

1
by: nimrod | last post by:
When running inserter and selector on the same connection (using binding expressions) on the second iteration the insertion/selection fails, receiving failure message: CT-Lib problem: sever=1...
20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
2
by: Chris | last post by:
Hi, I try Public Shared Operator +(ByVal Left As Point, ByVal Right As Size) As Point Return New Point(Left.X + Right.Width, Left.Y + Right.Height) End Operator but I get compiler errors....
2
by: Al-Burak | last post by:
somewhere in MyClass .... std::streampos pos; long long Id; char* Key; ...... std::istream& jme::operator>>( std::istream& is, jme::MyClass& obj ) { is >> obj.Id; is.ignore(1); is >> obj.Key;
3
by: mensanator | last post by:
## Holy Mother of Pearl! ## ## >>> for i in range(10): ## for j in range(10): ## print '%4d' % (gmpy.mpz(i)*gmpy.mpz(j)), ## print ## ## ## ...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
1
by: =?Utf-8?B?QnJldHRWUA==?= | last post by:
I have a child-class that inherits from a base class that implements an Interface. The child class overrides a SUB from the base class that implements a sub from the interface. (The...
5
by: ggloadi | last post by:
#include<iostream> #include<iomanip> using namespace std; class Currency { public: Currency(double v = 0.0) { unit = v;
7
by: MWimmer | last post by:
Dear members of this group, recently I came across a problem with repsect to operator=() and inheritance. Consider the following code snippet: ...
3
by: laikon | last post by:
this question is about how to define friend functions for a class template. the following is an example. template <typename T> class Array { private: T* parr; int sz;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.