473,831 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual function behaviour

Hello experts,

I was just playing around wrote this code.

sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>

using namespace std;

class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}
private:
void bar() { cout << "In Base::bar()" << endl; }
};

class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }
};

class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }
};

int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();

b1->foo();
b2->foo();
b3->foo();

return 0;
}

sundev1:/home/ptiwary/rnd $ g++ a1.cpp
sundev1:/home/ptiwary/rnd $ ./a.out
In Base::foo()
In Base::bar()
In Der1::foo()
In Der1::bar()
In Der2::foo()
In Der2::bar()

I have 2 questions regarding this.

1. Is the behaviour correct? Because someone told me I need to make
bar() also virtual to get the effect.
2. What is the deal with private virtual functions? Even if I make
bar() virtual, as its private, it won't be accessible from the derived
classes Der1 and Der2 so it does not make any sense having private
virtual functions. Am I right?

Thanks in advance.

Apr 3 '07 #1
7 2085
dragoncoder wrote:
I was just playing around wrote this code.

sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>

using namespace std;

class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}
private:
void bar() { cout << "In Base::bar()" << endl; }
};

class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }
};

class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }
};

int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();

b1->foo();
b2->foo();
b3->foo();

return 0;
}

sundev1:/home/ptiwary/rnd $ g++ a1.cpp
sundev1:/home/ptiwary/rnd $ ./a.out
In Base::foo()
In Base::bar()
In Der1::foo()
In Der1::bar()
In Der2::foo()
In Der2::bar()

I have 2 questions regarding this.

1. Is the behaviour correct? Because someone told me I need to make
bar() also virtual to get the effect.
No, the 'bar' resolved "statically " (as 'this->bar') in each of the
'foo' functions.
2. What is the deal with private virtual functions? Even if I make
bar() virtual, as its private, it won't be accessible from the derived
classes Der1 and Der2 so it does not make any sense having private
virtual functions. Am I right?
Access specifiers and virtuality are orthogonal. IOW, it does not
matter what access specifiers virtual functions have to establish
which of them overrides which.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 3 '07 #2
dragoncoder <pk******@gmail .comwrote:
I was just playing around wrote this code.

sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>

using namespace std;

class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}
private:
void bar() { cout << "In Base::bar()" << endl; }
};

class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }
};

class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }
};

int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();

b1->foo();
b2->foo();
b3->foo();

return 0;
}

sundev1:/home/ptiwary/rnd $ g++ a1.cpp
sundev1:/home/ptiwary/rnd $ ./a.out
In Base::foo()
In Base::bar()
In Der1::foo()
In Der1::bar()
In Der2::foo()
In Der2::bar()

I have 2 questions regarding this.

1. Is the behaviour correct? Because someone told me I need to make
bar() also virtual to get the effect.
It looks correct to me. For example, when you call b2->foo(), since
foo() is virtual, it looks to see what type *b2 is to call the correct
version of the function. When it discovers that it is a Der1, it calls
Der1::foo(). Inside Der1, you call Der1::bar() (since it already knows
it is a Der1.
2. What is the deal with private virtual functions? Even if I make
bar() virtual, as its private, it won't be accessible from the derived
classes Der1 and Der2 so it does not make any sense having private
virtual functions. Am I right?
No, there are some design patterns that use private virtual functions.
This GotW article explains one usage:

http://www.gotw.ca/publications/mill18.htm

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 3 '07 #3
dragoncoder wrote:
Hello experts,

I was just playing around wrote this code.

sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>

using namespace std;

class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}
private:
void bar() { cout << "In Base::bar()" << endl; }
};

class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }
};

class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }
};

int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();

b1->foo();
b2->foo();
b3->foo();

return 0;
}
Victor answered your questions, but I have another comment, relevant
because you used dynamic allocation.

You really need a virtual destructor in Base, even if it's empty, since
if you delete b1, b2, or b3, you will be doing so through a base class
pointer.
Apr 3 '07 #4
On Apr 3, 5:42 pm, "dragoncode r" <pktiw...@gmail .comwrote:
Hello experts,

I was just playing around wrote this code.

sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>

using namespace std;

class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}
// Since you are using Base pointers and allocating Derived objects
with new,
// the destructor must be virtual or you'll get memory leaks

virtual ~Base() { std::cout << "virtual ~Base\n"; }
private:
void bar() { cout << "In Base::bar()" << endl; }

};

class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }
// to test the theory above:
~Der1() { std::cout << "virtual ~Der1()\n"; }

>
};

class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }

};

int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();

b1->foo();
b2->foo();
b3->foo();
delete b1;
delete b2;
delete b3;
>
return 0;

}

