c++ reference to a class member but not changing value - c++

Please help me to review the following code.
I am wondering why the variable "b" is not the modified value.
I can not change the value using reference ?
Thanks!
#include <iostream>
using namespace std;
class Foo{
public:
int a = 1;
int& check(){
return a;
};
};
int main()
{
int b;
Foo foo;
b = foo.check();
cout << b << endl;
foo.check() = 2;
cout << foo.a << endl;
cout << b << endl;
return 0;
}
The output is
1
2
1

As #Igor Tandetnik indicated, foo.check returns a reference, but b is an int, not a reference to int, so it keeps the original value.
What you want can be achieved by ...
#include <iostream>
using namespace std;
class Foo
{
public:
int a = 1;
int &check()
{
return a;
};
};
int main()
{
Foo foo;
int &b { foo.check() };
cout << b << endl;
foo.check() = 2;
cout << foo.a << endl;
cout << b << endl;
return 0;
}

Related

c++11 - zero-initi of members insted of default-init

In the below code, I expect members of a being inited with gargabe as they are not mentioned in the members-init-list of the called constructor (with two int parameters). Instead, I'm constantly getting 0 in both i and j of a, b and c and I am failing to see why. Could anybody please point me in the right direction?
#include <iostream>
#include <type_traits>
class A {
public:
int i;
int j;
A() = delete;
A(int a, int b) { std::cout << "VOLOLO!" << std::endl; };
};
int
smear_stack(int p)
{
int j = p++;
int a[500] = {};
for(int i = 0; i < 499; i++) {
a[i] = ++j;
}
std::cout << j << std::endl;
return ++j;
}
int main(void)
{
int i = 2;
i = smear_stack(++i);
A a (i, 32 );
std::cout << "a is " << a.i << " " << a.j << std::endl;
A b = { a };
std::cout << "b is " << b.i << " " << b.j << std::endl;
A c = { a.i, a.j };
std::cout << "c is " << c.i << " " << c.j << std::endl;
}
The i and j fields that you are accessing are, indeed, uninitialized. However, you are smearing the wrong part of the stack. It just so happens that on most OSes, the stack is initially all zeros. It even used to be common in C (long ago) to assume that automatic variables in main were zero-initialized.
To see that the memory is indeed uninitialized, it suffices to put something else there. Here's an example:
#include <iostream>
#include <memory>
class A {
public:
int i;
int j;
A() = delete;
A(int a, int b) { std::cout << "VOLOLO!" << std::endl; };
};
union U {
int is[2];
A a;
U() : is{3,4} {}
};
int
main()
{
U u;
std::construct_at(&u.a, 5, 6);
std::cout << u.a.i << ", " << u.a.j << std::endl;
// output:
// VOLOLO!
// 3, 4
}

Clarification about function calls in cout needed

i'm doing an online c++ learning course with quiz. The last output line of this snippet is to be determined (comments added by me). Correct answer: 10. My question: why 10 and not 11?
Calling a(b) swaps the two variables, so why is the last a.a.b 0 and not 1? / Why does the a.b() in cout not affect the a.a.b?
#include <iostream>
using namespace std;
class classA {
public:
classA() { st.f = st.p = 1; }
struct { int f, p; } st;
int bfunc(void);
};
int classA::bfunc(void) { int x = st.f; st.f = st.p; st.p = x; return x; };
int main()
{
classA a;
a.st.f = 0;
cout << a.st.f << a.st.p << endl; //01
a.bfunc();
cout << a.st.f << a.st.p << endl; //10
a.bfunc();
cout << a.st.f << a.st.p << endl; //01
a.bfunc();
cout << a.st.f << a.st.p << endl; //10
cout << a.bfunc() << a.st.p << endl; //10
return 0;
}

How to call a setter function from one class in another class?

