How this line is working in this C++ code? - c++

I was testing how the deference between the dot member selection operator and the arrow member selection operator (.) and (->).
but i don't understand how when I put the reference equaling to itself. it works. and if i deleted any of the two lines above it. it gives me error.
#include <iostream>
using namespace std;
class count
{
public:
void setX(int value)
{
x=value;
}
void printX()
{
cout<<x<<endl;
}
private:
int x;
};
int main()
{
count counter;
count *counterPtr=&counter;
count &counterRef=counterRef; // here! I mistakenly put the
//to reference to itself. but it
//works fine.
counter.setX(7);
counter.printX();
counterRef.setX(8);
counterRef.printX(); //it works fine as and prints the value
(*counterPtr).setX(9);
(*counterPtr).printX();
counterPtr->setX(10);
counterPtr->printX();
}

Related

unable to access static methods (C++)

Here's the code:
#include <iostream>
using namespace std;
class Zaix
{
private:
static int mor;
public:
static int beri;
static void setmor(int lip)
{
Zaix::mor=lip;
}
static int getmor(void)
{
return mor;
}
};
int Zaix::beri=3;
int main()
{
cout<<Zaix::beri<<endl;
Zaix::beri++;
cout<<Zaix::beri<<endl;
Zaix::setmor(6);
return 0;
}
Now, line 4 of main() function Zaix::setmor(6); somehow invalidates line 11 of the code presented Zaix::mor=lip;. With this line commented out, the whole thing compiles OK, with it present, compiler gives this error:
undefined reference to Zaix::mor"
Any idea why that is?
Define the variable outside class as well.
int Zaix::mor;
For assignment:
int Zaix::mor = 4;
In C++ we need to define all the static member variable of a class outside of it else we get a linking error. You just need to do like below:-
int Zaix::mor;// Just add this line below int Zaix::beri = 3;

C++ Pointer crashes (Uninitialized)

It seems this problem is the so-called dangling pointer problem. Basically I'm trying to parse a pointer into a function (that stores the pointer as a global variable) inside a class, and I want the pointer to be stored in that class and can be used now and then. So from inside the class, I can manipulate this pointer and its value which is outside of the class.
I simplified the code and re-created the situation as the following:
main.cpp
#include <iostream>
#include "class.h"
using namespace std;
void main() {
dp dp1;
int input = 3;
int *pointer = &input;
dp1.store(pointer);
dp1.multiply();
}
class.h
#pragma once
#include <iostream>
using namespace std;
class dp {
public:
void store(int *num); // It stores the incoming pointer.
void multiply(); // It multiplies whatever is contained at the address pointed by the incoming pointer.
void print();
private:
int *stored_input; // I want to store the incoming pointer so it can be used in the class now and then.
};
class.cpp
#include <iostream>
#include "class.h"
using namespace std;
void dp::store(int *num) {
*stored_input = *num;
}
void dp::multiply() {
*stored_input *= 10;
print();
}
void dp::print() {
cout << *stored_input << "\n";
}
There is no compile error but after running it, it crashes.
It says:
Unhandled exception thrown: write access violation.
this->stored_input was 0xCCCCCCCC.
If there is a handler for this exception, the program may be safely continued.
I pressed "break" and it breaks at the 7th line of class.cpp:
*stored_input = *num;
It is not a dangling pointer, but a not initialized, you probably want:
void dp::store(int *num) {
stored_input = num;
}

I am unable to initialize a data member of object by passing object as parameter

#include<iostream>
using namespace std;
class one
{
public:
int datam;
void show()
{
cout<<datam;
}
};
void addv(one par)
{
par.datam=2;
}
int main()
{
one w;
addv(w);
w.show();
return 0;
}
After compilation this gives garbage value. Why can't I initialize datamember(datam) of object w.
I know there are other methods to initialize but what is the problem in this method?
You need to pass by reference. See the modified code below:
#include<iostream>
using namespace std;
class one
{
public:
int datam;
void show()
{
cout<<datam;
}
};
void addv(one &par)
{
par.datam=5;
}
int main()
{
one w;
addv(w);
w.show();
return 0;
}
Note that I have changed the prototype of function addv() as:
void addv(one &par)
//------------^^^^
In your code, you are passing by value (and not reference) due to which, you get a garbage value. Working code here.
You are not passing anything by reference. To pass by reference your function should be declared as void addv(one &par) . So you are passing a copy and initializing the copy which gets destroyed before the function returns.

object.function().function().function()....... how does this work?

i have a problem understanding how c++ syntax works.
#include<iostream>
using namespace std;
class Accumulator{
private:
int value;
public:
Accumulator(int value){this->value=value;}
Accumulator& add(int n){value+=n;}
int get(){return value;};
};
int main(int argc, char* argv[]){
Accumulator acc(10);
acc.add(5).add(6).add(7); //<-----how does this work?????
cout<<acc.get();
return 0;
}
this line: acc.add(5).add(6).add(7);
does it work left to right or the other way
something like acc.add(5) first and then do add(6)
i dont get it.
result is supposed to be 28.
thanks in advance.
edit:
weird, this code gets compiled successfully without any errors on g++.
i got this code from some non-english college c++ textbook. english is not my first language.
2nd edit: i now get the desired warnings after using -Wall option.
Your code doesn't compile, but if it did, it would work left to right. Add returns a reference to an Accumulator (it doesn't have a return value in your code, but it should probably return *this) so after you call
acc.add(5)
the return value is a reference to acc, which you can call add on again.
Here is a modified example with mult added that shows order of operations:
#include <iostream>
using namespace std;
class Accumulator{
private:
int value;
public:
Accumulator(int value){ this->value = value; }
Accumulator& add(int n){ value += n; return *this; }
Accumulator& mult(int n){ value *= n; return *this; }
int get(){ return value; };
};
int main(int argc, char* argv[]){
Accumulator acc(10);
acc.add(5).add(6).mult(7);
cout << acc.get();
return 0;
}
If it was right to left, the result would be 81, but it is left to right and the result is 147.
This is called Method chaining and is commonly seen in Fluent Interface pattern.
Each method call (acc.add(5)) returns a reference or pointer upon which successive method calls (.add(7)) can operate on.

Updating vector by passing by reference in constructor

I am trying to write a piece of code where I am passing the vector by reference through constructor of a class and updating the vector in the member function of the class. But when I get back to the main function, no update occurs in the vector:
// Header file
class A{
private:
std::vector<T> &x;
public:
A(std::vector<T>& x_):x(x_) {}
int func();
};
// Cpp file
int A::func() {
// process done
T temp;
x.push_back(temp);
}
// Main function
int main() {
std::vector<T> vec;
A a(vec);
a.func();
}
I have tried changing the vector to be a pointer in the class instead of a reference but the vector doesnt update after the function runs. Any suggestions on what to change in the program?
#include <iostream>
#include <vector>
using namespace std;
class A
{
std::vector<int> &x;
public:
A(std::vector<int>& x_):x(x_) {}
int func();
};
int A::func(){
int temp=0;
x.push_back(temp);
return 0;
}
int main(){
std::vector<int> vec;
A a(vec);
a.func();
return 0;
}
everything is ok vec changed. I think you have another question or bug that you even dont aware of .