Objects, Pointers and References

V

Vinodh Kumar

#include <iostream>
using namespace std;

struct A
{
virtual void fa();
};


struct B : public A
{
virtual void fa();
};


void A::fa()
{
cout << "A::fa" << endl;
}


void B::fa ()
{
cout << "B::fa" << endl;
}

int main()
{
/*
// 1. Normal objects
A a;
B b;
a = b;
(&a)->fa();


// 2. Pointers
A a;
B b;
A* a1 = &b;
a1->fa();
*/

//3. References
A a;
B b;

A& a1 = b;
a1.fa();

return 0;
}


In the above program within main( ) inside the commented block // 1. Normal
objects,
a = b;
(&a)->fa();
does not make an invocation to B::fa();Why is that?Then what the above
assignment does in the same block.Shed some light please.


Regards,
Vinodh Kumar P
 
J

John Harrison

Vinodh Kumar said:
#include <iostream>
using namespace std;

struct A
{
virtual void fa();
};


struct B : public A
{
virtual void fa();
};


void A::fa()
{
cout << "A::fa" << endl;
}


void B::fa ()
{
cout << "B::fa" << endl;
}

int main()
{
/*
// 1. Normal objects
A a;
B b;
a = b;
(&a)->fa();


// 2. Pointers
A a;
B b;
A* a1 = &b;
a1->fa();
*/

//3. References
A a;
B b;

A& a1 = b;
a1.fa();

return 0;
}


In the above program within main( ) inside the commented block // 1. Normal
objects,
a = b;
(&a)->fa();
does not make an invocation to B::fa();Why is that?Then what the above
assignment does in the same block.Shed some light please.

It converts your B object into a A object. So when you call (&a)->fa(); all
you have is an A object. This is sometimes called object slicing.

The bottom line is that if you want to use virtual functions in C++, you
have to use pointers or references.

john
 
D

David White

Vinodh Kumar said:
#include <iostream>
using namespace std;

struct A
{
virtual void fa();
};


struct B : public A
{
virtual void fa();
};


void A::fa()
{
cout << "A::fa" << endl;
}


void B::fa ()
{
cout << "B::fa" << endl;
}

int main()
{
/*
// 1. Normal objects
A a;
B b;
a = b;

Here you are slicing the A part off 'b' and copying the slice to 'a'.
(&a)->fa();

Calls A::fa.
// 2. Pointers
A a;
B b;
A* a1 = &b;

Here you are simply taking the address of 'b'. You aren't copying from one
object to another.
a1->fa();

Calls B::fa.
*/

//3. References
A a;
B b;

A& a1 = b;

Here you are creating a reference to 'b'. Again, you aren't copying from one
object to another.

Calls B::fa.
return 0;
}


In the above program within main( ) inside the commented block // 1. Normal
objects,
a = b;
(&a)->fa();
does not make an invocation to B::fa();Why is that?Then what the above
assignment does in the same block.Shed some light please.

An object of class A cannot metamorphose into an object of class B. Once an
A always an A. So the function called in 1. is naturally A::fa. In the other
cases you were taking the address of and reference to a B object. So the
function called was naturally B::fa.

DW
 
V

Vinodh Kumar

David White said:
Here you are slicing the A part off 'b' and copying the slice to 'a'.


Calls A::fa.


Here you are simply taking the address of 'b'. You aren't copying from one
object to another.


Calls B::fa.


Here you are creating a reference to 'b'. Again, you aren't copying from one
object to another.


Calls B::fa.


An object of class A cannot metamorphose into an object of class B. Once an
A always an A. So the function called in 1. is naturally A::fa. In the other
cases you were taking the address of and reference to a B object. So the
function called was naturally B::fa.

DW
Any how the my 'A' object in "case 1" will have an address.When the contents
of this address is determined?
The contents 'a1' pointer in "case 2" can not be determined in compile
time?In that case how one can say C++ is a statically typed language?Or the
contents of 'a' can not be determined at run time?
 
D

David White

Vinodh Kumar said:
David White said:
[snip]
[snip]
An object of class A cannot metamorphose into an object of class B. Once an
A always an A. So the function called in 1. is naturally A::fa. In the other
cases you were taking the address of and reference to a B object. So the
function called was naturally B::fa.
Any how the my 'A' object in "case 1" will have an address.When the contents
of this address is determined?

The contents are what they are. It's an A object, so it will be set up to
call A::fa when called via a run-time virtual call.
The contents 'a1' pointer in "case 2" can not be determined in compile
time?

The decision of what function to call in all three cases is made at
run-time, since in all three cases you are calling a virtual function
through a pointer or reference.
In that case how one can say C++ is a statically typed language?

I'm not sure how a language lawyer would answer this, but to me it has a
mixture of static and dynamic types. Actual objects and member access of
objects are statically typed. Virtual calls through pointers and references,
dynamic_cast and other run-time type support are dynamically typed.
Or the
contents of 'a' can not be determined at run time?

'a' is an object of type A, so it can only behave like an A no matter how
you access it.

DW
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top