I am new to c++ and this one has me stumped. I want to pass a struct to a class (I know they are technically the same) so the class can access the data in the struct. I don't mind if it is passed as a reference or a copy as there will be no changes to the struct within the class. Having said that a reference would probably be better for performance. I can get it all to work fine if I pass the members of the struct individually but the real version will have about 30 members so passing them individually isn't the best option.
My main cpp:
#include "stdafx.h"
#include "myClass.h"
#include <iostream>
using namespace std;
struct foo
{
int num;
double dbl;
};
int _tmain(int argc, _TCHAR* argv[])
{
foo bar;
bar.dbl=3.14;
bar.num=42;
baz qux(); //bar needs to be passed here
cout<<qux.getSum()<<endl;
return 0;
}
Class header:
using namespace std;
class baz
{
public:
baz(); //This is where type of bar (foo) is declared
void setSum(int, double);
double getSum();
private:
double sum;
};
Class cpp:
#include "stdafx.h"
#include "myClass.h"
#include <iostream>
using namespace std;
baz::baz() //this is where bar is called
{
setSum(bar.num, bar.dbl);
}
void baz::setSum(int num, double dbl)
{
sum=num*dbl;
}
double baz::getSum()
{
return sum;
}
So the nub of the question is, how do I get bar into baz?