C
ciccio
Hi,
I have a problem and I don't know what is going on here.
I basically call a function defined as test(const bar &a), in which I
define a const reference to an object foo which comes from bar. At this
point I have a copy constructor which should not be the case.
A working example you can find below which explains it a bit better.
Am I just doing something stupidly wrong here?
Thanks for the help in any case.
// EXAMPLE HERE
#include <iostream>
class foo {
public:
foo() { std::cout << "i foo" << std::endl; }
foo(const foo &c) { std::cout << "c foo" << std::endl; }
~foo() { std::cout << "d foo" << std::endl; }
};
class bar {
public:
bar(void);
~bar(void);
foo operator()(void) const { return f; };
private:
foo f;
};
bar::bar(void) { }
bar::~bar(void) { }
void test(const bar &b) {
std::cout << "start test" << std::endl;
const foo &f2 = b();
std::cout << "end test" << std::endl;
}
int main(void) {
bar b;
test(b);
}
// THE OUTPUT
i foo
start test
c foo << DONT EXPECT THIS
end test
d foo
d foo
I have a problem and I don't know what is going on here.
I basically call a function defined as test(const bar &a), in which I
define a const reference to an object foo which comes from bar. At this
point I have a copy constructor which should not be the case.
A working example you can find below which explains it a bit better.
Am I just doing something stupidly wrong here?
Thanks for the help in any case.
// EXAMPLE HERE
#include <iostream>
class foo {
public:
foo() { std::cout << "i foo" << std::endl; }
foo(const foo &c) { std::cout << "c foo" << std::endl; }
~foo() { std::cout << "d foo" << std::endl; }
};
class bar {
public:
bar(void);
~bar(void);
foo operator()(void) const { return f; };
private:
foo f;
};
bar::bar(void) { }
bar::~bar(void) { }
void test(const bar &b) {
std::cout << "start test" << std::endl;
const foo &f2 = b();
std::cout << "end test" << std::endl;
}
int main(void) {
bar b;
test(b);
}
// THE OUTPUT
i foo
start test
c foo << DONT EXPECT THIS
end test
d foo
d foo