C++ Funda !!!

  • Thread starter Subhransu Sekhar Sahoo
  • Start date
S

Subhransu Sekhar Sahoo

Hu folks,

Can anyone explain why is the out put of the following program is
"from CB fun".

#include <iostream>

using namespace std;

class CA{
public:
void fun() {cout<<"from CA fun"<<endl;}
};

class CB
{
CA *p;
public:
CB(CA* tp=new CA):p(tp)
{
}
CA * operator->()
{
int x = 10;
return p;
}
void fun() {cout<<"from CB fun"<<endl;}
};


void main()
{

CB *obj = new CB;

obj->fun();

delete obj;
}

Thnking in advance,
Subhransu S
 
A

Alf P. Steinbach

* Subhransu Sekhar Sahoo:
Can anyone explain why is the out put of the following program is
"from CB fun".

In practice, because you're calling a function that gives that output.

In theory, it would be just a coincidence, since the program is not
valid C++.
 
I

Ivan Vecerina

Subhransu Sekhar Sahoo said:
Hu folks,

Can anyone explain why is the out put of the following program is
"from CB fun". ....
class CB
{ ....
CA * operator->() ....
};


void main()
{

CB *obj = new CB;

obj->fun();

delete obj;
}
Regarding the expression obj->fun(),
the -> operator applies to a pointer type (CB*),
not to an instance of CB. So the built-in -> operator is
the one being used, leading to a call to CB's member function.

To get the behavior you seem to expect,
one would have to write:
(*obj)->fun(); //in the above sample->calls CA::fun()
or:
int main() //NB: main always returns int, not void
{
CB obj;
obj->fun(); // calls CA::fun()
}
 
S

Subhransu Sekhar Sahoo

Hi Ivan,

Thanks for ur answer. Btw could you tell me
why is it that operator overloading does not
work with pointers to objects.

This might be a very basic qus. Pls help.

Regards,
Subhransu S
 
B

bart.kowalski

Subhransu said:
Hi Ivan,

Thanks for ur answer. Btw could you tell me
why is it that operator overloading does not
work with pointers to objects.

Because you can't overload operators for basic types. Pointers are
basic types, while classes are not. So overloading works on class
objects but not on pointers.


Bart.
 
J

John Carson

Subhransu Sekhar Sahoo said:
Hi Ivan,

Thanks for ur answer. Btw could you tell me
why is it that operator overloading does not
work with pointers to objects.

This might be a very basic qus. Pls help.

By section 5.2.5/3 of the standard,

"If E1 has the type 'pointer to class X,' then the expression the expression
E1->E2 is converted to the equivalent form (*(E1)).E2"

Thus your

CB *ptr = new CB;

ptr->fun();

translates to

(*ptr).fun();

By contrast, when you have:

CB obj;

obj->fun();

obj is not of pointer type, so the passage quoted above does not apply. This
means that the overloaded operator -> is called.

Basically, operator-> is not meant to be used with pointers. It is meant to
be used with objects acting like pointers, such as smart pointers and
iterators.
 
A

Abhi

Else can we overload the "." operator and achieve similar results? (I
am not sure whether we can really overload the . operator)

void main()
{
CB obj;
obj.fun();
}

-Abhishikt
 
J

John Carson

Abhi said:
Else can we overload the "." operator and achieve similar results? (I
am not sure whether we can really overload the . operator)

void main()
{
CB obj;
obj.fun();
}

-Abhishikt

Overloading the . operator is not allowed.
 
K

Karl Heinz Buchegger

Abhi said:
Else can we overload the "." operator and achieve similar results? (I
am not sure whether we can really overload the . operator)

void main()
{
CB obj;
obj.fun();
}

If you make that a class and have an object of that class, then
you *can* create an operator -> for it.

It is just that with builtin types (int, long, double, char, ...
*and* pointers) you cannot create your own operators for them.
 
A

Abhi

hmmm... I would be delighted to hear the rational behind it. Can you
pls give details...

-Abhishikt
 
J

John Carson

Abhi said:
hmmm... I would be delighted to hear the rational behind it. Can you
pls give details...

-Abhishikt

For one thing, it makes it difficult to access class members.
 
M

Marcin Kalicinski

hmmm... I would be delighted to hear the rational behind it. Can you
pls give details...

I suggest you get "Design and Evolution of C++" by Stroustrup. There, a
better part of one chapter deals with the matter.

cheers,
Marcin
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top