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
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';
}
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;
};
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:
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.
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".