C++ global reference vs. local variable - c++

I am starter in this language and I have question: Does anybody can explain why this code works? Even if it works, Is this code "dangerous"? Is it a good practice to use global references in C++?
#include <stdio.h>
#include <iostream>
using namespace std;
class v3 {
protected:
double t[3];
public:
v3(double x, double y, double z);
void printout(void);
};
v3::v3(double x, double y , double z) {
t[0]=x;
t[1]=y;
t[2]=z;
}
void v3::printout(void) {
cout<<"(";
for (int i = 0; i < 2; i++) cout<<t[i]<<";";
cout<<t[2]<<")"<<endl;
}
v3 global(3, 3, 3);
v3& globalref = global;
void copylocal2globalref(void) {
v3 local(4, 4, 4);
globalref = local;
}
int main()
{
globalref.printout();
copylocal2globalref();
globalref.printout();
return 0;
}
I thought that the object called 'local' does not exist after the invocation of function called 'copylocal2globalref()'. But I could print out the reference which points to 'local' object! How?!? By the way Is the "reference points to an object" a good expression? It sounds like the refrence would be a pointer but it is obviously not!

Related

Assignment to a function pointer in the global scope

I am trying to define a type for function pointers, in c++. However, once I define the pointer funcp as a fptr, I cannot redefine the pointer as a different function, times. This program does not even compile. Is this because I have to delete the nullptr before reassigning times to funcp?
My code:
#include <iostream>
using namespace std;
typedef int (*fptr)(int, int);
int times(int x, int y)
{
return x*y;
}
fptr funcp = nullptr;
funcp = times;
int main()
{
return 0;
}
The problem is that you are trying to do that outside of any function. Try that:
int main()
{
funcp = times;
return 0;
}
Your question has nothing special for C++17. To make it a little more modern and easier to read you may use the using instead of typedef:
using fptr = int (*)(int, int);

I'm trying to learn how to pass pointers in c++, but I get error: no matching function for call to 'test'. What am I doing wrong?

Here's what I have:
#include <iostream>
using namespace std;
void test(double &r)
{
r = 0.1;
}
int main() {
double rdefined;
double yo = test(&rdefined);
cout << yo <<endl;
return 0;
}
I've tried putting the test function after the main function and assigning rdefined as 0.0 .
The function declaration void test(double &r) specifies that the argument is passed by reference not as a pointer. To pass a "pointer-to-double" use: void test(double *r).
Although, in practice, the effect is very similar, when passing an argument by reference, you don't explicitly give the address; so, in your code, the call should be as shown below. Also, as noted in the answer given by Vettri, your function does not return a double value (or, indeed, anything, as it is void), so you can't assign the variable yo directly from the call to it.
Try this, as one possible solution:
test(rdefined); // Changes the value of "rdefined"
double yo = rdefined; // Assigns modified value to "yo"
For a discussion on the differences between pointers and references, see here.
Corrected Solution:
#include <iostream>
using namespace std;
double test(double* r)
{
*r = 0.1;
return *r;
}
int main() {
double rdefined;
double yo = test(&rdefined);
cout << yo <<endl;
return 0;
}
You need to specify the correct return value. You got an error because you are expecting a double value in return of your test function, but you declared it as void.
The only thing you need to do is, to replace & with * in the function parameters. here is the code.
#include <iostream>
using namespace std;
void test(double* r)
{
*r = 0.1;
}
int main() {
double rdefined;
test(&rdefined);
cout << rdefined <<endl;
return 0;
}

C++ get data from double pointer

I am new in C++ area and faced with double pointers. The question is how to set x=5 in function test?
#include <iostream>
using namespace std;
typedef struct _DoublePointer
{
int x;
int y;
} DoublePointer;
void test(_DoublePointer** pointer)
{
}
void main()
{
_DoublePointer* pointer = new _DoublePointer;
_DoublePointer** doublePointer = &pointer;
test(doublePointer);
}
the way to do that is
(*pointer)->x = 5;

Bad Access Error on XCode C++

I am trying to implement a Polynomial structure using a linked list of Terms (the linked list is implemented separately).
When I run my main program, I get a (Thread 1: EXC_BAD_ACCESS code=2) error on the line
coeff = x; in the definition my setCoeff function.
I tried commenting out that specific function call, but it gives me the same error for the setX() and setY() functions.
I think I have my files and functions set up properly, I cannot figure out why it is not letting me use these functions.
Please help !
In order, I have included: Polynomial.h, Polynomial.cpp, and main.cpp.
#ifndef __Polynomial__Polynomial__
#define __Polynomial__Polynomial__
#include <stdio.h>
class Term {
private:
int coeff;
int deg_x;
int deg_y;
public:
Term();
int getCoeff();
int getX();
int getY();
void setX(int);
void setY(int);
void setCoeff(int);
};
#endif /* defined(__Polynomial__Polynomial__) */
___________________________
#include "Polynomial.h"
Term::Term() {
coeff = NULL;
deg_x = NULL;
deg_y = NULL;
}
int Term::getCoeff(){
return coeff;
}
int Term::getX() {
return deg_x;
}
int Term::getY() {
return deg_y;
}
void Term::setX(int x){
deg_x = x;
}
void Term::setY(int x){
deg_y = x;
}
void Term::setCoeff(int x){
coeff = x;
}
__________________________
#include <iostream>
#include <fstream>
#include "Polynomial.h"
int main() {
Term* t1;
t1->setCoeff(4);
t1->setX(3);
t1->setY(6);
}
You never create an object. You have Term* t1, which is an uninitialized pointer to random memory, then you try to use it with t1->setCoeff(4) which is trying to use an object that was never created. That's definitely gonna go wrong.
Do this instead..
auto t1 = std::make_unique<Term>();
Or if you don't need it to be a pointer, you can create a simple stack variable and access it with '.' operator like this ...
Term t1;
t1.setCoeff(4);
t1.setX(3);
t1.setY(6);

c++ floating pointer to the power of int pointer

I have a method that takes in a float pointer and an int pointer, it with then do pow(the float, the int) and return the value. I get a huge error of which I can't seem to understand what it is telling me is wrong.
#include <cmath>
#include <iostream>
using namespace std;
float method3(float *f, int *i); //initialize pointer method
float flt; //init variable
int nt; //init variable
int main() {
method3(*flt, *nt); //run method 3, which will do the same math, but with pointers instead of value or reference
cout << flt; //print it out
return 0;
}
float method3(float *f, int *i) { //method 3, get float and int by pointers
return pow(f, i); //f to power of i back to original flt variable
}
please let me know what I'm doing wrong?
You are dereferencing pointers improperly.
You should call
pow(*f,*i);
and
method3(&flt,&nt);
method3 returns void, so you can't write return pow(f, i); inside it.