This question already has answers here:
How can I pass std::unique_ptr into a function
(8 answers)
Closed 3 years ago.
How do I pass the unique_ptr in the below code? This code does not compile.
int abc(int*& a)
{
return 1;
}
int main()
{
std::cout<<"Hello World";
std::unique_ptr<int> a(new int);
abc(a.get());
return 0;
}
You may pass it by const reference or reference for example:
int abc(const std::unique_ptr<int>& a)
Related
This question already has answers here:
How do I remove code duplication between similar const and non-const member functions?
(21 answers)
Elegant solution to duplicate, const and non-const, getters? [duplicate]
(8 answers)
Avoid literally duplicating code for const and non-const with auto keyword?
(3 answers)
Closed 6 days ago.
class nums
{ public:
int sum(int a,int b) const
{
return a+b;
}
int sum(int a,int b)
{
return a+b;
}
};
int main()
{
const nums a;
a.sum(5,10);
nums b;
b.sum(1,2);
}
I am trying remove two functions for const objects and non const objects replace with one function using casting operators.
You can use int sum(int a,int b) const even if the object isn't marked as const. See this answer.
So, the following code would compile:
class nums
{
public:
int sum(int a,int b) const
{
return a+b;
}
};
int main()
{
const nums a;
a.sum(5,10);
nums b;
b.sum(1,2);
}
Link to Compiler Explorer.
This question already has answers here:
How to make a function return a pointer to a function? (C++)
(14 answers)
Closed 3 years ago.
How to return an void (*)(void) variable from a function?
More precisely how to mark the return type?
???? getFunc(){
void (*pt2Func)(void) = ...;
return pt2Func;
}
void main(){
void (*myFunc)(void) = getFunc();
myFunc();
}
The most readable approach is via a type alias:
using VoidFct = void (*)(void);
VoidFct getFunc(){
return &f;
}
where the function to which a pointer is passed could be
#include <cstdio>
void f() {
std::puts("laksjdf");
}
and the client could would be
VoidFct g = getFunc();
g();
This question already has answers here:
assignment operator within function parameter C++
(3 answers)
Closed 5 years ago.
Here is a modified code from geeks for geeks.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
What does parameter in function Test(int v=0) means?
That is default value for v parameter, which is the v variable will use that value if there is no value passed
This question already has answers here:
Function pointer vs Function reference
(4 answers)
Closed 6 years ago.
int g()
{
return 0;
}
int f1(int(fn)())
{
return fn();
}
int f2(int(*fn)())
{
return fn();
}
int main()
{
f1(g);
f2(g);
}
The code above can be sucessfully compiled.
I just wonder:
What's the difference between int f(int(fn)()) and int f(int(*fn)())?
Why does both of them are legal in C++?
int f(int(fn)()) and int f(int(*fn)()) are the same thing. A function takes a function pointer as parameter.
When we pass the name of a function as parameter, the function name is automatically converted to a pointer. So
int f(int(fn)()) {}
int f(int(*fn)()) {} // redefinition error
This question already has answers here:
"const T &arg" vs. "T arg"
(14 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
// const objects
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
const int& get() const {return x;}
};
void print (const MyClass& arg) { // Need to understand this line
cout << arg.get() << '\n';
}
int main() {
MyClass foo (10);
print(foo);
return 0;
}
I am new to C++. Need to understand what are the parameters passed in print function. If this is address then why are we passing foo is print function call.
The & in void print (const MyClass& arg) that arg is passed by reference. It is C++s way to make pointers and things a little bit easier.
References allow you to manipulate a variable inside of a function and make the results visible on the outside too. So a bit like pointers. But you don't need to explicitly get the address of the variable to do that.
The const statement is a way to prevent the described behavior. const forbids the manipulation of arg inside print.