473,320 Members | 1,810 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,320 software developers and data experts.

is this behaviour acceptible?

hey, i found something confusing. Look at the following code:

#include <iostream>

class mybase{
protected:
virtual void printX(){
std::cout << "i am from the base X" << std :: endl;
}
};

class firstsub : public mybase{
public:
void printX(){
std::cout << "i am from first sub class X " << std :: endl;
}
};

class secondsub : public firstsub{
private:
void printX(){
std::cout << "i am from second sub class X " << std ::
endl;
}
};

int main(){
firstsub ofs;
firstsub* pfs;
secondsub oss;

pfs = &ofs;

pfs -> printX();

pfs = &oss;

pfs -> printX();
}
And the output is:

[bhaskar@dallas basics]$ g++ inherit4.cxx

[bhaskar@dallas basics]$ ./a.out

i am from first sub class X
i am from second sub class X

I am bit confused to see the second message in the output...

Any clarifications?
--Bala

Jul 23 '05 #1
7 1975
bala wrote:


i am from first sub class X
i am from second sub class X

I am bit confused to see the second message in the output...

Any clarifications?


Thats the normal behaviour of virtual functions.
At runtime, the actual type of the object the pointer points
to is looked up and the function for that object is selected.

Why are you confused, what did you expect?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
sorry for not mentioning this before.

the confusion is not about late binding, but about access.

you can see that the method is private in the second sub class.

Jul 23 '05 #3


bala wrote:
sorry for not mentioning this before.

the confusion is not about late binding, but about access.

you can see that the method is private in the second sub class.


So? It is public in interface of the object that you call it from.

Jul 23 '05 #4
yes,

what i want to know is :

Is everything OK here?

Jul 23 '05 #5


bala wrote:
yes,

what i want to know is :

Is everything OK here?


Whilst the language allows you to do it, you probably shouldn't do it.
http://www.parashift.com/c++-faq-lit....html#faq-21.1

Jul 23 '05 #6
Got it.

Thanks. :)

Jul 23 '05 #7
bala <bh***********@persistent.co.in> wrote:
| yes,
|
| what i want to know is :
|
| Is everything OK here?

Yes. A quote from the standard:

11.6 Access to virtual functions [class.access.virt]

1 The access rules (clause 11) for a virtual function are determined by
its declaration and are not affected by the rules for a function that
later overrides it.

[Example:
class B {
public:
virtual int f();
};
class D : public B {
private:
int f();
};
void f()
{
D d;
B* pb = &d;
D* pd = &d;
pb->f(); //OK: B::f() is public,
// D::f() is invoked
pd->f(); //error: D::f() is private
}
end example]

Access is checked at the call point using the type of the expression
used to denote the object for which the member function is called
(B* in the example above). The access of the member function in the
class in which it was defined (D in the example above) is in general
not known.

--
Robert Bauck Hamar
Jul 23 '05 #8

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

Similar topics

2
by: Joona I Palaste | last post by:
AFAIK the C standard divides behaviour of different things into four classes. 1) Defined behaviour. The implementation must do exactly what the standard says. 2) Implementation-defined behaviour....
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
12
by: RoSsIaCrIiLoIA | last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org> wrote: >"Romeo Colacitti" <wwromeo@gmail.com> writes: >> Chris Torek wrote: >>> In article <4205BD5C.6DC8@mindspring.com> >>>...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
4
by: | last post by:
I have earlier used an HttpModule that did URL rewrites on the BeginRequest event. Now I am trying to use the same module in a different application on a new and upgraded machine (winxp sp2). ...
26
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
12
by: Franz Hose | last post by:
the following program, when compiled with gcc and '-std=c99', gcc says test.c:6: error: jump into scope of identifier with variably modified type that is, it does not even compile. ...
173
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the...
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.