Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Changing access specifier for virtual function

Reply
Thread Tools

Changing access specifier for virtual function

 
 
dragoncoder
Guest
Posts: n/a
 
      04-07-2006
Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.

$ ./a.out
Derived
$

I have 2 questions:
1. Is the above code legal ?
2. If the virtual function is private in Base, can I make it public in
Derived ?

Thanks

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      04-07-2006
dragoncoder wrote:
> Consider the following code
>
> #include <iostream>
>
> class Base {
> public:
> virtual void say() { std::cout << "Base" << std::endl; }
> };
>
> class Derived: public base {
> private:
> void say() { std::cout << "Derived" << std::endl; }
> };
>
> int main() {
> Derived d;
> Base* b = &d;
> b->say();
> return 0;
> }
>
> Running this program produces following output.
>
> $ ./a.out
> Derived
> $
>
> I have 2 questions:
> 1. Is the above code legal ?


Yes. And an excellent example of poor coding practice.

> 2. If the virtual function is private in Base, can I make it public in
> Derived ?


No. If it were private in Base, then Derived wouldn't inherit it.

Best regards,

Tom

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-07-2006
dragoncoder wrote:
> Consider the following code
>
> #include <iostream>
>
> class Base {
> public:
> virtual void say() { std::cout << "Base" << std::endl; }
> };
>
> class Derived: public base {


You mean

class Derived: public Base {

> private:
> void say() { std::cout << "Derived" << std::endl; }
> };
>
> int main() {
> Derived d;
> Base* b = &d;
> b->say();
> return 0;
> }
>
> Running this program produces following output.
>
> $ ./a.out
> Derived
> $
>
> I have 2 questions:
> 1. Is the above code legal ?


Yes, absolutely, considering the correction I made. Try not to type your
code directly into the message next time. Use "copy-and-paste" mechanism
undoubtedly available to you.

> 2. If the virtual function is private in Base, can I make it public in
> Derived ?


The access specifier only has effect on the ability to call the function
as if it weren't virtual. For example, if you make 'say' private in 'Base',
your call to it in 'main' (b->say()) will be ill-formed, and won't compile.

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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-07-2006
Thomas Tutone wrote:
> dragoncoder wrote:
>> Consider the following code
>>
>> #include <iostream>
>>
>> class Base {
>> public:
>> virtual void say() { std::cout << "Base" << std::endl; }
>> };
>>
>> class Derived: public base {
>> private:
>> void say() { std::cout << "Derived" << std::endl; }
>> };
>>
>> int main() {
>> Derived d;
>> Base* b = &d;
>> b->say();
>> return 0;
>> }
>>
>> Running this program produces following output.
>>
>> $ ./a.out
>> Derived
>> $
>>
>> I have 2 questions:
>> 1. Is the above code legal ?

>
> Yes. And an excellent example of poor coding practice.


Why do you call this practice "poor"?

>> 2. If the virtual function is private in Base, can I make it public
>> in Derived ?

>
> No. If it were private in Base, then Derived wouldn't inherit it.


Nothing is inherited here. It's _overridden_. Try it:

#include <iostream>

class Base {
virtual int foo() { return 42; } // private
public:
int bar() { return foo(); }
};

class Derived : public Base {
virtual int foo() { return 73; } // also private
};

int main()
{
Derived d;
Base *b = &d;
std::cout << b->bar() << std::endl;
return 0;
}

What happens here?

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


 
Reply With Quote
 
Tejas Kokje
Guest
Posts: n/a
 
      04-07-2006
dragoncoder wrote:
> Consider the following code
>
> #include <iostream>
>
> class Base {
> public:
> virtual void say() { std::cout << "Base" << std::endl; }
> };
>
> class Derived: public base {
> private:
> void say() { std::cout << "Derived" << std::endl; }
> };
>
> int main() {
> Derived d;
> Base* b = &d;
> b->say();
> return 0;
> }
>
> Running this program produces following output.
>
> $ ./a.out
> Derived
> $
>
> I have 2 questions:
> 1. Is the above code legal ?


Since the above code compiled, it is syntactically legal.

> 2. If the virtual function is private in Base, can I make it public in
> Derived ?


The call to b->say() is calling Derived class say() function. Here
Derived class does not know anything about say() function of Base class
since say() is private in Base(). Hence say() functions in Derived and
Base class are not related by virtual function paradigm.

Even if you remove "virtual" specifier from Base class say() function,
you will still run Derived class say() function since say() is public
in Derived and there is no public say() in Base class.

Tejas Kokje

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      04-07-2006
Tejas Kokje wrote:

>> 1. Is the above code legal ?

>
> Since the above code compiled, it is syntactically legal.


Famous last words.

Try: "The code is well-formed, so it should compile."

Plenty of compilers reject plenty of well-formed constructions.

Yes, the OP is correct to rely on their compiler to judge how well such a
simple bit of code is formed!

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
 
Reply With Quote
 
Tejas Kokje
Guest
Posts: n/a
 
      04-07-2006
Phlip wrote:
> Tejas Kokje wrote:
>
> >> 1. Is the above code legal ?

> >
> > Since the above code compiled, it is syntactically legal.

>
> Famous last words.
>
> Try: "The code is well-formed, so it should compile."


> Plenty of compilers reject plenty of well-formed constructions.
> Yes, the OP is correct to rely on their compiler to judge how well such a
> simple bit of code is formed!


I think you misunderstood my comments. I said that the code is
*syntactically* legal. This does not mean that code semantics make
sense. If compilers could detect semantic errors in code, most of the
softwares would be bug free.

Anyways, in OP's case, compiler did compile the code. So there is no
reason for him/her to see the code as illegal. However, OP is not
relying on compiler to tell him that code is well formed. Hence he/she
posted a question here.

Tejas Kokje

 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      04-07-2006
Tejas Kokje <(E-Mail Removed)> wrote:

> dragoncoder wrote:


>> Running this program produces following output.
>>
>> $ ./a.out
>> Derived
>> $
>>
>> I have 2 questions:
>> 1. Is the above code legal ?


> Since the above code compiled, it is syntactically legal.


No. Most compilers are broken in one respect or the other. I do not
know if Comeau is 100% compliant even. The code below will compile with
some compilers, but it is *not* syntactically legal:

class foo
{
void bar () {}
};

int main ()
{
void (foo::*pbar) () = foo::bar;
}
regards
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
Tejas Kokje
Guest
Posts: n/a
 
      04-07-2006
Can you tell me which compiler compiles above code ?

since bar() is private and non-static to class foo, how can you access
it outside the class using class scope ?

Tejas Kokje

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      04-07-2006
In article <(E-Mail Removed) .com>,
"dragoncoder" <(E-Mail Removed)> wrote:

> Consider the following code
>
> #include <iostream>
>
> class Base {
> public:
> virtual void say() { std::cout << "Base" << std::endl; }
> };
>
> class Derived: public base {
> private:
> void say() { std::cout << "Derived" << std::endl; }
> };
>
> int main() {
> Derived d;
> Base* b = &d;
> b->say();
> return 0;
> }
>
> Running this program produces following output.
>
> $ ./a.out
> Derived
> $
>
> I have 2 questions:
> 1. Is the above code legal ?


Absolutely. It's a great way to make sure the derived object is used
through a base pointer.


> 2. If the virtual function is private in Base, can I make it public in
> Derived ?


Yes.


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubt regarding "Protected" access specifier across different packages sayantan.chowdhury@gmail.com Java 9 09-25-2007 02:58 AM
diffrence between access modifier ....and access specifier...in c++ dost C++ 5 04-28-2006 01:40 AM
Changing access list on virtual-access interface? Yehavi Bourvine Cisco 1 08-26-2004 04:22 AM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM
Access specifier and inheritance mshetty C++ 3 02-17-2004 03:08 PM



Advertisments