sundev1:/home/ptiwary/rnd $ g++ a1.cpp
sundev1:/home/ptiwary/rnd $ ./a.out
In Base::foo()
In Base::bar()
In Der1::foo()
In Der1::bar()
In Der2::foo()
In Der2::bar()

I have 2 questions regarding this.

1. Is the behaviour correct? Because someone told me I need to make
bar() also virtual to get the effect.
Yes its correct. bar() needs not be virtual.
2. What is the deal with private virtual functions? Even if I make
bar() virtual, as its private, it won't be accessible from the derived
classes Der1 and Der2 so it does not make any sense having private
virtual functions. Am I right?
Not neccesarily, make the bar() functions public, the virtual foo()
functions private, invoke the private virtual foo() in bar(). You can
still have the derived object call the base's foo() by calling its
public interface ( Base::bar() ).

Apr 3 '07 #5
On Apr 3, 6:18 pm, "Salt_Peter " <pj_h...@yahoo. comwrote:
On Apr 3, 5:42 pm, "dragoncode r" <pktiw...@gmail .comwrote:
Hello experts,
I was just playing around wrote this code.
sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>
using namespace std;
class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}

// Since you are using Base pointers and allocating Derived objects
with new,
// the destructor must be virtual or you'll get memory leaks

virtual ~Base() { std::cout << "virtual ~Base\n"; }
private:
void bar() { cout << "In Base::bar()" << endl; }
};
class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }

// to test the theory above:
~Der1() { std::cout << "virtual ~Der1()\n"; }


};
class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }
};
int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();
b1->foo();
b2->foo();
b3->foo();

delete b1;
delete b2;
delete b3;


return 0;
}
sundev1:/home/ptiwary/rnd $ g++ a1.cpp
sundev1:/home/ptiwary/rnd $ ./a.out
In Base::foo()
In Base::bar()
In Der1::foo()
In Der1::bar()
In Der2::foo()
In Der2::bar()
I have 2 questions regarding this.
1. Is the behaviour correct? Because someone told me I need to make
bar() also virtual to get the effect.

Yes its correct. bar() needs not be virtual.
2. What is the deal with private virtual functions? Even if I make
bar() virtual, as its private, it won't be accessible from the derived
classes Der1 and Der2 so it does not make any sense having private
virtual functions. Am I right?

Not neccesarily, make the bar() functions public, the virtual foo()
functions private, invoke the private virtual foo() in bar(). You can
still have the derived object call the base's foo() by calling its
public interface ( Base::bar() ).- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Hello experts,

#include <iostream>
using namespace std;
class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl;
bar();}
private:
void bar() { cout << "In Base::bar()" << endl; }

};
class Der1: public Base
{

private:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
void bar() { cout << "In Der1::bar()" << endl; }
};

int main()
{
Base* b2 = new Der1();

b2->foo();
return 0;

}

One quick question:
In the above code, compiler does not generate error because virtual
foo() is public in base. But ultimately it calls Derived version of
this function which is private in derived class. Isn't it access
violation?

Apr 3 '07 #6
On Apr 3, 7:19 pm, "siddhu" <siddharth....@ gmail.comwrote:
On Apr 3, 6:18 pm, "Salt_Peter " <pj_h...@yahoo. comwrote:
On Apr 3, 5:42 pm, "dragoncode r" <pktiw...@gmail .comwrote:
Hello experts,
I was just playing around wrote this code.
sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>
using namespace std;
class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}
// Since you are using Base pointers and allocating Derived objects
with new,
// the destructor must be virtual or you'll get memory leaks
virtual ~Base() { std::cout << "virtual ~Base\n"; }
private:
void bar() { cout << "In Base::bar()" << endl; }
};
class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }
// to test the theory above:
~Der1() { std::cout << "virtual ~Der1()\n"; }
};
class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }
};
int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();
b1->foo();
b2->foo();
b3->foo();
delete b1;
delete b2;
delete b3;
return 0;
}
sundev1:/home/ptiwary/rnd $ g++ a1.cpp
sundev1:/home/ptiwary/rnd $ ./a.out
In Base::foo()
In Base::bar()
In Der1::foo()
In Der1::bar()
In Der2::foo()
In Der2::bar()
I have 2 questions regarding this.
1. Is the behaviour correct? Because someone told me I need to make
bar() also virtual to get the effect.
Yes its correct. bar() needs not be virtual.
2. What is the deal with private virtual functions? Even if I make
bar() virtual, as its private, it won't be accessible from the derived
classes Der1 and Der2 so it does not make any sense having private
virtual functions. Am I right?
Not neccesarily, make the bar() functions public, the virtual foo()
functions private, invoke the private virtual foo() in bar(). You can
still have the derived object call the base's foo() by calling its
public interface ( Base::bar() ).- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -

