Removing code repetition using casting operators [duplicate] - c++

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.

Related

unique_ptr usage in c++ [duplicate]

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)

What does v=0 in function parameters means [duplicate]

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

Need to understand when parameters passed as (className& arg) in c++ [duplicate]

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.

C++ define 3 arguments but using just 2 [duplicate]

This question already has answers here:
Default arguments in constructor--C++
(4 answers)
Closed 8 years ago.
I would like to use different number of arguments.
class A {
public:
A(int a, int b);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5); // I use only 1 argument and the second one I let default ?
}
Constructors are (a bit special) functions - regular default parameter syntax applies.
class A {
public:
A(int a, int b = default_value);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5);
}

When we use sizeof operator on object of that class which do not have any data members. Then what would be the output? [duplicate]

This question already has answers here:
C++: What is the size of an object of an empty class?
(17 answers)
Closed 9 years ago.
When we use sizeof operator on object of that class which do not have any data members. Then what would be the output? Why this always output 1??
class Abhi
{
public :
int sum (int, int);
int avg (int);
};
int Abhi:: sum(int a, int b)
{
float c;
c=a+b;
return 0;
}
int main()
{
Abhi abh;
int c;
c= sizeof(abh);
cout<<c;
}
According to the standard, each instance of a class must have a unique address. This is usually implemented by giving an "empty" class (no data members) a minimum size, as if there is a dummy char inside. This is why sizeof gives 1 and not 0.