473,659 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which print function going to be called. scope question.

class Base{

public:
std::ostream& operator<<( std::ostream& os, const string &str )
{ return print(os); }

void print(std:ostre am& os){ os<<"Base\n";}
};
class Derived{
public:
using Base::operator< <;
void print(std:ostre am& os){ os<<"Derived\n" ;}
};

D d=Derived;
D<<"str";

B & pd=new Derived;
pd<<"str";
Notice that that print is not a virtual function.

I wonder if you bring base class member in the Derive class scope
and invoke it via Derived object, which print is it going to call: one
from derived or one from base.
Jun 27 '08 #1
1 1115
sasha wrote:
class Base{

public:
std::ostream& operator<<( std::ostream& os, const string &str )
{ return print(os); }

void print(std:ostre am& os){ os<<"Base\n";}
};
class Derived{
public:
using Base::operator< <;
void print(std:ostre am& os){ os<<"Derived\n" ;}
};

D d=Derived;
D<<"str";

B & pd=new Derived;
pd<<"str";
Notice that that print is not a virtual function.

I wonder if you bring base class member in the Derive class scope
and invoke it via Derived object, which print is it going to call: one
from derived or one from base.
Sasha, please post real code. Your 'print' functions are 'void', so
you cannot 'return print(os)' from the operator <<.

Generally speaking, making Base::operator< < "visible" in Derived is
unnecessary, it's visible as it is. And, of course, such visibility
change does NOT affect the behaviour of the 'operator<<' function.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2

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

Similar topics

21
3837
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
28
15355
by: Michael B. | last post by:
I tend to use rather descriptive names for parameters, so the old style of declaration appeals to me, as I can keep a declaration within 80 chars: void * newKlElem (frame_size,num_blocks,num_frames,frame_locator) size_t frame_size; unsigned short num_blocks; unsigned short num_frames; Kl_frame_locator *locator; { /* code goes here */
26
3117
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The code will not be idiomatic C. GCC has an extension to ISO C that permits nested functions: <http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html> For implementing closures they have a serious limitation:
65
12568
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
4
9680
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person writing the JavaScript doesn't have to pass the index in the "onChange" event name. I thought that one might be able to use "this.value" or compare this as
26
2134
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
3
1734
by: AWasilenko | last post by:
I'm still in the process of learning python via a handful of books I bought. One book I am reading just introduced Base Class Methods. I found that I needed more understanding on this concept and wrote a short test program using the Author's code as a vague reference. My question now really isn't Base Class related, but my question stems from my test code so I will just post it as is. ##Test of Super() stuff class First(object):
16
1804
by: Wade Ward | last post by:
/* C version */ static unsigned long t,x=123456789,y=362436069,z=21288629,w=14921776,c=0; unsigned long KISS(){ x+=545925293; y^=(y<<13); y^=(y>>17); y^=(y<<5); t=z+w+c; z=w; c=(t>>31); w=t&2147483647; return(x+y+w); } What would be an appropriate caller for this function. Am I correct that the static specifier has the function remembering its older
25
595
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.