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

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 2061
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, "dragoncoder" <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, "dragoncoder" <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, "dragoncoder" <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
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...
4
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
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...
10
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...
175
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...
20
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
1
by: Rahul K | last post by:
Hi all I tried running the following code: #include<iostream.h> class Base { public: virtual void func()
5
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
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;"...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
1
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...
0
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...
0
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...
0
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...

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.