struct: persists in memory if changed in main not function

IN the code snippet below:

Change add_change_total(Change change)
.
.
.
change.nickels=+2000;

The iteration in nickels is only "remembered" in the function. When I access change.nickels elsewhere its back to its original pre-2000 value. Reading on structs mentioned that the beauty of structs was one of only a few ways for a function to return more than one var. What can I do to change it in the function block to carry on outside it as well.--> Note if I do change the nickel value in the int main()... it "sticks through out the program.!

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>

using namespace std;

struct Change
{

	int nickels;
	int dimes;
	int quarters;
	int pennies;
	int total;

};

Change list_change(Change change)
{
		cout<<"list change-----------------------"<<endl;
		cout<<change.nickels<<endl; //check contents
	    cout<<change.quarters<<endl;
	    cout<<change.pennies<<endl;
	    cout<<change.dimes<<endl;
	    cout<<change.total<<endl;
	    return change;
};

Change add_change_total(Change change)
{
	cout<<"add change total-----------------------"<<endl;
	change.nickels=+2000;
    change.total = change.nickels + change.quarters + change.pennies + change.dimes;
    cout<<change.total<<endl;
    return change;
}

int main()
{
	cout<<"main-----------------------"<<endl;
	Change change = {5,10,25,1};
	Change joe = {10,45,75,1};
	add_change_total(change);
	add_change_total(joe);
	list_change(change);
	list_change(joe);

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
// ...

Change change = {5,10,25,1,0}; // {5,10,25,1};
Change joe = {10,45,75,1,0}; // {10,45,75,1};

// add_change_total(change);
change = add_change_total(change);

// add_change_total(joe);
joe = add_change_total(joe);

// ... 
You may not have discovered yet that structs can have member functions (like classes).
A member function can refer to member variables directly.

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
#include<iostream>

using namespace std;

struct Change
{   int nickels;
	int dimes;
	int quarters;
	int pennies;
	int total;
	
	void list_change () const;
	void add_change_total ();
};

void Change::list_change () const
{   cout<<"list change-----------------------"<<endl;
	cout<<nickels<<endl; //check contents
	cout<<quarters<<endl;
	cout<<pennies<<endl;
	cout<<dimes<<endl;
	cout<<total<<endl;
};

void Change::add_change_total ()
{   cout<<"add change total-----------------------"<<endl;
	nickels=+2000;
    total = nickels + quarters + pennies + dimes;
    cout<<total<<endl;
}

int main()
{   cout<<"main-----------------------"<<endl;
	Change change = {5,10,25,1};
	Change joe = {10,45,75,1};
	
	change.add_change_total();
	joe.add_change_total();
	change.list_change();
	joe.list_change();
	return 0;
}

by default . If you use the external C linkage , it wont compile.
Ericool wrote:
by default . If you use the external C linkage , it wont compile.
Did you respond to the wrong thread?
looks like it..
C language linkage is ignored in determining the language linkage of the names of class members and the function type of class member functions. - IS


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

extern "C"
{
    struct A
    {
        void say_hello( const std::string& name = "" ) const { std::cout << "Hello " + name + "!\n" ; }
    };
}

extern "C" void foo( const A& a, std::string&& n ) { a.say_hello( std::move(n) ) ; }

extern "C" void bar() { A{}.say_hello( "Ericool" ) ; }

int main()
{
    bar() ;
}

http://coliru.stacked-crooked.com/a/6559c9c165939ed2
Last edited on
Not like this :

1
2
3
4
5
6
7
8
9
10
11
#ifdef __cplusplus
extern "C"
{
#endif
	struct A
	{
		void say_hello(const char* name = "")const;
	};
#ifdef __cplusplus
};
#endif 
I wanted first address JL's fix for me:
1
2
3
4
5
6
7
add_change_total(change);
...
change = add_change_total(change);

// add_change_total(joe);
joe = add_change_total(joe);
 ... 


The fix worked and didn't as well.

1
2
3
4
5
6
7
8
add_change_total(change);
...
change = add_change_total(change);

// add_change_total(joe);
joe = add_change_total(joe);
joe = add_change_total(joe);
 ... 


calling joe 2x makes no difference
Last edited on
calling joe 2x makes no difference

Probably because the implementation of add_change_total was incorrect. =+ is not +=.

1
2
3
4
5
6
Change add_change_total(Change change)
{
    cout<<"add change total-----------------------"<<endl;
    change.nickels=+2000;
    change.nickels += 2000;
    // ... 
cire, thank you! fixed that

thx too JL
Last edited on
AbstractonANON

looks pretty close to classes to me -- I'll study it, thx for sample.

generally why srtuct rather than classes, vice versa? struct seems pretty agile

Experimented w/ it a bit
using structs with member fx's reduces the number of struct defs and organizes them better w/ increased inter-relatedness & readability
Last edited on
generally why srtuct rather than classes, vice versa? struct seems pretty agile

Yes, structs and classes are nearly identical. The only difference is the default visibility of members. With structs, members are by default public. With classes, the default is private.

I like to use structs with dealing with POD (Plain Old Data) and classes when dealing with OO.
That's a somewhat arbitrary distinction though, as you can represent POD with a class and OO with a struct.

In C++, struct and class are synonyms. The only difference is the default privacy (structs default to public, classes default to private). Other than that, they're identical.


this doesn't work
 
lisa::add_change_total()


this does
1
2
3

lisa.add_change_total()

is there any way to use the scope operator:: ?

want to make sure I've covered everything
is there any way to use the scope operator:: ?

If you want to refer to a specific instance (lisa), then no.
That's what the dot operator is for.
Topic archived. No new replies allowed.