pointer to an object within another class.

closed account (zhq4izwU)
Hi, I'm trying to create a pointer to an object within a different class. I don't really know how to construct the pointer to an object.

There are three base classes: component, wire and event. There is one derived class SignalChangedEvent.

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
class Component;

class Wire {
private:
	Component& component;
	int inputBitNum;
	bool value;
public:
	Wire(Component& c, int i) : component(c), inputBitNum(i), value(false) { }
	void process();
	void setValue(bool v) {value = v;}
};

class Event
{
public:
	// Construct sets time of event.
	Event (unsigned int t) : time (t) { }

	// Execute event by invoking this method.
	virtual void processEvent () = 0;

	const unsigned int time;
};

};



My question is how should the constructor of wire be called in this SignalChangedEvent class. I have already initialized the object *wire in the initializer list of the SignalChangedEvent constructor.

1
2
3
4
5
6
7
8
9
class SignalChangedEvent : public Event
{

private:
	Wire *wire;
	bool value;
public:
	SignalChangedEvent(): wire(new Wire);
	virtual void processEvent();
How about?
SignalChangedEvent(Component& c, int i): wire(new Wire(c,i)) { }

If that doesn't work:
SignalChangedEvent(Component& c, int i) { wire = new Wire(c,i); }
Topic archived. No new replies allowed.