This question already has answers here:
How do I pass a reference to a two-dimensional array to a function?
(5 answers)
Reference to a Two-Dimesional Array
(2 answers)
Closed 8 years ago.
I have a problem. I dont know how to pass 2D array of pointers to fuction by reference.
class SomeClass
{
//body of class
}
void somefunction(SomeClass ***array)
{
//body of function
}
int main()
{
SomeClass * array[10][10]
someFunction(array?????)
}
Anyone know how to pass this array by reference??
Literally
void somefunction(SomeClass *(&array)[10][10])
{
}
int main()
{
SomeClass *array[10][10];
someFunction(array);
}
Related
This question already has answers here:
How do I initialize a variable size array in a C++ class?
(5 answers)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 11 months ago.
I'm pretty new to coding, could anyone please help me initialize a user define array in class stack. i have referred material online but don't know what needs to be done still...
this is a program to implement stack using array.
#include <iostream>
using namespace std;
class stack
{
public:
int stack[len], len, top=-1; // var len will store user defined value for array size
bool isEmpty()
{
if(top<=-1)
return 1;
else
return 0;
}
bool isFull()
{
if(top>=len-1)
return 1;
else
return 0;
}
// other push pop stackTop and display functions;
};
int main()
{
int ch, val;
stack s1;
cout<<"Enter the stack length: ";
cin>>len;
// using do while for menu-driven prg to seek the stack operations
return 0;
}
This question already has answers here:
How to refer to a global variable which has the same name as a local variable in C++?
(3 answers)
What is the meaning of prepended double colon "::"?
(9 answers)
Closed 1 year ago.
#include <stdio.h>
void val();
int v=10;
class test{
public:
int v=11;
void val()
{
printv();
}
private:
void printv()
{
int v=12;
printf("V: %d",this->v);
}
};
int main()
{
test obj;
obj.val();
return 0;
}
I am getting 11 as output but I need to access the global variable, How can I get it ??
Similarly are there any ways to access global variables with the same name as local variables in C & Python ??
This question already has answers here:
How to call a function by its name (std::string) in C++?
(4 answers)
Closed 1 year ago.
I am very kindergartener to C++. Hope someone can help me out with my problem.
Assume there is a function defined in one class.
void __foo__(int x, int y){
//do something
}
In another class, there is a char pointer that holds the value of the function name.
static const char *func = "__foo__";
How do I call the function by using the "func" like func(0, 0)?
C++ doesn't have reflection, so you have to write the code to call the function based on the name yourself
void fall_function_based_on_name(const char* func_name, classWithMethod* self, lint x, int y) {
if (strcmp(func_name, "__foo__")==0)
self->__foo__(x, y);
else
throw std::logic_error("method name not found");
}
This question already has answers here:
Returning a reference to a local variable in C++
(3 answers)
Closed 6 years ago.
Consider the following piece of program:
class cls
{
int vi;
public:
cls(int v=37)
{
vi=v;
}
friend int& f(cls);
};
int& f(cls c)
{
return c.vi;
}
int main()
{
const cls d(15);
f(d)=8;
cout<<f(d);
return 0;
}
When I run it, the output is
15
but I don't understand why 15, because I thought it should've outputed 8, because of the
f(d)=8
function, which from what I understand makes the c.vi=8, but I might be wrong and the function probably does something else entirely, so then I ask, what is the purpose or what does the
friend int& f(cls);
function do?
Your program has Undefined Behavior - you are returning a dangling reference to local variable of a function (argument is a local variable as well).
This question already has answers here:
Object array initialization without default constructor
(14 answers)
Closed 8 years ago.
I want to allocate an array of C++ objects using the following code:
class myClass {
public:
myClass(int userValue)
: value(userValue)
{ }
}
private:
int value;
};
int main(){
myClass* objArray = new myClass(22)[5];
return 0;
}
But it gives me the following error:
In constructor ‘myClass::myClass(int32)’:
error: expected ‘;’ before ‘[’ token
objArray = new objArray(22)[5];
How should I create an array of objects then while passing parameters to them?
Use std::vector.
std::vector<myClass> objArray(5, 22);