By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Objects, Pointers and References

P: n/a
#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


Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
#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
Jul 19 '05 #2

P: n/a
"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
#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.
a1.fa();
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

Jul 19 '05 #3

P: n/a

"David White" <no.email@provided> wrote in message
news:0z*****************@nasal.pacific.net.au...
"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
#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.
a1.fa();
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

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?


Jul 19 '05 #4

P: n/a
"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...

"David White" <no.email@provided> wrote in message
news:0z*****************@nasal.pacific.net.au...
"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
[snip]
int main()
{
/*
// 1. Normal objects
A a;
B b;
a = b;

[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

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.