I'm trying to call the setter function from Class A in class B but I'm unable to do this and I don't know why. (I'm a beginner in C++ and I need to do this as it's related to my assignment.)
Here is my code:
#include <iostream>
using namespace std;
class A{
private:
int i = 2;
public:
int getInt(){
return i;
};
void setI(int a){
i = a;
}
};
class B{
private:
int c = 3;
public:
void setAI(A a){
a.setI(c);
}
};
int main() {
A a;
B b;
cout << a.getInt() << endl;
b.setAI(a);
cout << a.getInt() << endl;
a.setI(5);
cout << a.getInt() << endl;
}
The output I'm getting is:
2
2
5
When I want to get:
2
3
5
I just made some changes in the setAI() function. I have passed the object by address rather than value. I'm not sure of what was causing the problem (as I'm also a beginner) but the pointer has solved the problem.
#include <iostream>
using namespace std;
class A{
private:
int i = 2;
public:
int getInt(){
return i;
};
void setI(int a){
i = a;
}
};
class B{
private:
int c = 3;
public:
void setAI(A *a){
a->setI(c);
}
};
int main() {
A a;
B b;
cout << a.getInt() << endl;
b.setAI(&a);
cout << a.getInt() << endl;
a.setI(5);
cout << a.getInt() << endl;
}

Changing values in arrays of a different class

(c++ problem) I have two classes A and B, and the main class(int main). I have an array in class A. In the main class, I want to make a change to the array in class A. When class B reads the array from class A, it should read the changed values. However, changes to the array I make in class A through int main take effect for all things in int main only, NOT class B. In other words, I am unable to permanently change the values in class A.
I created a dummy program (c++) to showcase my problem. If I input 3 for x (first cin) and 9 for y(second cin), the output is
00090
0
when it should be
00090
9
#include <math.h>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;
class A {
public:
int array[5] = { 0,0,0,0,0 };
int getNum(int index)
{
return array[index];
}
void changeNum(int index, int change)
{
array[index] = change;
}
};
class B {
public:
A obj1;
int getNum(int index)
{
return obj1.getNum(index);
}
};
int main()
{
A obj2;
B obj3;
int x,y;
cout << "Original Array: " << endl;
for (int i = 0; i < 5; ++i)
cout << obj2.getNum(i);
cout << endl << endl << "Enter index number:" << endl;
cin >> x;
cout << "Enter new number" << endl;
cin >> y;
obj2.changeNum(x, y);
for (int i = 0; i < 5; ++i)
cout << obj2.getNum(i);
cout << endl << obj3.getNum(x) << endl;
system("pause");
return 0;
}
If you want changes to the array in A to be visible globally, then you can make array a static variable like this:
class A {
public:
static int array[5];
// ...
};
int A::array[5] = { 0,0,0,0,0 };
From c++17, you could also define array inside the class:
class A {
public:
inline static int array[5] = { 0,0,0,0,0 };
// ...
}
Here's a demo.
Also, avoid doing using namespace std;. e.g. there is already an std::array, so variable names like array can cause serious problems.

C++ how to overload method using pass by value and pass by reference

How do I make the following overload work
#include <iostream>
using namespace std;
int subtractFive (int a)
{
a = a - 5;
return a;
}
int subtractFive (int &a)
{
a = a -5;
return a -5;
}
int main()
{
int A = 10;
cout << "Answer: " << subtractFive(*A) << endl;
cout << "A Value "<< A << endl;
cout << "Answer: " << subtractFive(A) << endl;
cout << "A Value "<< A << endl;
return 0;
}
Tried but doesnt compile
#include <iostream>
using namespace std;
int subtractFive (int a)
{
a = a - 5;
return a;
}
void subtractFive (int* a)
{
*a = *a -5;
}
int main()
{
int A = 10;
cout << "Answer: " << subtractFive(A) << endl;
cout << "A Value "<< A << endl;
subtractFive(A);
cout << "A Value "<< A << endl;
return 0;
}
You might try specifying an overload that takes an address as an argument:
int subtractFive (int *a)
{
*a = *a -5;
return *a -5;
}
Declare one function as pass by address the other by value or reference:
void subtractByFive(int * p_value)
{
if (p_value != NULL)
{
*p_value -= 5;
}
return;
}
A value and a reference have the same type so you can't overload on it. If you want two functions one of which modifies its parameter and one that returns the new value then you either have to give them different names or different types (e.g. make the latter function use a pointer type).