473,507 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with inheritance and virtual function

The following is a piece of code and the output of the code is pasted after
it . I dont understand how it works (while giving the output) .Can someone
enlighten me on this ?

thanking you .
maadhuu .
the following is the code

class base
{
int data;
public :
base() { }
virtual void virt()
{
}
};
class derived:public base
{
int data;
public :
derived(int a):data(a) { }
virtual void virt()
{
std::cout << data ;
}
};
int main()
{
int a = 1;
base *b[3] ;
for(int i = 0; i<3;++i)
{ b[i] = &derived(a++);b[i]->virt();
}
for(int i = 0; i<3;++i)
b[i]->virt();
return 0 ;
}

the output is 123333 (1 2 3 is for what is printed in the loop soon after
initializing it with an object ) .My question is why is the output 3 in
the other case ?(i.e.for b[i]->virt() for i = 0 to 2)

Oct 17 '05 #1
6 1154
maadhuu wrote:
The following is a piece of code and the output of the code is pasted after
it . I dont understand how it works (while giving the output) .Can someone
enlighten me on this ?

thanking you .
maadhuu .
the following is the code

class base
{
int data;
public :
base() { }
Don't forget the virtual destructor:

virtual ~base() {}

See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-20.7
virtual void virt()
{
}
};
class derived:public base
{
int data;
public :
derived(int a):data(a) { }
virtual void virt()
{
std::cout << data ;
}
};
int main()
{
int a = 1;
base *b[3] ;
for(int i = 0; i<3;++i)
{ b[i] = &derived(a++);
Here you have bound a pointer to a temporary object, and consequently,
all bets are off. To fix it, you could do something like:

b[i] = new derived(a++);

(You'd also need to delete them somewhere.)
b[i]->virt();
}
for(int i = 0; i<3;++i)
b[i]->virt();
return 0 ;
}

the output is 123333 (1 2 3 is for what is printed in the loop soon after
initializing it with an object ) .My question is why is the output 3 in
the other case ?(i.e.for b[i]->virt() for i = 0 to 2)


Cheers! --M

Oct 17 '05 #2
maadhuu wrote:

int main()
{
int a = 1;
base *b[3] ;
for(int i = 0; i<3;++i)
{ b[i] = &derived(a++);b[i]->virt();
}
for(int i = 0; i<3;++i)
b[i]->virt();
return 0 ;
}

the output is 123333 (1 2 3 is for what is printed in the loop soon after
initializing it with an object ) .My question is why is the output 3 in
the other case ?(i.e.for b[i]->virt() for i = 0 to 2)

Before you read on, try the following experiment.
Modify one loop in your main() to read as

for(i = 0; i<3;++i) {
std::cout << (void*)b[i] << " " << std::endl;
b[i]->virt();
}

and watch which addresses are stored in the array.

.....

Have you tried it?
What did you notice?

Funny, all the addresses stored in the array are euqal?
So all those pointers 'point to the same object'.
How come?

The problem is here:
{ b[i] = &derived(a++);b[i]->virt();


Here you create a temporary object, store its address in b[i].
Right when the ';' is reached, something happens.
The temporary gets destroyed!!!
So when the code executes b[i]->virt() it is calling a virtual
function of an object no longer in existence. You were unlucky
that the code didn't crash. But more important: when the creation
loop executes one more time, another temporary is created and as
it happens it is created in the exact same memory location as the
previous one.

That is why all b[i] contain the same address.
--
Karl Heinz Buchegger
kb******@gascad.at
Oct 17 '05 #3
maadhuu wrote:
The following is a piece of code and the output of the code is pasted
after it . I dont understand how it works (while giving the output) .Can
someone enlighten me on this ?

thanking you .
maadhuu .
the following is the code

class base
{
int data;
public :
base() { }
virtual void virt()
{
}
};
class derived:public base
{
int data;
public :
derived(int a):data(a) { }
virtual void virt()
{
std::cout << data ;
}
};
int main()
{
int a = 1;
base *b[3] ;
for(int i = 0; i<3;++i)
{ b[i] = &derived(a++);
The above statement creates a derived, assigns its address to b[i], then
destroys the derived.
b[i]->virt();
Here, you get undefined behavior because you acess a non-existent object.
}
for(int i = 0; i<3;++i)
b[i]->virt();
And here again.
return 0 ;
}


Oct 17 '05 #4
* maadhuu:
The following is a piece of code and the output of the code is pasted after
it . I dont understand how it works (while giving the output) .Can someone
enlighten me on this ?
It's not a program that should compile. If a compiler accepts it the output
is not specified by the standard.

class base
{
int data;
public :
base() { }
virtual void virt()
{
}
};
class derived:public base
{
int data;
public :
derived(int a):data(a) { }
virtual void virt()
{
std::cout << data ;
}
};
int main()
{
int a = 1;
base *b[3] ;
for(int i = 0; i<3;++i)
{ b[i] = &derived(a++);b[i]->virt();
Here you take the address of a _temporary_ object and store that address in
b[i]. After the assignment b[i] is a dangling, invalid pointer. Taking the
address of that temporary object should not compile.
}
for(int i = 0; i<3;++i)
b[i]->virt();
return 0 ;
}


If you're using MSVC, which is one compiler that does let you compile this
(via a non-standard extension), add the switch /W4 to the command, or change
your project settings correspondingly, to get warnings about such things.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 17 '05 #5
I have another problem regarding the virtual functions. I am putting
that in the same thread since it uses the same "address of local
variable" aspect.

#include <iostream>
class base
{
int data;
public :
base() { }
virtual void virt() {
std::cout << "Base version of VF called " << std::endl;
}
virtual ~base() { }
};
class derived:public base
{
int data;
public :
derived(int a):data(a) { }
virtual void virt()
{
std::cout << " Derived Version of VF called " << std::endl;
}
};
int main()
{
base *b = &derived(1);
b->virt();
}

My question is - In this situation (when there is an empty destructor
in the base class), the output is -
"Base Version of VF called"

Whereas if I remove the empty destructor from the base class, then the
output becomes -

"Derived Version of VF called"

Confused. Can somebody explain what I am missing out here?

Thanks in advance
~Neelesh

Oct 18 '05 #6
* Neelesh:
I have another problem regarding the virtual functions.
No, it's exactly the same problem as before.
base *b = &derived(1);


Address of temporary object => dangling pointer => Undefined Behavior.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 18 '05 #7

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

Similar topics

4
2563
by: Kevin L | last post by:
Below is a code snippet that fails to compile under vc.net class BaseInterface { public: virtual void f() = 0; }; class BaseImplementation : public BaseInterface {
8
3198
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
4
1595
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
4
2881
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
3
4532
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
4
1555
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
23
4578
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
3
2534
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
2
2604
by: mmcgarry.work | last post by:
Hi, I would like to follow Stroustrup's advice of separating an object interface (abstract class) from an object implementation (concrete class), See Section 15.2.5 in Stroustrup 3rd Edition. ...
6
4520
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
0
7223
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
7372
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...
0
7482
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
4702
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...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.