I'm not sure what I'm doing...
Let say I have a structure
struct Inner{
exampleType a;
int b;
}
struct Outer{
int current;
int total;
Inner records[MAXNUMBER];
}
struct Outer2{
Outer outer;
}
And I have the following functions:
void try3( Outer2& outer, type var, type2 var2 ){
}
void try2( Outer2* outer ){
try3(*outer, var, var2);
}
Inside main:
int myMain( int argc, char *argv[] ){
Outer2 outer2;
try2 (&outer2);
}
Here's the question. Can I increment the value of current by sticking the following line in try3:
++outer.outer.current;
errr, no, try3 has no knowledge of a thing caller outer2.
you can go outer.outer.current++; in try3
Sure you can, why not? Here is a working example for you:
typedef short exampleType;
struct Inner
{
exampleType a;
int b;
};
struct Outer
{
enum { MAXNUMBER = 2 };
int current;
int total;
Inner records[MAXNUMBER];
};
struct Outer2
{
Outer outer;
};
typedef int type;
typedef int type2;
void try3 (Outer2& outer, type var, type2 var2)
{
++outer.outer.current;
}
void try2 (Outer2* outer)
{
int var = 1, var2 = 2;
++outer->outer.current;
try3(*outer, var, var2);
}
int main ()
{
Outer2 outer2;
outer2.outer.current = 1986;
try2 (&outer2);
}
From try3, you would need to use ++outer.outer.current since outer is the name of the variable at the point. To answer the actual question though, look at the two functions. try2 takes a pointer to an Outer2, so no copy is made. try2 passes this to try3 which take a reference to an Outer2, so again no copy is made. This means that yes, ++outer.outer.current will in fact affect the original outer2 declared in myMain.
Edit (per Brian's comment)
However, outer.outer.current is never initialized, unless that's not your true myMain, so the final value of current is undefined since the value was never defined before the increment.
Related
I have a chicken and the egg problem. I want to pass a data struct to a routine which contains a pointer to a routine who needs the struct.
I made a very simple example.
I need to use the CalcDataStruct before it's defined, if I add it after the struct, the FuntctionPrototype is not defined.
The problem i run against is only in the first 2 lines, the rest may contain a few syntax errors because I have not checked this inside the compiler.
typedef void(*FunctionPrototype)(CalcDataStruct *Ptr);
struct CalcDataStruct
{
int A, B, C, D;
int Values;
char SignA, Sign B;
int Result;
FunctionPrototype Routine;
}
struct ScanStruct
{
char Sign;
int Values;
FunctionPrototype Routine;
};
const ScanStruct ExampleList[] =
{
{ '+', 2, AddTwo },
{ '+', 3, AddThree }
};
void AddTwo(CalcDataStruct *Ptr)
{
// use the data and if needed put it back
}
void AddThree(CalcDataStruct *Ptr)
{
// use the data and if needed put it back
}
void GetFunction(CalcDataStuct *Ptr, ScanStruct *List)
{
// Very simple return based on nothing
Ptr->Routine = *List[(1)].Routine;
}
void main()
{
CalcDataStruct A;
// struct is filled
// Fill in the routine pointer based on data
GetFunction(A, ExampleList)
// Execute the routine fetched with all the data
A->Routine(A)
}
You need to add a forward declaration (see below).
struct CalcDataStruct; will just declare that struct CalcDataStruct exists, so the typedef void(*FunctionPrototype)(CalcDataStruct *Ptr); declaration will succeed because now the compiler knows that struct CalcDataStruct exists but without knowing the details of the struct, which doesn't matter because all the FunctionPrototype declaration needs is to know that the parameter is a pointer to struct CalcDataStruct.
struct CalcDataStruct; // <<< add this
typedef void(*FunctionPrototype)(CalcDataStruct *Ptr);
struct CalcDataStruct
{
int A, B, C, D;
int Values;
char SignA, Sign B;
int Result;
FunctionPrototype Routine;
}
I've read about solution const A a[3] = { {0,0}, {1,1}, {2,2} }, but in my program const can't be used:
class Paper: public PaperQueue{
...
protected:
typedef int (Utils::*funcPtr) (int, int); //I use external function there
funcPtr p;
Utils* fptr;
public:
int pricefunc(){
addprice = (fptr->*p) (t,price);
}
Paper(int n, unsigned int pr):PaperQueue(n){
...
p=&Utils::commonpricefunc;
}
void Put(int a){
...
}
...
}
class Bank{
...
void Buy(Paper &p){
(/*this function modifies many parameters in 'p'*/)
...
}
...
}
int main(){
Bank B;
int pn=5;
/* ? */ const Paper p[pn] = {{5,15},{5,15},{5,15},{5,15},{5,15}}; /* ? */
int paperloop=0;
...
p[paperloop].Put(p[paperloop].addprice);
B.Buy(p[paperloop]);
...
That gives me a LOT of errors(with pricefunc(),Put(),Buy(),...), or just "variable-sized object āpā may not be initialized". Is there any way to make this array work? (Everything works fine if not to pass any parameters to constructor!)
You can't use initializer lists for classes (non-PODs) because that would bypass the call to the constructor. You'll have to change it to a POD or use std::vector in the following ways:
If the class is copyable, as it appears to be, you can create a std::vector and fill it with the values you want:
const vector<Paper> papers(5, Paper(5, 15));
If you want to initialize it with different values, you can use an initializer list, but this is only supported in C++11:
const vector<Paper> papers = {Paper(1, 1), Paper(2, 2)};
Without C++11, you'll have to add the elements one by one, but then you can't make the vector const:
vector<Paper> papers;
papers.push_back(Paper(1, 1));
papers.push_back(Paper(2, 2));
Please check the code below, it can be compiled:
class Paper {
public:
int x, y;
};
int main() {
Paper p[5] = {{5,15}, {5,15}, {5,15}, {5,15}, {5,15}};
}
Please refer this post for more details, I think it explains very well
This is probably a really basic error I'm making, but I'm quite new to c++ so please don't judge!
Basically, I've got two classes as follows:
class A{
private:
vector< vector<int> > images;
public:
int f1(int X, int Y);
}
class B{
private:
int x;
int y;
public:
int f2(A var);
}
I want to be able to call B.f2(A) with defined variables A and B and have f2() call A.f1(x,y). So far, all this works.
But the function f1 assigns values to the vector 'images' which aren't there when f2() returns. Any ideas why?
Here's the code:
int A::f1(int X, int Y){
// Some stuff to resize images accordingly
images[X][Y] = 4;
return 0;
}
int B::f2(A var){
var.f1(x, y);
return 0;
}
int main(){
A var1;
B var2;
// Stuff to set var2.x, var2.y
var2.f2(var1);
// HERE: var1.images IS UNCHANGED?
}
this is because you have passed A by value. instead, pass it by reference.
void fn(A& p);
^ << refer to the original object passed as the parameter.
as it is now, your program creates, and then mutates a copy of var1.
when you do not want to mutate the parameter, you can pass it as a const reference:
void fn(const A& p);
^^^^^ ^
So, it's possible to declalre anonymous class or struct but how to I make it useful?
int main() {
class{
int ClassVal;
};
struct{
short StructVal;
};
StructVal = 5; //StructVal is undefined
ClassVal = 5; //ClassVal is undefined too?
return 0;
}
if you put both of them outside of main function they will be inaccessible as well.
I'm asking this only because it's somehow intersting :)
EDIT:
Why union outside of main function (at global scope) must be static declared
for example:
static struct {
int x;
};
int main() {
//...
}
Anonymous classes and structures may be used to directly define a variable:
int main()
{
class
{
int ClassVal;
} classVar;
struct
{
short StructVal;
} structVar;
structVar.StructVal = 5;
classVar.ClassVal = 5;
return 0;
}
The above is not very common like that, but very common when used in unions as described by Simon Richter in his answer.
These are most useful in the form of nested struct and union:
struct typed_data {
enum type t;
union {
int i; // simple value
struct {
union { // any of these need a length as well
char *cp;
unsigned char *ucp;
wchar_t *wcp;
};
unsigned int len;
};
};
};
Now typed_data is declared as a type with the members t, i, cp, ucp, wcp and len, with minimal storage requirements for its intended use.
I want to use a nested structure, but I don't know how to enter data in it. For example:
struct A {
int data;
struct B;
};
struct B {
int number;
};
So in main() when I come to use it:
int main() {
A stage;
stage.B.number;
}
Is that right? If not how do I use it?
Each member variable of a struct generally has a name and a type. In your code, the first member of A has type int and name data. The second member only has a type. You need to give it a name. Let's say b:
struct A {
int data;
B b;
};
To do that, the compiler needs to already know what B is, so declare that struct before you declare A.
To access a nested member, refer to each member along the path by name, separated by .:
A stage;
stage.b.number = 5;
struct A {
struct B {
int number;
};
B b;
int data;
};
int main() {
A a;
a.b.number;
a.data;
}
struct B { // <-- declare before
int number;
};
struct A {
int data;
B b; // <--- declare data member of `B`
};
Now you can use it as,
stage.b.number;
The struct B within A must have a name of some sort so you can reference it:
struct B {
int number;
};
struct A {
int data;
struct B myB;
};
:
struct A myA;
myA.myB.number = 42;
struct A
{
int data;
struct B
{
int number;
}b;
};
int main()
{
A stage = { 42, {100} };
assert(stage.data == 42);
assert(stage.b.number == 100);
}
struct TestStruct {
short Var1;
float Var2;
char Var3;
struct TestStruct2 {
char myType;
CString myTitle;
TestStruct2(char b1,CString b2):myType(b1), myTitle(b2){}
};
std::vector<TestStruct2> testStruct2;
TestStruct(short a1,float a2,char a3): Var1(a1), Var2(a2), Var3(a3) {
testStruct2.push_back(TestStruct2(0,"Test Title"));
testStruct2.push_back(TestStruct2(4,"Test2 Title"));
}
};
std::vector<TestStruct> testStruct;
//push smthng to vec later and call
testStruct.push_back(TestStruct(10,55.5,100));
TRACE("myTest:%s\n",testStruct[0].testStruct2[1].myTitle);
I have somewhat like the following code running for a while live now and it works.
// define a timer
struct lightTimer {
unsigned long time; // time in seconds since midnight so range is 0-86400
byte percentage; // in percentage so range is 0-100
};
// define a list of timers
struct lightTable {
lightTimer timer[50];
int otherVar;
};
// and make 5 instances
struct lightTable channel[5]; //all channels are now memory allocated
#zx485: EDIT: Edited/cleaned the code. Excuse for the raw dump.
Explanation:
Define a lightTimer. Basically a struct that contains 2 vars.
struct lightTimer {
Define a lightTable. First element is a lightTimer.
struct lightTable {
Make an actual (named) instance:
struct lightTable channel[5];
We now have 5 channels with 50 timers.
Access like:
channel[5].timer[10].time = 86400;
channel[5].timer[10].percentage = 50;
channel[2].otherVar = 50000;