passing a struct to a class

i want to write a generic class that i can configure by passing an "initialize" function that is in the class a struct by reference. the reason i want to do it by reference is that it would allow me to modify the settings on the fly without having to re run the "initialize"

i've passed functions structs before for example

1
2
3
4
5
6
7
8
9
10
11
12
  struct myStruct {
int one;
int two;
};

struct myStruct example_struct;

void myFunction (struct myStruct * _pass) {
_pass->one = 3;
}

myFunction (&example_struct);


but how would it work inside a class?

does there only need to be a decloration in the same scope for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  struct myStruct {
int one;
int two;
};

struct myStruct example_struct;

class myClass {
private:
struct myStruct * save_data;

public:
void myFunction (struct myStruct * _pass);

};

void myClass::myFunction (struct myStruct * _pass) {
save_data = _pass;
}

class myClass testClass;

testClass.myFunction (&example_struct);


or does the struct have to be declared inside the class as well

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  struct myStruct {
int one;
int two;
};

struct myStruct example_struct;

class myClass {
private:

  struct myStruct {
int one;
int two;
};

struct myStruct * save_data;

public:
void myFunction (struct myStruct * _pass);

};

void myClass::myFunction (struct myStruct * _pass) {
save_data = _pass;
}

class myClass testClass;

testClass.myFunction (&example_struct);


or do i just misunderstand the whole thing?
C++ is not C with Classes, despite that being its original name. If you're going to use C++ features, go all in and ditch C.

In modern C++ we rarely use pointers (we rarely need to) and we try our best to avoid them.
Do you mean something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>

class MyStruct
{
public:
	int one;
	int two;

	MyStruct() : one(1), two(2)
	{}
};

class MyClass
{
public:
	MyClass::MyClass(MyStruct& myStruct)
	{
		m_myStruct = myStruct;
	}

	void printOne()
	{
		std::cout << "value: " << m_myStruct.one << std::endl;
	}

private:
	MyStruct m_myStruct;
};


int main()
{

	MyStruct myStructObject;


	// do stuff with myStructObject
	// e.g. :
	myStructObject.one = 42;

	// then pass into your 'generic class'
	MyClass myClassObject(myStructObject);

	myClassObject.printOne();

	return 0;
}

?

value: 42
Last edited on
even though that sounds great in theory this project is going into an embedded micro-controller which has limited resources. further pointers are already used to work with the hardware so i would still have to pass pointers to the hardware control registers.
yes that looks like what i'm trying to do thanks
how does my example use more resources than creating pointers?
sorry, that reply was to "LB"
Topic archived. No new replies allowed.