implicite function call

M

manny

Hi all c++ experts
I have a problem with determining why are certain functions called in
the given code example.
In main, fkcja1(one) [ void fkcja1(A as) ] is called, then in its body
it should call fkcja1(1) but actually B::B(int a) is called (WHY?) and
before returning from fkcja1(one) there is a call to
[ void fkcja1(B be) ] (WHY?)

Why there is no call to void fkcja1(int c) !

I'd be grateful for explaining or hint about language feature
responsible for this behaviour so I could dig for more info.

//code example
#include <iostream>
#include <stdio.h>
using namespace std;

class B;
class B {
public:
B();
B(int a);
};

class A {
public:
A();
fkcja1(B be);
};

void fkcja1(B be)
{
B next(1);
}

void fkcja1(A as)
{
B be(1);
fkcja1(1);
}

void fkcja1(int c)
{
cout << "E\n";
}

B::B()
{
cout << "C" << endl;
A next;
}

B::B(int a)
{
cout << "D\n";
}

A::A()
{
cout << "A\n";
}

int main()
{
A one;
fkcja1(one);
return 0;
}
 
A

Alf P. Steinbach

* manny:
>
Hi all c++ experts

There are probably only four or five C++ experts in the world.

I have a problem with determining why are certain functions called in
the given code example.
In main, fkcja1(one) [ void fkcja1(A as) ] is called,

Are you /sure/ you there is no better name for that function?

then in its body
it should call fkcja1(1) but actually B::B(int a) is called (WHY?)

Because you told it to,

void fkcja1(A as)
{
B be(1);

This calls B::B( int ) and

fkcja1(1); // X

this also invokes the B::B( int ) constructor.

}

and before returning from fkcja1(one) there is a call to
[ void fkcja1(B be) ] (WHY?)

At the point in the code where you have the call marked X above, the
only as-yet-encountered declaration of fkcja1 that is compatible with
the call, is fkcja1(B), via conversion of int to B.

Note that your declaration of a B member function called fkcja1 (which
is never defined) is unrelated to the three freestanding functions you
have also chosen to name fkcja1.

Why there is no call to void fkcja1(int c) !

Because that function is not yet declared, and thus unknown, at the
place you're trying to call it.

I'd be grateful for explaining or hint about language feature
responsible for this behaviour so I could dig for more info.

//code example
#include <iostream>
#include <stdio.h>

Don't #include headers you don't use.

using namespace std;

class B;
class B {
public:
B();
B(int a);
};

class A {
public:
A();
fkcja1(B be);

This member function is never defined anywhere.
};

void fkcja1(B be)
{
B next(1);
}

void fkcja1(A as)
{
B be(1);
fkcja1(1);
}

At this point only fkcja1( B ) is known.

void fkcja1(int c)
{
cout << "E\n";
}

Are you sure no better diagnostic output could be devised? ;-)

[snipped further code]
 
M

manny

Hi
thanks for quick response,
Of course the names are terrible (in fact it is not my code) and
includes are unnecessary, I'll do my best to not post again such ugly
code ;)
best regards
manny
 

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,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top