Hello experts,

#include <iostream>

using namespace std;

class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl;
bar();}
private:
void bar() { cout << "In Base::bar()" << endl; }

};

class Der1: public Base
{

private:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
void bar() { cout << "In Der1::bar()" << endl; }

};

int main()
{
Base* b2 = new Der1();

b2->foo();

return 0;

}

One quick question:
In the above code, compiler does not generate error because virtual
foo() is public in base. But ultimately it calls Derived version of
this function which is private in derived class. Isn't it access
violation?
no its not.
You are asking a pointer to base to polymorphicly call a virtual
function.
If you try to access that virtual function directly - it would fail.

Der1 instance;
instance.foo(); <- error

Did you not read the above posts?
You have a serious condition here - its called a memory leak.
Even if you invoke the destructor with:

delete b2;

you still have a memory leak.
Do you not read the warnings your compiler is giving you?

Apr 4 '07 #7
dragoncoder wrote:
Hello experts,

I was just playing around wrote this code.

sundev1:/home/ptiwary/rnd $ cat a1.cpp
#include <iostream>

using namespace std;

class Base
{
public:
virtual void foo() { cout << "In Base::foo()" << endl; bar();}
private:
void bar() { cout << "In Base::bar()" << endl; }
};

class Der1: public Base
{
public:
virtual void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
void bar() { cout << "In Der1::bar()" << endl; }
};

class Der2: public Base
{
public:
virtual void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
void bar() { cout << "In Der2::bar()" << endl; }
};

int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();

b1->foo();
b2->foo();
b3->foo();

return 0;
}

sundev1:/home/ptiwary/rnd $ g++ a1.cpp
sundev1:/home/ptiwary/rnd $ ./a.out
In Base::foo()
In Base::bar()
In Der1::foo()
In Der1::bar()
In Der2::foo()
In Der2::bar()

I have 2 questions regarding this.

1. Is the behaviour correct? Because someone told me I need to make
bar() also virtual to get the effect.
The behavior is correct. This is the idea of polymorphism, the run time
automatically picks up the correct function to use.
2. What is the deal with private virtual functions? Even if I make
bar() virtual, as its private, it won't be accessible from the derived
classes Der1 and Der2 so it does not make any sense having private
virtual functions. Am I right?
No, virtual tells the compiler/linker that instead of static linking,
let the run time decide which function to use. Check the following example:

#include <iostream>

using namespace std;

class Base
{
public:
void foo() { cout << "In Base::foo()" << endl; bar();}
private:
virtual void bar() { cout << "In Base::bar()" << endl; }
};

class Der1: public Base
{
public:
void foo() { cout << "In Der1::foo()" << endl; bar();}
private:
virtual void bar() { cout << "In Der1::bar()" << endl; }
};

class Der2: public Base
{
public:
void foo() { cout << "In Der2::foo()" << endl; bar();}
private:
virtual void bar() { cout << "In Der2::bar()" << endl; }
};

int main()
{
Base* b1 = new Base();
Base* b2 = new Der1();
Base* b3 = new Der2();

b1->foo();
b2->foo();
b3->foo();

return 0;
}
Thanks in advance.
Apr 4 '07 #8

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

Similar topics

7
1625
by: Calvin Lai | last post by:
Hi all, I have a simple question. If I have a ClassA as base class, and ClassB derive from it. There is a virtual function foo() in ClassA, and in Class B, I defined a function called foo() as well (w/ or w/o declaring it as virtual doesn't matter since the virtual is inhered, right?). Both of them have a virtual destructor. Now, here is the part of the code:
4
1509
by: Eric | last post by:
I found the following phenomenon in VC++ 2005: struct A {}; struct B : public A { virtual ~B() {} }; A* p = new B;
11
4375
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
10
2988
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that class A have some derived classes. I want A's constructor change its behaviour accounting to the derived class. I tried to make A::fun() not pure virtual but virtual. It doesn't generate any error. But A::fun() is called in A's construction, while I
175
8925
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
20
2024
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
1
286
by: Rahul K | last post by:
Hi all I tried running the following code: #include<iostream.h> class Base { public: virtual void func()
5
1286
by: V Patel | last post by:
I am trying to understand the behaviour for the following two cases: class A { virtual funcA() {i++;}; // case 1 funct A() {i++;}; // case 2 private: int i; }; main ()
17
3557
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
9794
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
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
10778
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
10210
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
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
7750
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
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4419
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
3967
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.