Virtual function behaviour

D

dragoncoder

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.
 
V

Victor Bazarov

dragoncoder said:
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
 
M

Marcus Kwok

dragoncoder said:
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
 
R

red floyd

dragoncoder said:
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.
 
S

Salt_Peter

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() ).
 
S

siddhu

// 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"; }


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










delete b1;
delete b2;
delete b3;










Yes its correct. bar() needs not be virtual.


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?
 
S

Salt_Peter

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?
 
F

Fei Liu

dragoncoder said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top