0

This is my first question on this forum, and i expect this will not be a dummy one.

I searched the answer everywhere but can't find it. Feel free to redirect me if the question is already answered somewhere.

How can i set a component of an object, into a bigger object ?

EDIT :

I will make a proper example if it's unclear,

BigObject.h

#include "Object.h"
Class BigObject {
private : 
 Object object
 int number
public 
BigObject(Object,int);
Object get_object();
void set_object(Object);
int get_number()
void set_number(int);
}

BigObject.cpp

#include "BigObject.cpp" 
BigObject::BigObject(Object a,int b){
set_Object(a); 
set_number(b);
};
Object BigObject::get_object(){ return this->object; };
void BigObject::set_object(Object a){ this->object = a};
int BigObject::get_number(){return this->number};
void BigObject::set_number(int b){this->number = b};

Object.h

#include <string>
Class Object {
    private : 
     int bidule;
     std::string machin;
    public :
    Object(int,std::string);
    int get_int();
    void set_int(int);
    std::string get_string()
    void set_string(std::string);
    }

Object.cpp

#include "Object.h"
using namespace std;
Object::Object(int a,string b){
set_int(a);
set_string(b);
};
int Object::get_int(){return this->bidule};
void Object::set_int(int a){this->bidule = a};
string Object::get_string(){return this->machin};
void Object::set_string(string a){ this->machin = a};

question :

This does not work if i do :

BigObject.getObject().set_string("Foo");

It is not saved

cout << BigObject.getObject().get_string() << endl;

Doesn"t give foo. Why ? How can i save it in my Big Object ?

1 Answers1

0

The problem is that BigObject.getObject() returns an instance, which creates a copy of the object. So when you call setComponent you are actually setting it onto a copy of the object, which is why when you attempt to print it out, you don't see what you're expecting. You need to read up on references and pointers (preferably the smart type).

You could change the definition of getObject to return a reference easily enough:

class BigObject
{
public:
    Object& getObject();
    const Object& getObject() const;
    void setObject(const Object &other);
};
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • I was sure it had something to do with that but i can't manage to find the answer.. thanks Senpaï :D – CreatorOfMoon Feb 06 '19 at 15:26
  • Also can you tell me the name of what you're doing, or a reference where i can find it ? i can't manage to find what should i type in the cpp file.. – CreatorOfMoon Feb 06 '19 at 15:39
  • @CreatorOfMoon if you Google for 'C++ references' you'll find plenty of stuff about it. Technical information is available here - https://en.cppreference.com/w/cpp/language/reference That's assuming you are interested in references, and not smart pointers (normally you'll use a mix of both). – Mark Ingram Feb 06 '19 at 15:43
  • Thank you a lot, it's amazing, i learned C++ in school but i never heard of that O_o. Have a nice day – CreatorOfMoon Feb 06 '19 at 15:45
  • @CreatorOfMoon "_i learned C++ in school but i never heard of that_" Then, I doubt that you learned C++. Sure, the course might have been _called_ C++, but you might have been actually taught "C with classes". – Algirdas Preidžius Feb 06 '19 at 17:15
  • @AlgirdasPreidžius Ahah, you're probably right ! – CreatorOfMoon Feb 07 '19 at 08:40