Why I am not getting output. I was expecting fun(Test2) called as output. As the parameters passed are different for the function so there shouldn't be any conflict.
#include <iostream>
using namespace std;
class Test2
{
int y;
};
class Test
{
int x;
Test2 t2;
public:
operator Test2 () { return t2; }
operator int () { return x; }
};
void fun ( int x) { cout << "fun(int) called"; }
void fun ( Test2 t ) { cout << "fun(Test 2) called"; }
int main()
{
Test t;
fun(t);
return 0;
}
Your call to fun() is ambiguous. t is of type Test, which can be converted to both Test2 and int, hence both fun implementations are candidates.
Related
I am learning C++ and I have written the below given simple program to understand the working of friend function
(ignore all the complication I made by using complex syntax in the code because I am learning and I practice the syntax that I learn in programs).
The friend function is not accessing the private members of test and stu.
#include<iostream>
#include<conio.h>
class test;
class stu
{
private:
int z;
public:
stu(int z)
{
this->z=z;
}
friend disp(stu,test);
~stu(void)
{
std::cout<<"Destructor of stu class is executed!!"<<std::endl;
}
};
class test{
private:
int x;
public:
test(int a)
{
x=a;
}
friend disp(stu,test);
~test(void)
{
std::cout<<"Destructor is executed!!"<<std::endl;
}
};
class test2:public test
{
private:
int b;
public:
test2(int b)
{
this->b=b;
}
void show(void);
~test2(void)
{
std::cout<<"Destructor of second class executed!!"<<std::endl;
}
};
int main()
{
test t1(3);
test2 t2(5);
t2.show();
stu s1(10);
disp(s1,t1);
return 0;
}
void test2::show(void)
{
std::cout<<"Value of k = "<<b<<std::endl;
}
void disp(stu s2, test t2)
{
int sum;
sum = s2.z + t2.x;
std::cout<<"Sum = "<<sum<<std::endl;
}
Try to define the disp function before the main function :
void disp(stu s2, test t2)
{
int sum;
sum = s2.z + t2.x;
std::cout<<"Sum = "<<sum<<std::endl;
}
int main()
{
test t1(3);
test2 t2(5);
t2.show();
stu s1(10);
disp(s1,t1);
return 0;
}
and change the disp function signature as:
friend void disp(stu,test);
For the following code, Fun() is returning a temporary object of Test type
#include<iostream>
#include<typeinfo>
using namespace std;
class Test
{
private:
int x;
static int count;
public:
Test(int i=0 ) {cout<<"sumit";}
Test(const Test& rhs) : x(rhs.x) { ++count; cout<<"sum"; }
static int getCount() { return count; }
};
int Test::count = 0;
Test fun()
{
Test ob(7);
return ob;
}
int main()
{
Test a=fun();
cout<<typeid(fun()).name();
cout<< Test::getCount();
return 0;
}
Still copy is not taking place
The output of the code:
constructor 4Test
I'm trying to make this code work:
#include <iostream>
using namespace std;
int f(int x) {
return x+1;
}
class A {
public:
int g(int y);
};
int A::g(int y) = f;
int main() {
A test;
cout << test.g(3) << endl;
return 0;
}
It does not compile because of the line int A::g(int y) = f;.
What is the correct way to achieve that an external function can be used as a method?
You can use a pointer to function as a member of class A. Now assign function f to g member of A.
int f(int x) {
return x+1;
}
class A {
public:
int (*g)(int);
};
int main(){
A test;
test.g = f;
cout << test.g(10); // prints 11
}
You can accomplish the same by making your function a callable objects by implementing () operator. so you can have that as member of the class, and then it can normally be used as function on the class objects.
#include <iostream>
struct do_something{
int operator()(int num){
return num;
}
};
class test{
int sum;
public:
do_something fun;
};
int main(){
test obj;
std::cout << obj.fun(10);
}
How to read the following code for main?
I do not know this
Code :
class one
{
public:
void operator()() const
{
f();
f1();
}
};
I want to call the operator To main?
void operator()() const defines a function call operator, which can be used as:
one ob;
ob(); // calls ob.operator()()
For another, more complete, example.
#include <iostream>
class Two
{
public:
int operator()(const char *str) const
{
std::cout << "operator() called with " << str << std::endl;
return 101;
}
};
int main()
{
Two two;
int n = two("'test'");
std::cout << "operator() returned " << n << std::endl;
}
Output:
operator() called with 'test'
operator() returned 101
You can create an instance of the class in the main function and call the function using that instance.
class one
{
public:
void operator()() const
{
f();
f1();
}
};
int main() {
one obj_one;
// calling the member function -> method
obj_one.operator()();
return 0;
}
I'm trying to pass a reference to a function in a class but am having trouble figuring out how to do it. So say I have a class test defined as such
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int);
int getA (void);
private:
int a;
};
void test::setA (int A) { a = A; }
int test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
T.setA(5);
cout << "a = " << T.getA() << endl;
return 0;
}
That works fine but if I want to pass the values by reference
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int);
int & getA (void);
private:
int a;
};
void test::setA (int & A) { a = A; }
int & test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
T.setA(5);
cout << "a = " << T.getA() << endl;
return 0;
}
I cannot figure out how to configure setA to pass by reference.
There are two issues with the code. First, the definition of setA does not match the declaration. You must make the declaration take in a reference as a parameter.
Change this:
void setA (int);
To this:
void setA (int&);
The second issue is that you are trying to pass an r-value (5) as a reference. You must pass in an l-value. You can do that by creating an int first and then passing that by reference:
int i = 5;
T.setA(i);
Full example:
#include <iostream>
class test {
public:
test () {};
~test () {};
void setA (int&);
int & getA (void);
private:
int a;
};
void test::setA (int & A) { a = A; }
int & test::getA (void) { return a; }
using namespace std;
int main ()
{
test T;
int i = 5;
T.setA(i);
cout << "a = " << T.getA() << endl;
return 0;
}
When you pass something by reference to a function in C++, the function does not keep the parameter in memory automatically. Thus, you have to declare it before so that it stays in memory throughout the entire function.
The 5 you tried to pass as a reference would go out of scope and get destroyed as soon as the function starts. The declared i variable is instead destroyed at the end of the main function.
The reason is because in order to pass by reference, you must have an lvalue, which is a fancy way of saying something that persists beyond a single use.
If you created an int variable, you would be able to pass it in by reference. In the code above, you attempted to pass in a raw integer value (5), which fails, since the compiler is expecting a reference to an int, not a raw integer value.
The following code would work:
int main ()
{
test T;
int myVariable = 4; // Need an actual variable to pass by reference.
T.setA(myVariable);
cout << "a = " << T.getA() << endl;
return 0;
}
However, if you want your function to take raw integer values like you showed in your second example, you must have a function definition like your first example, where all the function takes is an integer. Hope this helps!
Maybe you could try this:
#include <iostream>
class test {
public:
test() {};
~test() {};
void setA(int&&); // requires at least C++11
void setA(int&);
int & getA(void);
private:
int a;
};
void test::setA(int && A) { a = A; }
void test::setA(int&A) { a = A; }
int & test::getA(void) { return a; }
using namespace std;
int main()
{
test T;
int i = 5;
T.setA(i);
cout << "a = " << T.getA() << endl;
T.setA(8);
cout << "a = " << T.getA() << endl;
return 0;
}
In the example, int& passes a l-value while int&& passes a r-value as a reference.