Hello,
i have the following code, which has an object composition relationship between the class Group and Person:
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "group.h"
ui(new Ui::Dialog)
{
ui->setupUi(this);
Group g1;
g1.setGroupId(1);
// this function should print the groupID
g1.p1.printGroupId();
}
Dialog::~Dialog()
{
delete ui;
}
#include "dialog.h"
#include "ui_dialog.h"
#include "group.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
Group g1;
g1.setGroupId(1);
// this function should print the groupID
g1.p1.printGroupId();
}
Dialog::~Dialog()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
group.h
#include "person.h"
class Group
{
public:
Group();
int getGroupId() const;
void setGroupId(int value);
Person p1;
private:
int groupId;
};
#include "person.h"
class Group
{
public:
Group();
int getGroupId() const;
void setGroupId(int value);
Person p1;
private:
int groupId;
};
To copy to clipboard, switch view to plain text mode
group.cpp
#include "group.h"
Group::Group()
{
}
int Group::getGroupId() const
{
return groupId;
}
void Group::setGroupId(int value)
{
groupId = value;
}
#include "group.h"
Group::Group()
{
}
int Group::getGroupId() const
{
return groupId;
}
void Group::setGroupId(int value)
{
groupId = value;
}
To copy to clipboard, switch view to plain text mode
person.h
#include "group.h"
class Person
{
public:
Person();
void printGroupId();
};
#include "group.h"
class Person
{
public:
Person();
void printGroupId();
};
To copy to clipboard, switch view to plain text mode
person.cpp
#include "person.h"
Person::Person()
{
}
void Person::printGroupId()
{
//How it is possible to access the member variable groupID of the Group object to which this Person objects belongs ?
qDebug()<<"The GroupID is:";
}
#include "person.h"
Person::Person()
{
}
void Person::printGroupId()
{
//How it is possible to access the member variable groupID of the Group object to which this Person objects belongs ?
qDebug()<<"The GroupID is:";
}
To copy to clipboard, switch view to plain text mode
Thx
Bookmarks