How can an object access a variable belonging the class containing it?
Right now I have a class called system that contains some other objects, and those objects need to access and modify one of the variables in the System class.
Example:
Class System {
BlockA _blockA = new BlockA();
BlockB _blockB = new BlockB();
BlockC _blockC = new BlockC();
BlockD _blockD = new BlockD();
int myVariable;
...stuff...
}
Class BlockA {
...stuff...
void someFunction () {
System.myVariable++;
}
...stuff...
}
etc...
Alright so I thought about this some more and realized that when initializing the objects, I will pass a pointer to the variable of interest. That way all objects can read that variable. For anyone else with this problem, if you need to write, you'll have to make sure that the variable is atomic.
Hard to know exactly what you're after, but appears something along these lines:
#BlockA.h
#ifndef BLOCKA_H
#define BLOCKA_H
class System;
class BlockA {
System* sys;
public:
BlockA(System* sys) : sys(sys) {}
void SomeFunction();
};
#endif // BLOCKA_H
#BlockA.cpp
#include "System.h"
void BlockA::SomeFunction() {
sys->setMyVariable(sys->getMyVariable() + 1);
}
#System.h
#ifndef SYSTEM_H
#define SYSTEM_H
class BlockA;
class System {
BlockA* _blockA;
int myVariable;
public:
System();
int getMyVariable() const;
void setMyVariable(int value);
BlockA& getBlockA() const;
};
#endif // SYSTEM_H
#System.cpp
#include "System.h"
#include "BlockA.h"
System::System()
: _blockA(new BlockA(this)) { }
int System::getMyVariable() const {
return myVariable;
}
void System::setMyVariable(int value) {
myVariable = value;
}
BlockA& System::getBlockA() const {
return *_blockA;
}
Related
There are 1 main class and 3 classes: Main, MLME, MAC and Network.
I would want an MLME object to be created within the MAC object upon calling the constructor of the MAC. Then share the same object to the Network object when calling the Network constructor.
This without making the MLME object global or allocating memory with malloc() or new.
I believe this should be done with references which I don't understand fully. Also there might have to be some initializer-list in Network.cpp? I am more experienced with C than C++ and have tried a lot in order to understand all of this.
This is some of my thoughts of the structure, header and cpp files:
Main.cpp
#include "MAC.h"
#include "Network.h"
int main() {
MAC mac();
Network network(mac);
return 0;
}
Here is the MLME part to be shared:
MLME.h
#ifndef MLME_H_INCLUDED
#define MLME_H_INCLUDED
class MLME {
public:
MLME();
};
#endif
MLME.cpp
#include "MLME.h"
MLME::MLME() {}
The MAC class:
MAC.h
#ifndef MAC_H_INCLUDED
#define MAC_H_INCLUDED
#include "MLME.h"
class MAC {
private:
MLME mlme; // NULLED ?
public:
MAC();
MLME getMLME();
};
#endif
MAC.cpp
#include "MAC.h"
MAC::MAC() {
mlme = MLME:MLME();
}
MLME MAC::getMLME() {
return mlme;
}
The Network class:
Network.h
#ifndef NETWORK_H_INCLUDED
#define NETWORK_H_INCLUDED
#include "MLME.h"
class Network {
private:
MLME& mlme;
public:
Network(MAC mac);
};
#endif
Network.cpp
#include "Network.h"
class MAC;
Network::Network(MAC mac) {
mlme = mac.getMLME();
}
You're close, but:
The Network::Network constructor should take MAC by reference, like so: Network::Network(MAC& mac). Currently you take a copy, which means taking a copy of MLME as well.
In addition, the Network::Network constructor should use an initializer list to initialize mlme. So the full form would be:
Network::Network(MAC& mac) : mlme(mac.getMLME()) {}
MAC::getMLME() should return a reference to MLME: MLME& MAC::getMLME(). Otherwise you return a copy.
The explicit construction of MLME in the MAC constructor is not needed: it is already default-constructed.
You may want to prevent copying of MLME instances by saying MLME(const MLME&) = delete. If it is really a shared resource, you want any changes to go back to the shared instance. Making it impossible to copy MLME instances will prevent you from accidentally making or modifying a copy.
In your main function: MAC mac() does not do what you think it does. Remove the () or you get a "most vexing parse" error.
Main.cpp
int main() {
MAC mac;
Network network(mac);
mac.set(10);
mac.print();
network.print();
mac.set(11);
mac.print();
network.print();
return 0;
}
MLME.h
#ifndef MLME_H_INCLUDED
#define MLME_H_INCLUDED
#include "iostream"
using namespace std;
class MLME {
private:
int i;
public:
MLME();
void print();
void set(int in);
};
#endif
MLME.cpp
#include "MLME.h"
MLME::MLME() {}
void MLME::print() { cout << 'i' << i << endl; }
void MLME::set(int in) {
i = in;
}
MAC.h
#ifndef MAC_H_INCLUDED
#define MAC_H_INCLUDED
#include "MLME.h"
class MAC {
private:
MLME mlme; // NULLED ?
public:
MAC();
MLME& getMLME();
void print();
void set(int in);
};
#endif
MAC.cpp
#include "MAC.h"
MAC::MAC() {
mlme = MLME();
}
MLME& MAC::getMLME() {
return mlme;
}
void MAC::print() {
mlme.print();
}
void MAC::set(int in) {
mlme.set(in);
}
Network.h
#ifndef NETWORK_H_INCLUDED
#define NETWORK_H_INCLUDED
#include "MAC.h"
class Network {
private:
MLME& mlme;
public:
Network(MAC& mac);
void print();
};
#endif
Network.cpp
#include "Network.h"
Network::Network(MAC& mac) : mlme(mac.getMLME()) {}
void Network::print() {
mlme.print();
}
output
i10
i10
i11
i11
So, I create a State class. In that State class's create function, it creates a StateVisual class by calling the StateVisual class's create function, passing itself as a parameter. (The parameter is a reference so that there is no copying). The StateVisual then sets the parameter (The State Instance), as it's parent variable, which is a pointer of a State type.
In the StateVisual's create function, everything works fine. However, when you get to it's update method, and try to print it's parent size, it prints some weird value.
#ifndef STATE_H
#define STATE_H
#include "cocos2d.h"
class StateVisual;
class State {
public:
State();
~State();
static State create();
StateVisual *visual;
float size;
void setSize(float);
void update(float);
private:
cocos2d::Scheduler* _scheduler;
};
#endif
StateVisual.cpp
#ifndef STATE_VIS_H
#define STATE_VIS_H
#include "cocos2d.h"
#include "State.h"
class StateVisual : public cocos2d::Sprite {
public:
StateVisual();
~StateVisual();
// create a visual
State* parent;
static StateVisual* create(State& parent);
cocos2d::Label* label;
void setSize(float);
void update(float);
private:
bool _activated;
float _size;
};
#endif
State.cpp
#include "State.h"
#include "GameScene.h"
State::State() : size(0) {
CCLOG("Created");
}
State::~State() {
}
void State::setSize(float newSize) {
size = newSize;
CCLOG("%f, %f", newSize, size);
}
void State::update(float dt) {
}
State State::create() {
State state;
state.visual = StateVisual::create(state);
cocos2d::SEL_SCHEDULE ss;
return state;
}
StateVisual.cpp
#include "StateVisual.h"
#include "GameScene.h"
using namespace cocos2d;
StateVisual::StateVisual() : parent(nullptr) {
CCLOG("New STATE VISUAL!");
}
StateVisual::~StateVisual() {
}
void StateVisual::setSize(float size) {
setContentSize(Size(size, size));
if (size > 30) {
label->setSystemFontSize(size*.1);
label->setOpacity(255);
}
else {
label->setOpacity(0);
}
}
void StateVisual::update(float dt) {
cocos2d::MathUtil::smooth(&_size, parent->size, dt, .2);
setSize(_size);
CCLOG("%f, %f", _size, (*this->parent).size);
}
StateVisual* StateVisual::create(State &parent) {
StateVisual* visual(new StateVisual());
if (visual->initWithFile("Circle.png"))
{
visual->setSize(200);
visual->_size = 200;
visual->parent = &parent;
visual->parent->setSize(20);
CCLOG("PARENT SIZE: %f", visual->parent->size);
visual->autorelease();
visual->scheduleUpdate();
return visual;
}
CC_SAFE_DELETE(visual);
return NULL;
}
It outputs:
Created
New STATE VISUAL!
cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible
missing file.
20.000000, 20.000000
PARENT SIZE: 20.000000
500.000000, 500.000000
cocos2d: QuadCommand: resizing index size from [-1] to [2560]
168.058044, -107374176.000000 <-- Those are the weird values it prints
155.130508, -107374176.000000
The program '[1464] State.exe' has exited with code 0 (0x0).
0xCCCCCCCC, a typical value used to fill uninitialized memory in Debug builds, interpreted as a 32-bit float, equals -107374176.000000. You're printing an uninitialized float value.
State State::create() {
State state;
state.visual = StateVisual::create(state);
cocos2d::SEL_SCHEDULE ss;
return state;
}
At the end of this function the local object state is destroyed a copy ois made (or moved to a new object). Which means any pointer to the address &state is invalid after the end of State::create
visual->parent = &parent; // parent = state object on stack
Because of this line visual->parent is now dangling.
Those weird values are the random content of you thread stack...
What you should do is use shared pointers for state\parent.
void StateVisual::update(float dt) {
cocos2d::MathUtil::smooth(&_size, parent->size, dt, .2);
setSize(_size);
CCLOG("%f, %f", _size, (*this->parent).size);
Why do you use parent->size in 1 row of function and (*this->parent).size in third?
Why don't you use getters and setters?
Why haven't you shown header files?
Why haven't you prepared minimal example (like just parent-child logic and sizes without labels, positions etc.?
What is this suposed to do? StateVisual* visual(new StateVisual());
If visual is automatic variable, i think it should look loke StateVisual visual(new StateVisual()); otherwise shouldn't it be StateVisual* visual = new StateVisual(new StateVisual()); Also are you sure you want to pass new StateVisual object into constructor of StateVisual?
I'm facing a problem using forward declaration, and I don't know how to fix it. Here's my files:
BubblePlug.h
#ifndef _BUBBLEPLUG_
#define _BUBBLEPLUG_
#include "IPlug_include_in_plug_hdr.h"
#include "resource.h"
#include "IControl.h"
class IPianoRoll;
class IMidiEngine;
class BubblePlug: public IPlug
{
private:
public:
IMidiEngine *pMidiEngine;
IPianoRoll *pPianoRoll;
BubblePlug(IPlugInstanceInfo instanceInfo);
~BubblePlug();
};
#endif // !_BUBBLEPLUG_
BubblePlug.cpp
#include "BubblePlug.h"
#include "IPlug_include_in_plug_src.h"
#include "IPianoRoll.h"
#include "IMidiEngine.h"
BubblePlug::BubblePlug(IPlugInstanceInfo instanceInfo) : IPLUG_CTOR(10, 1, instanceInfo) {
pPianoRoll = new IPianoRoll(this, 8, 8);
pMidiEngine = new IMidiEngine(this);
}
BubblePlug::~BubblePlug() {
delete pPianoRoll;
delete pMidiEngine;
}
IPianoRoll.h
#ifndef _IPIANOROLL_
#define _IPIANOROLL_
#include "IMidiEngine.h"
class IPianoRoll : public IControl
{
private:
BubblePlug *pBubblePlug;
public:
IPianoRoll(BubblePlug *bubbleplug, int x, int y) : IControl(bubbleplug, IRECT(x, y, x + 10, y + 10)), pBubblePlug(bubbleplug) {
}
~IPianoRoll() {
};
bool Draw(IGraphics *pGraphics) {
return true;
}
void Random(bool onlyScore = false) {
pBubblePlug->pMidiEngine->Init();
}
void Start() {
}
};
#endif // !_IPIANOROLL_
IMidiEngine.h
#ifndef _IMIDIENGINE_
#define _IMIDIENGINE_
class IMidiEngine
{
private:
BubblePlug *pBubblePlug;
public:
IMidiEngine(BubblePlug *bubbleplug) : pBubblePlug(bubbleplug) {
}
~IMidiEngine() {
};
void Init(bool randomScore = true) {
pSamplwhk->pPianoRoll->Start();
}
};
#endif // !_IMIDIENGINE_
when I compile, it says around pSamplwhk->pPianoRoll->Start();:
use of undefined type 'IPianoRoll'
left of '->Start' must point to class/struct/union/generic type
VS2015 find each element writing the code (I've no problem), it happens only when I compile (Build).
Why? I pass BubblePlug and I do forward of both IPianoRoll and IMidiEngine, including them in order (on BubblePlug.cpp).
IMidiEngine should know everythings about IPianoRoll (which it is included first).
At least, I should have problem at "runtime", why at compile?
Can you help me to understand the problem and how to fix it? Thanks.
IPianoRoll.h includes IMidiEngine.h, so no matter in which order you include the two files, the definition of IPianoRoll will always come after the init function where it is being used.
One way to avoid this is to move the body of the init function into a separate .cpp file:
In IMidiEngine.h:
void Init(bool randomScore=true);
In IMidiEngine.cpp:
void IMidiEngine::Init(bool randomScore) {
pSamplwhk->pPianoRoll->Start();
}
In the below programme i use one boolean variable named check , which is being accessed inside main function by two objects of Tst1 and Test2 . But the value of check variable is not maintained in the programme . we can use static but i want to know some alternative way ..could anyone give me some hints on it ?
Thanks in advance .
Inside jointdeatils.h
#pragma once
class Jointdetails
{
public:
Jointdetails(void);
~Jointdetails(void);
bool check;
};
Inside jointdeatils.cpp
#include "Jointdetails.h"
Jointdetails::Jointdetails(void)
{
check = false ;
}
Jointdetails::~Jointdetails(void)
{
}
Inside analyzer.h
#pragma once
#include "Jointdetails.h"
class Analyzer
{
public:
Analyzer(void);
Jointdetails* GetJointDetails();
Jointdetails* m_ptheCTJointDetails;
~Analyzer(void);
};
Inside analyzer.cpp
#include "Analyzer.h"
#include "stddef.h"
Analyzer::Analyzer(void)
{
m_ptheCTJointDetails = new Jointdetails();
}
Analyzer::~Analyzer(void)
{
}
Jointdetails* Analyzer::GetJointDetails()
{
if(m_ptheCTJointDetails)
return m_ptheCTJointDetails;
else
return NULL;
}
Inside Test1.h
#pragma once
#include "Analyzer.h"
class Tst1
{
public:
Tst1(void);
Analyzer *analyzer1 ;
public:
~Tst1(void);
};
Inside Test1.cpp
#include "Tst1.h"
Tst1::Tst1(void)
{
analyzer1 = new Analyzer ;
}
Tst1::~Tst1(void)
{
}
Inside Test2.h
#pragma once
#include "Analyzer.h"
class Test2
{
public:
Test2(void);
Analyzer *analyzer2 ;
public:
~Test2(void);
};
Inside Test2.cpp
#include "Test2.h"
Test2::Test2(void)
{
analyzer2 = new Analyzer ;
}
Test2::~Test2(void)
{
}
Inside main.cpp
#include "Test2.h"
#include "Tst1.h"
#include "stdio.h"
int main()
{
Tst1 *test1 = new Tst1 ; //check = false
Test2 *test2 = new Test2 ; //check = false
test1->analyzer1->GetJointDetails()->check = true ;
if(test2->analyzer2->GetJointDetails()->check )
printf("Check value is changed");
else
printf("Check value is not changed");
return 0 ;
}
There are only two possible ways to do so:
Use static storage data
Pass automatic or dynamic storage data as parameters to destination functions/ctors
Way #1 is more handy as you can access such data directly from any function. But it shall be considered as bad design because it almost the same thing as evil global variables.
Way #2 is more correct (see answer by justin for example) but could be a bit irritating - you will need to pass required data as parameter to each required function and/or store data as class data member. Not a pleasant work in case of many classes/nested calls.
Nevertheless, if you don't care on drawbacks of way #1 consider singleton-on-demand concept. It allows to use static data in more dynamic way - create on demand, share access by several users and destroy when nobody use it anymore. See example (several details, includes etc skipped for brevity):
JointDetails.h
class JointDetails
{
// Actual class definition
// ...
public:
// Function accessing to JointDetails instance
static std::shared_ptr<JointDetails> Get();
};
JointDetails.cpp
std::shared_ptr<JointDetails> JointDetails::Get()
{
static std::weak_ptr<JointDetails> s_trackInstance;
if(s_trackInstance.expired())
{
auto instance = std::make_shared<JointDetails>();
s_trackInstance = instance;
return instance;
}
return s_trackInstance.lock();
}
Analyzer.h
// All instances of Analyzer share the same instance of JointDetails.
// But JointDetails instance is created dynamically only when first instance of
// Analyzer is created and destoyed when the last instance of Analyzer is destroyed.
class Analyzer
{
std::shared_ptr<JointDetails> m_details;
public:
Analyzer(): m_details(JointDetails::Get()) {}
const JointDetails& GetDetails() const { return *m_details; }
};
You're either going to have to make check static, or JointDetails a singleton (which also uses the static keyword).
If you make check static, you are saying that all instances of JointDetails have the same check.
If you make JointDetails a singleton, then you're saying that every reference to a JointDetails object is the same object, so your Tst1 and your Test2 will both have a pointer to the same object.
I think the latter is what you're looking for:
Jointdetails.h
#pragma once
class Jointdetails
{
public:
~Jointdetails(void);
bool check;
static Jointdetails* getInstance();
private:
Jointdetails(void);
};
Jointdetails.cpp
#include "Jointdetails.h"
Jointdetails::Jointdetails(void)
{
check = false ;
}
Jointdetails::~Jointdetails(void)
{
}
Jointdetails* Jointdetails::getInstance() {
static Jointdetails s_instance;
return &s_instance;
}
Analyzer.cpp
Analyzer::Analyzer(void)
{
m_ptheCTJointDetails = Jointdetails::getInstance();
}
0) you needn't use new so often.
1) you can construct your objects with the joint details as a parameter in your constructor.
To illustrate:
class Tst1 {
public:
Tst1(Jointdetails& pJointdetails) : analyzer1(pJointdetails) {
}
Analyzer analyzer1;
public:
~Tst1(void);
};
int RunProgram(Jointdetails& pJointdetails) {
Tst1(pJointdetails);
...
}
int main() {
Jointdetails jointdetails;
const int result(RunProgram(jointdetails));
return result;
}
Before I present the code which is found at the bottom of this post I would like to talk about the issue and the fix's that I do not desire. Okay basically I've created a GUI from scratch sort of and one requirement I wanted for this was allow components to have their own click executions so if i click a button or tab etc.. It would call Component->Execute(); Well normally you would do something like a switch statement of ids and if that components ID equaled n number then it would perform this action. Well that seemed kinda dumb to me and I thought there has to be a better way. I eventually tried to incorporate a feature in JAVA where you would do like Component.AddActionListener(new ActionListener( public void execute(ActionEvent ae) { })); or something like that and I thought that this feature has to be possible in C++. I eventually came across storing void functions into a variable in which could be executed at any time and modified at any time. However I hadn't noticed an issue and that was this only worked with static functions. So below you'll see my problem. I've patched the problem by using a pointer to SomeClass however this would mean having an individual function call for every class type is there no way to store a function callback to a non-static class member without doing the below strategy? and instead doing a strategy like the commented out code?
//Main.cpp
#include <iostream> //system requires this.
#include "SomeClass.h"
void DoSomething1(void)
{
std::cout << "We Called Static DoSomething1\n";
}
void DoSomething2(void)
{
std::cout << "We Called Static DoSomething2\n";
}
int main()
{
void (*function_call2)(SomeClass*);
void (*function_call)() = DoSomething1; //This works No Problems!
function_call(); //Will Call the DoSomething1(void);
function_call = DoSomething2; //This works No Problems!
function_call(); //Will Call the DoSomething2(void);
SomeClass *some = new SomeClass(); //Create a SomeClass pointer;
function_call = SomeClass::DoSomething3; //Static SomeClass::DoSomething3();
function_call(); //Will Call the SomeClass::DoSomething3(void);
//function_call = some->DoSomething4; //Non-Static SomeClass::DoSomething4 gives an error.
//function_call(); //Not used because of error above.
function_call2 = SomeClass::DoSomething5; //Store the SomeClass::DoSomething(SomeClass* some);
function_call2(some); //Call out SomeClass::DoSomething5 which calls on SomeClass::DoSomething4's non static member.
system("pause");
return 0;
}
//SomeClass.hpp
#pragma once
#include <iostream>
class SomeClass
{
public:
SomeClass();
~SomeClass();
public:
static void DoSomething3(void);
void DoSomething4(void);
static void DoSomething5(SomeClass* some);
};
//SomeClass.cpp
#include "SomeClass.h"
SomeClass::SomeClass(void)
{
}
SomeClass::~SomeClass(void)
{
}
void SomeClass::DoSomething3(void)
{
std::cout << "We Called Static DoSomething3\n";
}
void SomeClass::DoSomething4(void)
{
std::cout << "We Called Non-Static DoSomething4\n";
}
void SomeClass::DoSomething5(SomeClass *some)
{
some->DoSomething4();
}
Secondary Fix for what I'll do not an exact answer I wanted but it meets my needs for now along with allowing additional features which would have become overly complicate had this not existed.
//Component.hpp
#pragma once
#include <iostream>
#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>
#include "Constants.hpp"
#include "ScreenState.hpp"
#include "ComponentType.hpp"
using namespace std;
class Component
{
static void EMPTY(void) { }
static void EMPTY(int i) { }
public:
Component(void)
{
callback = EMPTY;
callback2 = EMPTY;
callback_id = -1;
}
Component* SetFunction(void (*callback)())
{
this->callback = callback;
return this;
}
Component* SetFunction(void (*callback2)(int), int id)
{
this->callback_id = id;
this->callback2 = callback2;
return this;
}
void execute(void)
{
callback();
callback2(callback_id);
}
}
The syntax for pointers-to-member-functions is as follows:
struct Foo
{
void bar(int, int);
void zip(int, int);
};
Foo x;
void (Foo::*p)(int, int) = &Foo::bar; // pointer
(x.*p)(1, 2); // invocation
p = &Foo::zip;
(x.*p)(3, 4); // invocation
Mind the additional parentheses in the function invocation, which is needed to get the correct operator precedence. The member-dereference operator is .* (and there's also ->* from an instance pointer).