Less complicated way of changing variables of class instance from another class

Take a look at my below example. I want class B to be able to change the variable "x" in an object of class A. Sure I can pass in the object into the constructor like I have done, then create yet another object of A inside class B, modify that object and then finally copy the attributes like I have done...but that's a lot of work. There must be a simpler way of doing the same thing.
One idea I have is to simply have the constructor act as sort of a main-like function where all the function calls for the class are executed inside the class' constructor. That way, I could pass by reference the class A object and avoid having to create multiple other instances of class A inside class B, but I'm not sure if that's even allowed. If it is, I'm sure it's bad practice.
So yeah, I'm open to all ideas. What are my options?

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include "Header.h"
#include "B.h"
int main()
{
	A classA;
	classA.x = 5;
	B classB(classA);
	classB.changeX();
	classA.x = classB.newClassAObject.x;
    return 0;
}

Header.h
1
2
3
4
5
6
7
8
9
#pragma once
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <string>
class A {
public:
	int x;
};
#endif 

B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include "B.h"


B::B(A classA)
{
	newClassAObject = classA;
}
void B::changeX()
{
	newClassAObject.x = 10;
}

B.h
1
2
3
4
5
6
7
8
9
#pragma once
#include "Header.h"
class B
{
public:
	B(A);
	void changeX();
	A newClassAObject;
};

Last edited on
Separate from the narrow scope of the question, B::changeX() should not be a class member.

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

struct A {
  int x = 0;  
};

struct B { 
  void change_x(A& a) { ++a.x; }
};

int main() 
{ 
  A a; B b;
  std::cout << a.x << '\n'; 
  b.change_x(a); 
  std::cout << a.x << '\n';
}

http://coliru.stacked-crooked.com/a/cd7a502f8ba626d8
Last edited on
I don't understand why it shouldn't be a class member but I tried passing the class by reference like that but class A still won't reflect the changes.
New B.h
1
2
3
4
5
6
7
class B
{
public:
	B(A&);
	void changeX();
	A newClassAObject;
};

new B.cpp
1
2
3
4
5
6
7
8
B::B(A& classA)
{
	newClassAObject = classA;
}
void B::changeX()
{
	newClassAObject.x = 10;
}


I tried passing the class by reference


Good job. You're passing the class by reference, but you're still making a whole new copy inside B. You're passing a reference to an object, and then B is copying that object.

Instead of having B contain an object of type A, make it hold a reference to an object of type A:


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


struct A {
  int x = 0;  
};

class B
{
public:
	B(A&);
	void changeX();
	A& newClassAObject;
};

B::B(A& classA) : newClassAObject(classA)
{
}
void B::changeX()
{
	newClassAObject.x = 10;
}


int main() 
{ 
  A a;
  B b(a);
  std::cout << a.x << '\n'; 
  b.changeX(); 
  std::cout << a.x << '\n';
}


This, of course, comes with dangers. If the A object vanishes, B is now holding a reference to an object that doesn't exist anymore.
I followed what you did but instead of doing "B::B(A& classA) : newClassAObject(classA), I kept it as:
1
2
3
4
B::B(A& classA)
{
	newClassAObject = classA;
}

Two questions:
1. I've never really understood the meaning of the ":" after a class definition. How exactly does it work?
2. I receive a "no initializer" error on the left bracket for newClassAObject, but I don't know why this would be. It looks initialized to me.
1. http://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/

2. newClassAObject is a reference to some other object. What did you make it refer to? What other object is it referring to? None. You never initialised it. You never made it refer to any other object.

Thank you for the link! I thought newClassAObject would simply refer to the instance of class A that was passed into the constructor. I'm confused why it wouldn't since a reference to it is held as classA and then the same reference is copied to newClassAObject. Shouldn't that make it so newClassAObject also points back at the instance of class A?
One other thing. Even though my constructor clearly asks for "A& classA", when I'm actually creating the object, the compiler insists that I should pass it in without the "&". For example, when I do "B newClassB(&AObject)" where A is an instantiation of class A, it insists that I should instead provide it with "AObject".
Topic archived. No new replies allowed.